Skip to content

Commit

Permalink
Merge branch '5.x' of github.com:thephpleague/route into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Jul 30, 2021
2 parents aeae4d1 + 01ab87d commit 1816d72
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
7 changes: 3 additions & 4 deletions docs/5.x/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ use Psr\Http\Message\ServerRequestInterface;

$router = new League\Route\Router;

// this route will only match if {id} is numeric and {name} is a alpha
// this route will only match if {id} is numeric and {name} is an alpha
$router->map('GET', '/user/{id:number}/{name:word}', function (ServerRequestInterface $request, array $args): ResponseInterface {
// $args = [
// 'id' => {id}, // the actual value of {id}
Expand Down Expand Up @@ -222,19 +222,18 @@ use Psr\Http\Message\ServerRequestInterface;

$router = new League\Route\Router;

$router->addPatternMatcher('wordStartsWithM', '(m|M)[a-zA-Z]+');
$router->addPatternMatcher('wordStartsWithM', '(?:m|M)[a-zA-Z]+');

$router->map('GET', 'user/mTeam/{name:wordStartsWithM}', function (
ServerRequestInterface $request,
array $args
): ResponseInterface {
// $args = [
// 'id' => {id}, // the actual value of {id}
// 'name' => {name} // the actual value of {name}
// ];

// ...
});
~~~

The above pattern matcher will create an internal regular expression string: `{$1:(m|M)[a-zA-Z]+}`, where `$1` will interpret to `name`, the variable listed before the colon.
The above pattern matcher will create an internal regular expression string: `{$1:(?:m|M)[a-zA-Z]+}`, where `$1` will interpret to `name`, the variable listed before the colon.
62 changes: 61 additions & 1 deletion tests/DispatchIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace League\Route;

use Exception;
use League\Route\Fixture\Controller;
use League\Route\Fixture\Middleware;
use League\Route\Http\Exception\{BadRequestException, MethodNotAllowedException, NotFoundException};
use League\Route\Strategy\JsonStrategy;
Expand Down Expand Up @@ -118,6 +117,67 @@ public function testDispatchesFoundRouteMultipleTimes(): void
$this->assertSame($response, $returnedResponse);
}

/** @dataProvider wordsStartingWithM */
public function testDispatchesFoundRouteMatchingPattern(string $wordStartingWithM): void
{
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
$uri = $this->createMock(UriInterface::class);

$uri
->expects($this->exactly(2))
->method('getPath')
->willReturn('/example/' . $wordStartingWithM)
;

$request
->expects($this->once())
->method('getMethod')
->willReturn('GET')
;

$request
->expects($this->exactly(2))
->method('getUri')
->willReturn($uri)
;

$request
->expects($this->once())
->method('withAttribute')
->willReturn($request)
;

$router = new Router();
$router->addPatternMatcher('wordStartsWithM', '(?:m|M)[a-zA-Z]+');

$router->map('GET', '/example/{name:wordStartsWithM}', function (
ServerRequestInterface $request,
array $args
) use (
$response,
$wordStartingWithM
): ResponseInterface {
$this->assertSame([
'name' => $wordStartingWithM
], $args);

return $response;
});

$returnedResponse = $router->handle($request);
$this->assertSame($response, $returnedResponse);
}

public function wordsStartingWithM(): array
{
return [
'min length' => ['Mi'],
'upper case' => ['Max'],
'lower case' => ['magnetic'],
];
}

public function testDispatchesExceptionRoute(): void
{
$this->expectException(Exception::class);
Expand Down

0 comments on commit 1816d72

Please sign in to comment.