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 testExtractSingle()
80 'action/one' => 'First',
81 'user/view/{id}' => 'Second',
84 $this->fixture
->set_map($map);
86 $request = new http\
Request('user/view/42');
87 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
88 $this->assertEquals('42', $request->data
['id']);
90 $request = new http\
Request('user/add');
91 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
94 public function testExtractDouble()
97 'action/unused' => 'Invalid',
98 'document/{action}/{key}' => 'DocumentController'
100 $this->fixture
->set_map($map);
102 $request = new http\
Request('document/edit/42');
103 $this->assertEquals('DocumentController', $this->fixture
->Evaluate($request));
104 $this->assertEquals('edit', $request->data
['action']);
105 $this->assertEquals('42', $request->data
['key']);
108 public function testExactMatch()
111 'action/one' => 'First',
112 'action/two//' => 'Second',
113 'action/two/alpha' => 'Third'
115 $this->fixture
->set_map($map);
117 $request = new http\
Request('action/one');
118 $this->assertEquals('First', $this->fixture
->Evaluate($request));
120 $request = new http\
Request('action/two');
121 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
123 $request = new http\
Request('action/two/alpha');
124 $this->assertEquals('Third', $this->fixture
->Evaluate($request));
127 public function testRegEx()
130 'user/test' => 'First',
131 '/user\/([a-z]+)(\/([0-9]+))?/' => 'Second',
132 'user/TEST' => 'Third'
134 $this->fixture
->set_map($map);
136 $request = new http\
Request('user/test');
137 $this->assertEquals('First', $this->fixture
->Evaluate($request));
139 $request = new http\
Request('user/add');
140 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
141 $this->assertEquals('add', $request->data
['url_pattern'][1]);
142 $this->assertTrue(!isset($request->data
['url_pattern'][2]));
144 $request = new http\
Request('user/view/14');
145 $this->assertEquals('Second', $this->fixture
->Evaluate($request));
146 $this->assertEquals('view', $request->data
['url_pattern'][1]);
147 $this->assertEquals('14', $request->data
['url_pattern'][3]);
149 $request = new http\
Request('user/TEST');
150 $this->assertEquals('Third', $this->fixture
->Evaluate($request));