Special case the empty rule.
[hoplite.git] / testing / tests / http / url_map_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\http as http;
19
20 require_once HOPLITE_ROOT . '/http/url_map.php';
21
22 class UrlMapTest extends \PHPUnit_Framework_TestCase
23 {
24 public function setUp()
25 {
26 $globals = array();
27 $this->fixture = new http\UrlMap(new http\RootController($globals));
28 }
29
30 public function testSimpleEvaluate()
31 {
32 $map = array(
33 'action/two' => 'Invalid',
34 'some/long/action' => 'Valid'
35 );
36 $this->fixture->set_map($map);
37
38 $request = new http\Request('some/long/action');
39 $this->assertEquals('Valid', $this->fixture->Evaluate($request));
40
41 $request = new http\Request('another/action');
42 $this->assertNull($this->fixture->Evaluate($request));
43 }
44
45 public function testSimpleExtendedEvaluate()
46 {
47 $map = array(
48 'one/two/three' => 'Invald',
49 'another/action' => 'Valid',
50 'four/five/six' => 'Invalid2'
51 );
52 $this->fixture->set_map($map);
53
54 $request = new http\Request('another/action/thing');
55 $this->assertEquals('Valid', $this->fixture->Evaluate($request));
56 }
57
58 public function testSimplePrecedenceEvaluate()
59 {
60 $map = array(
61 'some/action/first' => 'First',
62 'some/action' => 'Second',
63 'some/action/second' => 'Third'
64 );
65 $this->fixture->set_map($map);
66
67 $request = new http\Request('some/action/first');
68 $this->assertEquals('First', $this->fixture->Evaluate($request));
69
70 $request = new http\Request('some/action/second');
71 $this->assertEquals('Second', $this->fixture->Evaluate($request));
72
73 $request = new http\Request('some/action/third');
74 $this->assertEquals('Second', $this->fixture->Evaluate($request));
75 }
76
77 public function testEmptyRule()
78 {
79 $map = array(
80 'some/first' => 'First',
81 '' => 'Index',
82 'some/second' => 'Second'
83 );
84 $this->fixture->set_map($map);
85
86 $request = new http\Request('some/first');
87 $this->assertEquals('First', $this->fixture->Evaluate($request));
88
89 $request = new http\Request('');
90 $this->assertEquals('Index', $this->fixture->Evaluate($request));
91
92 $request = new http\Request('some/second');
93 $this->assertEquals('Second', $this->fixture->Evaluate($request));
94 }
95
96 public function testExtractSingle()
97 {
98 $map = array(
99 'action/one' => 'First',
100 'user/view/{id}' => 'Second',
101 'user/add' => 'Third'
102 );
103 $this->fixture->set_map($map);
104
105 $request = new http\Request('user/view/42');
106 $this->assertEquals('Second', $this->fixture->Evaluate($request));
107 $this->assertEquals('42', $request->data['id']);
108
109 $request = new http\Request('user/add');
110 $this->assertEquals('Third', $this->fixture->Evaluate($request));
111 }
112
113 public function testExtractDouble()
114 {
115 $map = array(
116 'action/unused' => 'Invalid',
117 'document/{action}/{key}' => 'DocumentController'
118 );
119 $this->fixture->set_map($map);
120
121 $request = new http\Request('document/edit/42');
122 $this->assertEquals('DocumentController', $this->fixture->Evaluate($request));
123 $this->assertEquals('edit', $request->data['action']);
124 $this->assertEquals('42', $request->data['key']);
125 }
126
127 public function testExactMatch()
128 {
129 $map = array(
130 'action/one' => 'First',
131 'action/two//' => 'Second',
132 'action/two/alpha' => 'Third'
133 );
134 $this->fixture->set_map($map);
135
136 $request = new http\Request('action/one');
137 $this->assertEquals('First', $this->fixture->Evaluate($request));
138
139 $request = new http\Request('action/two');
140 $this->assertEquals('Second', $this->fixture->Evaluate($request));
141
142 $request = new http\Request('action/two/alpha');
143 $this->assertEquals('Third', $this->fixture->Evaluate($request));
144 }
145
146 public function testRegEx()
147 {
148 $map = array(
149 'user/test' => 'First',
150 '/user\/([a-z]+)(\/([0-9]+))?/' => 'Second',
151 'user/TEST' => 'Third'
152 );
153 $this->fixture->set_map($map);
154
155 $request = new http\Request('user/test');
156 $this->assertEquals('First', $this->fixture->Evaluate($request));
157
158 $request = new http\Request('user/add');
159 $this->assertEquals('Second', $this->fixture->Evaluate($request));
160 $this->assertEquals('add', $request->data['url_pattern'][1]);
161 $this->assertTrue(!isset($request->data['url_pattern'][2]));
162
163 $request = new http\Request('user/view/14');
164 $this->assertEquals('Second', $this->fixture->Evaluate($request));
165 $this->assertEquals('view', $request->data['url_pattern'][1]);
166 $this->assertEquals('14', $request->data['url_pattern'][3]);
167
168 $request = new http\Request('user/TEST');
169 $this->assertEquals('Third', $this->fixture->Evaluate($request));
170 }
171
172 public function testLookupActionClass()
173 {
174 $test_class = '\hoplite\test\TestAction';
175 $this->assertEquals($test_class, $this->fixture->LookupAction($test_class));
176
177 $test_class = 'TestAction';
178 $this->assertEquals($test_class, $this->fixture->LookupAction($test_class));
179 }
180
181 public function testLookupActionFile()
182 {
183 $this->assertEquals('TestAction', $this->fixture->LookupAction('actions/test_action'));
184 $this->assertEquals('TestAction', $this->fixture->LookupAction('actions/test_action.php'));
185 }
186 }