OSDN Git Service

add test for Router::parseConfig
[php-libraries/Router.git] / tests / src / PHPRouterTest / RouterTest.php
1 <?php
2 /**
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.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license.
17  */
18 namespace PHPRouterTest\Test;
19
20 use PHPRouter\Config;
21 use PHPRouter\Route;
22 use PHPRouter\Router;
23 use PHPRouter\RouteCollection;
24 use PHPUnit_Framework_TestCase;
25
26 class RouterTest extends PHPUnit_Framework_TestCase
27 {
28     /**
29      * @dataProvider matcherProvider
30      *
31      * @param Router $router
32      * @param string $path
33      * @param string $expected
34      */
35     public function testMatch($router, $path, $expected)
36     {
37         $this->assertEquals($expected, (bool)$router->match($path));
38     }
39
40     public function testBasePathConfigIsSettedProperly()
41     {
42         $router = new Router(new RouteCollection);
43         $router->setBasePath('/webroot/');
44
45         $this->assertAttributeEquals('/webroot', 'basePath', $router);
46     }
47
48     public function testMatchRouterUsingBasePath()
49     {
50         $collection = new RouteCollection();
51         $collection->attach(new Route('/users/', array(
52             '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
53             'methods' => 'GET'
54         )));
55
56         $router = new Router($collection);
57         $router->setBasePath('/localhost/webroot');
58
59         $_SERVER = array();
60         $_SERVER['REQUEST_METHOD'] = 'GET';
61         $_SERVER['REQUEST_URI'] = '/localhost/webroot/users/';
62         $_SERVER['SCRIPT_NAME'] = 'index.php';
63
64         $this->assertTrue((bool)$router->matchCurrentRequest());
65     }
66
67     public function testGetParamsInsideControllerMethod()
68     {
69         $collection = new RouteCollection();
70         $route = new Route(
71             '/page/:page_id',
72             array(
73                 '_controller' => 'PHPRouter\Test\SomeController::page',
74                 'methods' => 'GET'
75             )
76         );
77         $route->setFilters(array('page_id' => '([a-zA-Z]+)'), true);
78         $collection->attachRoute($route);
79
80         $router = new Router($collection);
81         $this->assertEquals(
82             array(array('page_id' => 'MySuperPage')),
83             $router->match('/page/MySuperPage')->getParameters()
84         );
85     }
86
87     public function testParamsWithDynamicFilterMatch()
88     {
89         $collection = new RouteCollection();
90         $route = new Route(
91             '/js/:filename.js',
92             array(
93                 '_controller' => 'PHPRouter\Test\SomeController::dynamicFilterUrlMatch',
94                 'methods' => 'GET',
95             )
96         );
97         $route->setFilters(array(':filename' => '([[:alnum:]\.]+)'), true);
98         $collection->attachRoute($route);
99
100         $router = new Router($collection);
101         $this->assertEquals(
102             array(array('filename' => 'someJsFile')),
103             $router->match('/js/someJsFile.js')->getParameters()
104         );
105
106         $this->assertEquals(
107             array(array('filename' => 'someJsFile.min')),
108             $router->match('/js/someJsFile.min.js')->getParameters()
109         );
110
111         $this->assertEquals(
112             array(array('filename' => 'someJsFile.min.js')),
113             $router->match('/js/someJsFile.min.js.js')->getParameters()
114         );
115     }
116
117     public function testParseConfig()
118     {
119         $config = Config::loadFromFile(__DIR__ . '/../../Fixtures/router.yaml');
120         $router = Router::parseConfig($config);
121         $this->assertAttributeEquals($config['base_path'], 'basePath', $router);
122     }
123
124     /**
125      * @return Router
126      */
127     private function getRouter()
128     {
129         $collection = new RouteCollection();
130         $collection->attachRoute(new Route('/users/', array(
131             '_controller' => 'PHPRouter\Test\SomeController::usersCreate',
132             'methods' => 'GET'
133         )));
134         $collection->attachRoute(new Route('/user/:id', array(
135             '_controller' => 'PHPRouter\Test\SomeController::user',
136             'methods' => 'GET'
137         )));
138         $collection->attachRoute(new Route('/', array(
139             '_controller' => 'PHPRouter\Test\SomeController::indexAction',
140             'methods' => 'GET'
141         )));
142
143         return new Router($collection);
144     }
145
146     /**
147      * @return mixed[][]
148      */
149     public function matcherProvider1()
150     {
151         $router = $this->getRouter();
152
153         return array(
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),
160         );
161     }
162
163     /**
164      * @return mixed[][]
165      */
166     public function matcherProvider2()
167     {
168         $router = $this->getRouter();
169         $router->setBasePath('/api');
170
171         return array(
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),
183         );
184     }
185
186     /**
187      * @return string[]
188      */
189     public function matcherProvider()
190     {
191         return array_merge($this->matcherProvider1(), $this->matcherProvider2());
192     }
193 }