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 testBasePathConfigIsSettedProperly()
42 $router = new Router(new RouteCollection);
43 $router->setBasePath('/webroot/');
45 $this->assertAttributeEquals('/webroot', 'basePath', $router);
48 public function testMatchRouterUsingBasePath()
50 $collection = new RouteCollection();
51 $collection->attach(new Route('/users/', array(
52 '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
56 $router = new Router($collection);
57 $router->setBasePath('/localhost/webroot');
60 $_SERVER['REQUEST_METHOD'] = 'GET';
61 $_SERVER['REQUEST_URI'] = '/localhost/webroot/users/';
62 $_SERVER['SCRIPT_NAME'] = 'index.php';
64 $this->assertTrue((bool)$router->matchCurrentRequest());
67 public function testGetParamsInsideControllerMethod()
69 $collection = new RouteCollection();
73 '_controller' => 'PHPRouter\Test\SomeController::page',
77 $route->setFilters(array('page_id' => '([a-zA-Z]+)'), true);
78 $collection->attachRoute($route);
80 $router = new Router($collection);
82 array(array('page_id' => 'MySuperPage')),
83 $router->match('/page/MySuperPage')->getParameters()
87 public function testParamsWithDynamicFilterMatch()
89 $collection = new RouteCollection();
93 '_controller' => 'PHPRouter\Test\SomeController::dynamicFilterUrlMatch',
97 $route->setFilters(array(':filename' => '([[:alnum:]\.]+)'), true);
98 $collection->attachRoute($route);
100 $router = new Router($collection);
102 array(array('filename' => 'someJsFile')),
103 $router->match('/js/someJsFile.js')->getParameters()
107 array(array('filename' => 'someJsFile.min')),
108 $router->match('/js/someJsFile.min.js')->getParameters()
112 array(array('filename' => 'someJsFile.min.js')),
113 $router->match('/js/someJsFile.min.js.js')->getParameters()
117 public function testParseConfig()
119 $config = Config::loadFromFile(__DIR__ . '/../../Fixtures/router.yaml');
120 $router = Router::parseConfig($config);
121 $this->assertAttributeEquals($config['base_path'], 'basePath', $router);
127 private function getRouter()
129 $collection = new RouteCollection();
130 $collection->attachRoute(new Route('/users/', array(
131 '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
134 $collection->attachRoute(new Route('/user/:id', array(
135 '_controller' => 'PHPRouter\Test\SomeController::user',
138 $collection->attachRoute(new Route('/', array(
139 '_controller' => 'PHPRouter\Test\SomeController::indexAction',
143 return new Router($collection);
149 public function matcherProvider1()
151 $router = $this->getRouter();
154 array($router, '', true),
155 array($router, '/', true),
156 array($router, '/aaa', false),
157 array($router, '/users', true),
158 array($router, '/user/1', true),
159 array($router, '/user/%E3%81%82', true),
166 public function matcherProvider2()
168 $router = $this->getRouter();
169 $router->setBasePath('/api');
172 array($router, '', false),
173 array($router, '/', false),
174 array($router, '/aaa', false),
175 array($router, '/users', false),
176 array($router, '/user/1', false),
177 array($router, '/user/%E3%81%82', false),
178 array($router, '/api', true),
179 array($router, '/api/aaa', false),
180 array($router, '/api/users', true),
181 array($router, '/api/user/1', true),
182 array($router, '/api/user/%E3%81%82', true),
189 public function matcherProvider()
191 return array_merge($this->matcherProvider1(), $this->matcherProvider2());