OSDN Git Service

add more route unit tests
[php-libraries/Router.git] / tests / src / PHPRouterTest / RouteTest.php
1 <?php
2
3 namespace PHPRouterTest\Test;
4
5 use PHPRouter\Route;
6 use PHPRouter\Test\Fixtures\InvokableController;
7 use PHPUnit_Framework_TestCase;
8
9 class RouteTest extends PHPUnit_Framework_TestCase
10 {
11     private $routeUsing__invoke;
12     private $routeWithParameters;
13     private $routeInvalid;
14
15     protected function setUp()
16     {
17         $this->routeUsing__invoke = new Route(
18             '/home/:user/:id',
19             array(
20                 '_controller' => '\PHPRouter\Test\Fixtures\InvokableController',
21                 'methods'     => array(
22                     'GET',
23                 ),
24             )
25         );
26
27         $this->routeWithParameters = new Route(
28             '/page/:page_id/:page_size',
29             array(
30                 '_controller' => '\PHPRouter\Test\Fixtures\SomeController::page',
31                 'methods'     => array(
32                     'GET',
33                 ),
34                 'target'      => 'thisIsAString',
35                 'name'        => 'page',
36             )
37         );
38
39         $this->routeInvalid = new Route(
40             '/test',
41             array(
42                 '_controller' => '\PHPRouter\Test\Fixtures\TestController::page',
43                 'methods'     => array(
44                     'GET',
45                 ),
46                 'target'      => 'thisIsAString',
47                 'name'        => 'page',
48             )
49         );
50     }
51
52     public function testGetUrl()
53     {
54         $this->assertEquals('/page/:page_id/:page_size', $this->routeWithParameters->getUrl());
55     }
56
57     public function testSetUrl()
58     {
59         $this->routeWithParameters->setUrl('/pages/:page_name/');
60         $this->assertEquals('/pages/:page_name/', $this->routeWithParameters->getUrl());
61
62         $this->routeWithParameters->setUrl('/pages/:page_name');
63         $this->assertEquals('/pages/:page_name/', $this->routeWithParameters->getUrl());
64     }
65
66     public function testGetMethods()
67     {
68         $this->assertEquals(array('GET'), $this->routeWithParameters->getMethods());
69     }
70
71     public function testSetMethods()
72     {
73         $this->routeWithParameters->setMethods(array('POST'));
74         $this->assertEquals(array('POST'), $this->routeWithParameters->getMethods());
75
76         $this->routeWithParameters->setMethods(array('GET', 'POST', 'PUT', 'DELETE'));
77         $this->assertEquals(array('GET', 'POST', 'PUT', 'DELETE'), $this->routeWithParameters->getMethods());
78     }
79
80     public function testGetTarget()
81     {
82         $this->assertEquals('thisIsAString', $this->routeWithParameters->getTarget());
83     }
84
85     public function testSetTarget()
86     {
87         $this->routeWithParameters->setTarget('ThisIsAnotherString');
88         $this->assertEquals('ThisIsAnotherString', $this->routeWithParameters->getTarget());
89     }
90
91     public function testGetName()
92     {
93         $this->assertEquals('page', $this->routeWithParameters->getName());
94     }
95
96     public function testSetName()
97     {
98         $this->routeWithParameters->setName('pageroute');
99         $this->assertEquals('pageroute', $this->routeWithParameters->getName());
100     }
101
102     public function testGetAction()
103     {
104         $this->assertEquals('page', $this->routeWithParameters->getAction());
105         $this->assertEquals(null, $this->routeUsing__invoke->getAction());
106     }
107
108     public function testGetClass()
109     {
110         $this->assertEquals('\PHPRouter\Test\Fixtures\SomeController', $this->routeWithParameters->getClass());
111         $this->assertEquals('\PHPRouter\Test\Fixtures\InvokableController', $this->routeUsing__invoke->getClass());
112     }
113
114     public function testGetValidController()
115     {
116         $this->assertEquals("\PHPRouter\Test\Fixtures\SomeController::page",
117                             $this->routeWithParameters->getValidController());
118         $this->assertNull($this->routeInvalid->getValidController());
119     }
120
121     public function testSetGetParameters()
122     {
123         $param = array("page_id" => 123);
124         $this->routeWithParameters->setParameters($param);
125
126         $this->assertEquals($param, $this->routeWithParameters->getParameters());
127     }
128
129     public function testGetRegex()
130     {
131         $regex = $this->routeWithParameters->getRegex();
132
133         $this->assertEquals("/page/([\\w-%]+)/([\\w-%]+)", $regex);
134     }
135
136     public function testGetFilterRegex()
137     {
138         $filters = array(
139             ":user" => "([A-Z][a-z]+)",
140             ":id"   => "([1-9]\\d)",
141         );
142
143         $this->routeUsing__invoke->setFilters($filters);
144
145         $regex = $this->routeUsing__invoke->getRegex();
146
147         $this->assertEquals("/home/([A-Z][a-z]+)/([1-9]\\d)", $regex);
148     }
149
150     public function testDispatch()
151     {
152         $message = "welcome";
153         $param   = array(
154             "id"   => 1,
155             "user" => "akane",
156         );
157
158         $this->routeUsing__invoke->setParameters($param);
159
160         $this->assertEquals(" {$param['id']}:{$param['user']}", $this->routeUsing__invoke->dispatch());
161
162         $instance = new InvokableController();
163
164         $instance->message = $message;
165
166         $this->assertEquals("$message {$param['id']}:{$param['user']}", $this->routeUsing__invoke->dispatch($instance));
167     }
168 }