3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license.
18 namespace PHPRouterTest\Test;
23 use PHPRouter\RouteCollection;
24 use PHPUnit_Framework_TestCase;
26 class RouterTest extends PHPUnit_Framework_TestCase
29 * @dataProvider matcherProvider
31 * @param Router $router
33 * @param string $expected
35 public function testMatch($router, $path, $expected)
37 $this->assertEquals($expected, (bool)$router->match($path));
40 public function testMatchWrongMethod()
42 $router = $this->getRouter();
43 $this->assertFalse($router->match('/users', 'POST'));
46 public function testBasePathConfigIsSettedProperly()
48 $router = new Router(new RouteCollection);
49 $router->setBasePath('/webroot/');
51 $this->assertAttributeEquals('/webroot', 'basePath', $router);
54 public function testMatchRouterUsingBasePath()
56 $collection = new RouteCollection();
57 $collection->attach(new Route('/users/', array(
58 '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
62 $router = new Router($collection);
63 $router->setBasePath('/localhost/webroot');
65 foreach ($this->serverProvider() as $server) {
67 $this->assertTrue((bool)$router->matchCurrentRequest());
71 private function serverProvider()
75 'REQUEST_METHOD' => 'GET',
76 'REQUEST_URI' => '/localhost/webroot/users/',
77 'SCRIPT_NAME' => 'index.php'
80 'REQUEST_METHOD' => 'GET',
81 'REQUEST_URI' => '/localhost/webroot/users/?foo=bar&bar=foo',
82 'SCRIPT_NAME' => 'index.php'
87 public function testGetParamsInsideControllerMethod()
89 $collection = new RouteCollection();
93 '_controller' => 'PHPRouter\Test\SomeController::page',
97 $route->setFilters(array('page_id' => '([a-zA-Z]+)'), true);
98 $collection->attachRoute($route);
100 $router = new Router($collection);
102 array(array('page_id' => 'MySuperPage')),
103 $router->match('/page/MySuperPage')->getParameters()
107 public function testParamsWithDynamicFilterMatch()
109 $collection = new RouteCollection();
113 '_controller' => 'PHPRouter\Test\SomeController::dynamicFilterUrlMatch',
117 $route->setFilters(array(':filename' => '([[:alnum:]\.]+)'), true);
118 $collection->attachRoute($route);
120 $router = new Router($collection);
122 array(array('filename' => 'someJsFile')),
123 $router->match('/js/someJsFile.js')->getParameters()
127 array(array('filename' => 'someJsFile.min')),
128 $router->match('/js/someJsFile.min.js')->getParameters()
132 array(array('filename' => 'someJsFile.min.js')),
133 $router->match('/js/someJsFile.min.js.js')->getParameters()
137 public function testParseConfig()
139 $config = Config::loadFromFile(__DIR__ . '/../../Fixtures/router.yaml');
140 $router = Router::parseConfig($config);
141 $this->assertAttributeEquals($config['base_path'], 'basePath', $router);
144 public function testGenerate()
146 $router = $this->getRouter();
147 $this->assertSame('/users/', $router->generate('users'));
148 $this->assertSame('/user/123', $router->generate('user', array('id' => 123)));
152 * @expectedException \Exception
154 public function testGenerateNotExistent()
156 $router = $this->getRouter();
157 $this->assertSame('/notExists/', $router->generate('notThisRoute'));
163 private function getRouter()
165 $collection = new RouteCollection();
166 $collection->attachRoute(new Route('/users/', array(
167 '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
171 $collection->attachRoute(new Route('/user/:id', array(
172 '_controller' => 'PHPRouter\Test\SomeController::user',
176 $collection->attachRoute(new Route('/', array(
177 '_controller' => 'PHPRouter\Test\SomeController::indexAction',
182 return new Router($collection);
188 public function matcherProvider1()
190 $router = $this->getRouter();
193 array($router, '', true),
194 array($router, '/', true),
195 array($router, '/aaa', false),
196 array($router, '/users', true),
197 array($router, '/user/1', true),
198 array($router, '/user/%E3%81%82', true),
205 public function matcherProvider2()
207 $router = $this->getRouter();
208 $router->setBasePath('/api');
211 array($router, '', false),
212 array($router, '/', false),
213 array($router, '/aaa', false),
214 array($router, '/users', false),
215 array($router, '/user/1', false),
216 array($router, '/user/%E3%81%82', false),
217 array($router, '/api', true),
218 array($router, '/api/aaa', false),
219 array($router, '/api/users', true),
220 array($router, '/api/user/1', true),
221 array($router, '/api/user/%E3%81%82', true),
228 public function matcherProvider()
230 return array_merge($this->matcherProvider1(), $this->matcherProvider2());