@@ -10,136 +10,136 @@ |
||
| 10 | 10 | |
| 11 | 11 | class RouterTest extends \PHPUnit_Framework_TestCase |
| 12 | 12 | { |
| 13 | - /** @var Router */ |
|
| 14 | - protected $router; |
|
| 15 | - |
|
| 16 | - public function setUp() |
|
| 17 | - { |
|
| 18 | - $this->router = new \Aimeos\Slim\Router; |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public function testRelativePathFor() |
|
| 22 | - { |
|
| 23 | - $this->router->setBasePath('/base/path'); |
|
| 24 | - |
|
| 25 | - $methods = ['GET']; |
|
| 26 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
| 27 | - $callable = function ($request, $response, $args) { |
|
| 28 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 29 | - }; |
|
| 30 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 31 | - $route->setName('foo'); |
|
| 32 | - |
|
| 33 | - $this->assertEquals( |
|
| 34 | - '/hello/josh/lockhart', |
|
| 35 | - $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 36 | - ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function testPathForWithNoBasePath() |
|
| 40 | - { |
|
| 41 | - $this->router->setBasePath(''); |
|
| 42 | - |
|
| 43 | - $methods = ['GET']; |
|
| 44 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
| 45 | - $callable = function ($request, $response, $args) { |
|
| 46 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 47 | - }; |
|
| 48 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 49 | - $route->setName('foo'); |
|
| 50 | - |
|
| 51 | - $this->assertEquals( |
|
| 52 | - '/hello/josh/lockhart', |
|
| 53 | - $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function testPathForWithBasePath() |
|
| 58 | - { |
|
| 59 | - $methods = ['GET']; |
|
| 60 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
| 61 | - $callable = function ($request, $response, $args) { |
|
| 62 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 63 | - }; |
|
| 64 | - $this->router->setBasePath('/base/path'); |
|
| 65 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 66 | - $route->setName('foo'); |
|
| 67 | - |
|
| 68 | - $this->assertEquals( |
|
| 69 | - '/base/path/hello/josh/lockhart', |
|
| 70 | - $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 71 | - ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function testPathForWithOptionalParameters() |
|
| 75 | - { |
|
| 76 | - $methods = ['GET']; |
|
| 77 | - $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]'; |
|
| 78 | - $callable = function ($request, $response, $args) { |
|
| 79 | - return $response; |
|
| 80 | - }; |
|
| 81 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 82 | - $route->setName('foo'); |
|
| 83 | - |
|
| 84 | - $this->assertEquals( |
|
| 85 | - '/archive/2015', |
|
| 86 | - $this->router->pathFor('foo', ['year' => '2015']) |
|
| 87 | - ); |
|
| 88 | - $this->assertEquals( |
|
| 89 | - '/archive/2015/07', |
|
| 90 | - $this->router->pathFor('foo', ['year' => '2015', 'month' => '07']) |
|
| 91 | - ); |
|
| 92 | - $this->assertEquals( |
|
| 93 | - '/archive/2015/07/d/19', |
|
| 94 | - $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19']) |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - public function testPathForWithSurplusRouteParameters() |
|
| 99 | - { |
|
| 100 | - $methods = ['GET']; |
|
| 101 | - $pattern = '/hello/{name}'; |
|
| 102 | - $callable = function ($request, $response, $args) { |
|
| 103 | - echo sprintf('Hello %s', $args['name']); |
|
| 104 | - }; |
|
| 105 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 106 | - $route->setName('foo'); |
|
| 107 | - |
|
| 108 | - $this->assertEquals( |
|
| 109 | - '/hello/josh?a=b', |
|
| 110 | - $this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b']) |
|
| 111 | - ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @expectedException \InvalidArgumentException |
|
| 116 | - */ |
|
| 117 | - public function testPathForWithMissingSegmentData() |
|
| 118 | - { |
|
| 119 | - $methods = ['GET']; |
|
| 120 | - $pattern = '/hello/{first}/{last}'; |
|
| 121 | - $callable = function ($request, $response, $args) { |
|
| 122 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 123 | - }; |
|
| 124 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 125 | - $route->setName('foo'); |
|
| 126 | - |
|
| 127 | - $this->router->pathFor('foo', ['last' => 'lockhart']); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @expectedException \RuntimeException |
|
| 132 | - */ |
|
| 133 | - public function testPathForRouteNotExists() |
|
| 134 | - { |
|
| 135 | - $methods = ['GET']; |
|
| 136 | - $pattern = '/hello/{first}/{last}'; |
|
| 137 | - $callable = function ($request, $response, $args) { |
|
| 138 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 139 | - }; |
|
| 140 | - $route = $this->router->map($methods, $pattern, $callable); |
|
| 141 | - $route->setName('foo'); |
|
| 142 | - |
|
| 143 | - $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']); |
|
| 144 | - } |
|
| 13 | + /** @var Router */ |
|
| 14 | + protected $router; |
|
| 15 | + |
|
| 16 | + public function setUp() |
|
| 17 | + { |
|
| 18 | + $this->router = new \Aimeos\Slim\Router; |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public function testRelativePathFor() |
|
| 22 | + { |
|
| 23 | + $this->router->setBasePath('/base/path'); |
|
| 24 | + |
|
| 25 | + $methods = ['GET']; |
|
| 26 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
| 27 | + $callable = function ($request, $response, $args) { |
|
| 28 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 29 | + }; |
|
| 30 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 31 | + $route->setName('foo'); |
|
| 32 | + |
|
| 33 | + $this->assertEquals( |
|
| 34 | + '/hello/josh/lockhart', |
|
| 35 | + $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function testPathForWithNoBasePath() |
|
| 40 | + { |
|
| 41 | + $this->router->setBasePath(''); |
|
| 42 | + |
|
| 43 | + $methods = ['GET']; |
|
| 44 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
| 45 | + $callable = function ($request, $response, $args) { |
|
| 46 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 47 | + }; |
|
| 48 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 49 | + $route->setName('foo'); |
|
| 50 | + |
|
| 51 | + $this->assertEquals( |
|
| 52 | + '/hello/josh/lockhart', |
|
| 53 | + $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function testPathForWithBasePath() |
|
| 58 | + { |
|
| 59 | + $methods = ['GET']; |
|
| 60 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
| 61 | + $callable = function ($request, $response, $args) { |
|
| 62 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 63 | + }; |
|
| 64 | + $this->router->setBasePath('/base/path'); |
|
| 65 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 66 | + $route->setName('foo'); |
|
| 67 | + |
|
| 68 | + $this->assertEquals( |
|
| 69 | + '/base/path/hello/josh/lockhart', |
|
| 70 | + $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function testPathForWithOptionalParameters() |
|
| 75 | + { |
|
| 76 | + $methods = ['GET']; |
|
| 77 | + $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]'; |
|
| 78 | + $callable = function ($request, $response, $args) { |
|
| 79 | + return $response; |
|
| 80 | + }; |
|
| 81 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 82 | + $route->setName('foo'); |
|
| 83 | + |
|
| 84 | + $this->assertEquals( |
|
| 85 | + '/archive/2015', |
|
| 86 | + $this->router->pathFor('foo', ['year' => '2015']) |
|
| 87 | + ); |
|
| 88 | + $this->assertEquals( |
|
| 89 | + '/archive/2015/07', |
|
| 90 | + $this->router->pathFor('foo', ['year' => '2015', 'month' => '07']) |
|
| 91 | + ); |
|
| 92 | + $this->assertEquals( |
|
| 93 | + '/archive/2015/07/d/19', |
|
| 94 | + $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19']) |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + public function testPathForWithSurplusRouteParameters() |
|
| 99 | + { |
|
| 100 | + $methods = ['GET']; |
|
| 101 | + $pattern = '/hello/{name}'; |
|
| 102 | + $callable = function ($request, $response, $args) { |
|
| 103 | + echo sprintf('Hello %s', $args['name']); |
|
| 104 | + }; |
|
| 105 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 106 | + $route->setName('foo'); |
|
| 107 | + |
|
| 108 | + $this->assertEquals( |
|
| 109 | + '/hello/josh?a=b', |
|
| 110 | + $this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b']) |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @expectedException \InvalidArgumentException |
|
| 116 | + */ |
|
| 117 | + public function testPathForWithMissingSegmentData() |
|
| 118 | + { |
|
| 119 | + $methods = ['GET']; |
|
| 120 | + $pattern = '/hello/{first}/{last}'; |
|
| 121 | + $callable = function ($request, $response, $args) { |
|
| 122 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 123 | + }; |
|
| 124 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 125 | + $route->setName('foo'); |
|
| 126 | + |
|
| 127 | + $this->router->pathFor('foo', ['last' => 'lockhart']); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @expectedException \RuntimeException |
|
| 132 | + */ |
|
| 133 | + public function testPathForRouteNotExists() |
|
| 134 | + { |
|
| 135 | + $methods = ['GET']; |
|
| 136 | + $pattern = '/hello/{first}/{last}'; |
|
| 137 | + $callable = function ($request, $response, $args) { |
|
| 138 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
| 139 | + }; |
|
| 140 | + $route = $this->router->map($methods, $pattern, $callable); |
|
| 141 | + $route->setName('foo'); |
|
| 142 | + |
|
| 143 | + $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']); |
|
| 144 | + } |
|
| 145 | 145 | } |