3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite\test
;
18 use hoplite\http
as http
;
20 require_once HOPLITE_ROOT
. '/http/url_map.php';
22 class TestAction
extends http\Action
24 public function Invoke(http\Request
$request, http\Response
$response) {}
27 class UrlMapTest
extends \PHPUnit_Framework_TestCase
29 public function setUp()
32 $this->fixture
= new http\
UrlMap(new http\
RootController($globals));
35 public function testSimpleEvaluate()
38 'action/two' => 'Invalid',
39 'some/long/action' => 'Valid'
41 $this->fixture
->set_map($map);
43 $request = new http\
Request('some/long/action');
44 $this->assertEquals('Valid', $this->fixture
->Evaluate($request));
46 $request = new http\
Request('another/action');
47 $this->assertNull($this->fixture
->Evaluate($request));
50 public function testSimpleExtendedEvaluate()
53 'one/two/three' => 'Invald',
54 'another/action' => 'Valid',
55 'four/five/six' => 'Invalid2'
57 $this->fixture
->set_map($map);
59 $request = new http\
Request('another/action/thing');
60 $this->assertEquals('Valid', $this->fixture
->Evaluate($request));
63 public function testSimplePrecedenceEvaluate()
66 'some/action/first' => 'First',
67 'some/action' => 'Second',
68 'some/action/second' => 'Third'
70 $this->fixture
->set_map($map);
72 $request = new http\
Request('some/action/first');
73 $this->assertEquals('First', $this->fixture
->Evaluate($request));
75 $request = new http\
Request('some/action/second');
76 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
78 $request = new http\
Request('some/action/third');
79 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
82 public function testEmptyRule()
85 'some/first' => 'First',
87 'some/second' => 'Second'
89 $this->fixture
->set_map($map);
91 $request = new http\
Request('some/first');
92 $this->assertEquals('First', $this->fixture
->Evaluate($request));
94 $request = new http\
Request('');
95 $this->assertEquals('Index', $this->fixture
->Evaluate($request));
97 $request = new http\
Request('some/second');
98 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
101 public function testExtractSingle()
104 'action/one' => 'First',
105 'user/view/{id}' => 'Second',
106 'user/add' => 'Third'
108 $this->fixture
->set_map($map);
110 $request = new http\
Request('user/view/42');
111 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
112 $this->assertEquals('42', $request->data
['id']);
114 $request = new http\
Request('user/add');
115 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
118 public function testExtractDouble()
121 'action/unused' => 'Invalid',
122 'document/{action}/{key}' => 'DocumentController'
124 $this->fixture
->set_map($map);
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']);
132 public function testExactMatch()
135 'action/one' => 'First',
136 'action/two//' => 'Second',
137 'action/two/alpha' => 'Third'
139 $this->fixture
->set_map($map);
141 $request = new http\
Request('action/one');
142 $this->assertEquals('First', $this->fixture
->Evaluate($request));
144 $request = new http\
Request('action/two');
145 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
147 $request = new http\
Request('action/two/alpha');
148 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
151 public function testRegEx()
154 'user/test' => 'First',
155 '/user\/([a-z]+)(\/([0-9]+))?/' => 'Second',
156 'user/TEST' => 'Third'
158 $this->fixture
->set_map($map);
160 $request = new http\
Request('user/test');
161 $this->assertEquals('First', $this->fixture
->Evaluate($request));
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]));
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]);
173 $request = new http\
Request('user/TEST');
174 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
177 public function testLookupActionClass()
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';
186 $test_class = '\hoplite\test\TestAction';
187 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture
->LookupAction($test_class));
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';
194 $test_class = 'TestAction';
195 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture
->LookupAction($test_class));
198 public function testLookupActionFile()
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';
207 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture
->LookupAction('actions/test_action'));
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';
214 $this->assertInstanceOf('hoplite\test\TestAction', $this->fixture
->LookupAction('actions/test_action.php'));