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 UrlMapTest
extends \PHPUnit_Framework_TestCase
24 public function setUp()
27 $this->fixture
= new http\
UrlMap(new http\
RootController($globals));
30 public function testSimpleEvaluate()
33 'action/two' => 'Invalid',
34 'some/long/action' => 'Valid'
36 $this->fixture
->set_map($map);
38 $request = new http\
Request('some/long/action');
39 $this->assertEquals('Valid', $this->fixture
->Evaluate($request));
41 $request = new http\
Request('another/action');
42 $this->assertNull($this->fixture
->Evaluate($request));
45 public function testSimpleExtendedEvaluate()
48 'one/two/three' => 'Invald',
49 'another/action' => 'Valid',
50 'four/five/six' => 'Invalid2'
52 $this->fixture
->set_map($map);
54 $request = new http\
Request('another/action/thing');
55 $this->assertEquals('Valid', $this->fixture
->Evaluate($request));
58 public function testSimplePrecedenceEvaluate()
61 'some/action/first' => 'First',
62 'some/action' => 'Second',
63 'some/action/second' => 'Third'
65 $this->fixture
->set_map($map);
67 $request = new http\
Request('some/action/first');
68 $this->assertEquals('First', $this->fixture
->Evaluate($request));
70 $request = new http\
Request('some/action/second');
71 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
73 $request = new http\
Request('some/action/third');
74 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
77 public function testEmptyRule()
80 'some/first' => 'First',
82 'some/second' => 'Second'
84 $this->fixture
->set_map($map);
86 $request = new http\
Request('some/first');
87 $this->assertEquals('First', $this->fixture
->Evaluate($request));
89 $request = new http\
Request('');
90 $this->assertEquals('Index', $this->fixture
->Evaluate($request));
92 $request = new http\
Request('some/second');
93 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
96 public function testExtractSingle()
99 'action/one' => 'First',
100 'user/view/{id}' => 'Second',
101 'user/add' => 'Third'
103 $this->fixture
->set_map($map);
105 $request = new http\
Request('user/view/42');
106 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
107 $this->assertEquals('42', $request->data
['id']);
109 $request = new http\
Request('user/add');
110 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
113 public function testExtractDouble()
116 'action/unused' => 'Invalid',
117 'document/{action}/{key}' => 'DocumentController'
119 $this->fixture
->set_map($map);
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']);
127 public function testExactMatch()
130 'action/one' => 'First',
131 'action/two//' => 'Second',
132 'action/two/alpha' => 'Third'
134 $this->fixture
->set_map($map);
136 $request = new http\
Request('action/one');
137 $this->assertEquals('First', $this->fixture
->Evaluate($request));
139 $request = new http\
Request('action/two');
140 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
142 $request = new http\
Request('action/two/alpha');
143 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
146 public function testRegEx()
149 'user/test' => 'First',
150 '/user\/([a-z]+)(\/([0-9]+))?/' => 'Second',
151 'user/TEST' => 'Third'
153 $this->fixture
->set_map($map);
155 $request = new http\
Request('user/test');
156 $this->assertEquals('First', $this->fixture
->Evaluate($request));
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]));
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]);
168 $request = new http\
Request('user/TEST');
169 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
172 public function testLookupActionClass()
174 $test_class = '\hoplite\test\TestAction';
175 $this->assertEquals($test_class, $this->fixture
->LookupAction($test_class));
177 $test_class = 'TestAction';
178 $this->assertEquals($test_class, $this->fixture
->LookupAction($test_class));
181 public function testLookupActionFile()
183 $this->assertEquals('TestAction', $this->fixture
->LookupAction('actions/test_action'));
184 $this->assertEquals('TestAction', $this->fixture
->LookupAction('actions/test_action.php'));