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);