]> src.bluestatic.org Git - hoplite.git/blob - testing/tests/http/url_map_test.php
Allow an array of variables to be passed to Template::Render()
[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 TestAction extends http\Action
23 {
24 public function Invoke(http\Request $request, http\Response $response) {}
25 }
26
27 class UrlMapTest extends \PHPUnit_Framework_TestCase
28 {
29 public function setUp()
30 {
31 $globals = array();
32 $this->fixture = new http\UrlMap(new http\RootController($globals));
33 }
34
35 public function testSimpleEvaluate()
36 {
37 $map = array(
38 'action/two' => 'Invalid',
39 'some/long/action' => 'Valid'
40 );
41 $this->fixture->set_map($map);
42
43 $request = new http\Request('some/long/action');
44 $this->assertEquals('Valid', $this->fixture->Evaluate($request));
45
46 $request = new http\Request('another/action');
47 $this->assertNull($this->fixture->Evaluate($request));
48 }
49
50 public function testSimpleExtendedEvaluate()
51 {
52 $map = array(
53 'one/two/three' => 'Invald',
54 'another/action' => 'Valid',
55 'four/five/six' => 'Invalid2'
56 );
57 $this->fixture->set_map($map);
58
59 $request = new http\Request('another/action/thing');
60 $this->assertEquals('Valid', $this->fixture->Evaluate($request));
61 }
62
63 public function testSimplePrecedenceEvaluate()
64 {
65 $map = array(
66 'some/action/first' => 'First',
67 'some/action' => 'Second',
68 'some/action/second' => 'Third'
69 );
70 $this->fixture->set_map($map);
71
72 $request = new http\Request('some/action/first');
73 $this->assertEquals('First', $this->fixture->Evaluate($request));
74
75 $request = new http\Request('some/action/second');
76 $this->assertEquals('Second', $this->fixture->Evaluate($request));
77
78 $request = new http\Request('some/action/third');
79 $this->assertEquals('Second', $this->fixture->Evaluate($request));
80 }
81
82 public function testEmptyRule()
83 {
84 $map = array(
85 'some/first' => 'First',
86 '' => 'Index',
87 'some/second' => 'Second'
88 );
89 $this->fixture->set_map($map);
90
91 $request = new http\Request('some/first');
92 $this->assertEquals('First', $this->fixture->Evaluate($request));
93
94 $request = new http\Request('');
95 $this->assertEquals('Index', $this->fixture->Evaluate($request));
96
97 $request = new http\Request('some/second');
98 $this->assertEquals('Second', $this->fixture->Evaluate($request));
99 }
100
101 public function testExtractSingle()
102 {
103 $map = array(
104 'action/one' => 'First',
105 'user/view/{id}' => 'Second',
106 'user/add' => 'Third'
107 );
108 $this->fixture->set_map($map);
109
110 $request = new http\Request('user/view/42');
111 $this->assertEquals('Second', $this->fixture->Evaluate($request));
112 $this->assertEquals('42', $request->data['id']);
113
114 $request = new http\Request('user/add');
115 $this->assertEquals('Third', $this->fixture->Evaluate($request));
116 }
117
118 public function testExtractDouble()
119 {
120 $map = array(
121 'action/unused' => 'Invalid',
122 'document/{action}/{key}' => 'DocumentController'
123 );
124 $this->fixture->set_map($map);
125
126 $request = new http\Request('document/edit/42');
127 $this->assertEquals('DocumentController', $this->fixture->Evaluate($request));
128 $this->assertEquals('edit', $request->data['action']);
129 $this->assertEquals('42', $request->data['key']);
130 }
131
132 public function testExactMatch()
133 {
134 $map = array(
135 'action/one' => 'First',
136 'action/two//' => 'Second',
137 'action/two/alpha' => 'Third'
138 );
139 $this->fixture->set_map($map);
140
141 $request = new http\Request('action/one');
142 $this->assertEquals('First', $this->fixture->Evaluate($request));
143
144 $request = new http\Request('action/two');
145 $this->assertEquals('Second', $this->fixture->Evaluate($request));
146
147 $request = new http\Request('action/two/alpha');
148 $this->assertEquals('Third', $this->fixture->Evaluate($request));
149 }
150
151 public function testRegEx()
152 {
153 $map = array(
154 'user/test' => 'First',
155 '/user\/([a-z]+)(\/([0-9]+))?/' => 'Second',
156 'user/TEST' => 'Third'
157 );
158 $this->fixture->set_map($map);
159
160 $request = new http\Request('user/test');
161 $this->assertEquals('First', $this->fixture->Evaluate($request));
162
163 $request = new http\Request('user/add');
164 $this->assertEquals('Second', $this->fixture->Evaluate($request));
165 $this->assertEquals('add', $request->data['url_pattern'][1]);
166 $this->assertTrue(!isset($request->data['url_pattern'][2]));
167
168 $request = new http\Request('user/view/14');
169 $this->assertEquals('Second', $this->fixture->Evaluate($request));
170 $this->assertEquals('view', $request->data['url_pattern'][1]);
171 $this->assertEquals('14', $request->data['url_pattern'][3]);
172
173 $request = new http\Request('user/TEST');
174 $this->assertEquals('Third', $this->fixture->Evaluate($request));
175 }
176
177 public function testLookupActionClass()
178 {
179 $self = $this;
180
181 $this->fixture->set_file_loader(function ($name, $value) use ($self) {
182 $self->assertEquals('\hoplite\test\TestAction', $name);
183 $self->assertEquals('\hoplite\test\TestAction', $value);
184 return 'hoplite\test\TestAction';
185 });
186 $test_class = '\hoplite\test\TestAction';
187 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture->LookupAction($test_class));
188
189 $this->fixture->set_file_loader(function ($name, $value) use ($self) {
190 $self->assertEquals('TestAction', $name);
191 $self->assertEquals('TestAction', $value);
192 return 'hoplite\test\TestAction';
193 });
194 $test_class = 'TestAction';
195 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture->LookupAction($test_class));
196 }
197
198 public function testLookupActionFile()
199 {
200 $self = $this;
201
202 $this->fixture->set_file_loader(function ($name, $value) use ($self) {
203 $self->assertEquals('TestAction', $name);
204 $self->assertEquals('actions/test_action.php', $value);
205 return 'hoplite\test\TestAction';
206 });
207 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture->LookupAction('actions/test_action'));
208
209 $this->fixture->set_file_loader(function ($name, $value) use ($self) {
210 $self->assertEquals('TestAction', $name);
211 $self->assertEquals('actions/test_action.php', $value);
212 return 'hoplite\test\TestAction';
213 });
214 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture->LookupAction('actions/test_action.php'));
215 }
216 }