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/action.php';
21 require_once HOPLITE_ROOT
. '/http/output_filter.php';
22 require_once HOPLITE_ROOT
. '/http/root_controller.php';
23 require_once HOPLITE_ROOT
. '/http/url_map.php';
25 class ActionReporter
extends http\Action
27 public $did_filter_request = FALSE
;
28 public $did_invoke = FALSE
;
29 public $did_filter_response = FALSE
;
31 public function FilterRequest(http\Request
$q, http\Response
$s)
33 $this->did_filter_request
= TRUE
;
36 public function Invoke(http\Request
$q, http\Response
$s)
38 $this->did_invoke
= TRUE
;
41 public function FilterResponse(http\Request
$q, http\Response
$s)
43 $this->did_filter_response
= TRUE
;
47 class RootControllerTest
extends \PHPUnit_Framework_TestCase
50 Configures a mock RootControler.
51 @param array|NULL Array of methods to mock
52 @param varargs Constructor parameters.
53 @return Mock RootControler
55 public function ConfigureMock()
57 $args = func_get_args();
58 return $this->getMock('hoplite\http\RootController', $args[0], array_slice($args, 1));
61 public function testRun()
63 $globals = array('_SERVER' => array(
64 'REQUEST_METHOD' => 'GET',
65 'PATH_INFO' => '/some/action/42'
67 $mock = $this->ConfigureMock(array('RouteRequest', 'Stop'), $globals);
69 $mock->request()->url
= 'some/action/42';
71 $mock->expects($this->once())
72 ->method('RouteRequest')
73 ->with($this->equalTo($mock->request()));
75 $mock->expects($this->once())
81 public function testInvokeAction()
84 $fixture = new http\
RootController($globals);
86 $action = new ActionReporter($fixture);
88 $this->assertFalse($action->did_filter_request
);
89 $this->assertFalse($action->did_invoke
);
90 $this->assertFalse($action->did_filter_response
);
92 $fixture->InvokeAction($action);
94 $this->assertTrue($action->did_filter_request
);
95 $this->assertTrue($action->did_invoke
);
96 $this->assertTrue($action->did_filter_response
);
99 public function testRouteRequest()
102 $mock = $this->ConfigureMock(array('Stop', 'InvokeAction'), $globals);
104 $mock->request()->url
= 'some/action/42';
105 $map_value = 'ActionReporter';
106 $action = new ActionReporter($mock);
108 $mock->expects($this->once())
109 ->method('InvokeAction')
110 ->with($this->isInstanceOf('hoplite\test\ActionReporter'));
112 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
113 $url_map->expects($this->once())
115 ->with($this->equalTo($mock->request()))
116 ->will($this->returnValue($map_value));
117 $url_map->expects($this->once())
118 ->method('LookupAction')
119 ->with($this->equalTo($map_value))
120 ->will($this->returnValue($action));
122 $mock->set_url_map($url_map);
123 $mock->RouteRequest($mock->request());
126 public function testRouteRequestInvalid()
129 $mock = $this->ConfigureMock(array('Stop'), $globals);
131 $mock->request()->url
= 'another/action';
133 $mock->expects($this->once())
136 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
137 $url_map->expects($this->once())
139 ->with($this->equalTo($mock->request()));
141 $mock->set_url_map($url_map);
142 $mock->RouteRequest($mock->request());
143 $this->assertEquals(http\ResponseCode
::NOT_FOUND
, $mock->response()->response_code
);
146 public function testStop()
149 $mock = $this->ConfigureMock(array('_Exit'), $globals);
151 $mock->expects($this->once())
154 $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock));
155 $output_filter->expects($this->once())
156 ->method('FilterOutput')
157 ->with($this->isInstanceOf('hoplite\http\Request'),
158 $this->isInstanceOf('hoplite\http\Response'));
160 $mock->set_output_filter($output_filter);
164 public function testStopWithRedirect()
167 $mock = $this->ConfigureMock(array('_Exit'), $globals);
168 $mock->expects($this->once())
171 $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock));
172 $output_filter->expects($this->once())
173 ->method('FilterOutput')
174 ->with($this->isInstanceOf('hoplite\http\Request'),
175 $this->isInstanceOf('hoplite\http\Response'));
177 $mock->set_output_filter($output_filter);
179 $mock->StopWithRedirect('/foo/bar');
181 $this->assertEquals('/foo/bar', $mock->response()->headers
['Location']);
182 $this->assertEquals(http\ResponseCode
::FOUND
, $mock->response()->response_code
);
185 public function testAbsolutify()
189 'HTTP_HOST' => 'www.bluestatic.org',
190 'REQUEST_URI' => '/hoplite/webapp/test/path',
191 'PATH_INFO' => '/test/path',
195 $mock = new \hoplite\http\
RootController($globals);
197 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
198 $this->assertEquals($mock->MakeURL('/', TRUE
), 'http://www.bluestatic.org/hoplite/webapp/');
200 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
201 $this->assertEquals($mock->MakeURL('/path/3', TRUE
), 'http://www.bluestatic.org/hoplite/webapp/path/3');
203 $globals['_SERVER']['HTTPS'] = 'on';
204 $globals['_SERVER']['SERVER_PORT'] = 443;
205 $mock = new \hoplite\http\
RootController($globals);
206 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
207 $this->assertEquals($mock->MakeURL('/', TRUE
), 'https://www.bluestatic.org/hoplite/webapp/');
209 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
210 $this->assertEquals($mock->MakeURL('/path/3', TRUE
), 'https://www.bluestatic.org/hoplite/webapp/path/3');
212 $globals['_SERVER']['SERVER_PORT'] = 8080;
213 $mock = new \hoplite\http\
RootController($globals);
214 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
215 $this->assertEquals($mock->MakeURL('/', TRUE
), 'https://www.bluestatic.org:8080/hoplite/webapp/');
216 $this->assertEquals($mock->MakeURL('/path/3', TRUE
), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3');
219 public function testAbsolutifyRoot()
223 'HTTP_HOST' => 'www.bluestatic.org',
224 'REQUEST_URI' => '/hoplite/webapp/',
229 $mock = new \hoplite\http\
RootController($globals);
231 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
232 $this->assertEquals($mock->MakeURL('/', TRUE
), 'http://www.bluestatic.org/hoplite/webapp/');
234 $globals['_SERVER']['HTTPS'] = 'on';
235 $globals['_SERVER']['SERVER_PORT'] = 443;
236 $mock = new \hoplite\http\
RootController($globals);
237 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
238 $this->assertEquals($mock->MakeURL('/', TRUE
), 'https://www.bluestatic.org/hoplite/webapp/');
240 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
241 $this->assertEquals($mock->MakeURL('/path/3', TRUE
), 'https://www.bluestatic.org/hoplite/webapp/path/3');
243 $globals['_SERVER']['SERVER_PORT'] = 8080;
244 $mock = new \hoplite\http\
RootController($globals);
245 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
246 $this->assertEquals($mock->MakeURL('/', TRUE
), 'https://www.bluestatic.org:8080/hoplite/webapp/');
247 $this->assertEquals($mock->MakeURL('/path/3', TRUE
), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3');