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->expects($this->once())
70 ->method('RouteRequest')
71 ->with($this->equalTo('some/action/42'));
73 $mock->expects($this->once())
79 public function testInvokeAction()
82 $fixture = new http\
RootController($globals);
84 $action = new ActionReporter($fixture);
86 $this->assertFalse($action->did_filter_request
);
87 $this->assertFalse($action->did_invoke
);
88 $this->assertFalse($action->did_filter_response
);
90 $fixture->InvokeAction($action);
92 $this->assertTrue($action->did_filter_request
);
93 $this->assertTrue($action->did_invoke
);
94 $this->assertTrue($action->did_filter_response
);
97 public function testRouteRequest()
100 $mock = $this->ConfigureMock(array('Stop', 'InvokeAction'), $globals);
102 $fragment = 'some/action/42';
103 $map_value = 'ActionReporter';
104 $action = new ActionReporter($mock);
106 $mock->expects($this->once())
107 ->method('InvokeAction')
108 ->with($this->isInstanceOf('hoplite\test\ActionReporter'));
110 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
111 $url_map->expects($this->once())
113 ->with($this->equalTo($fragment))
114 ->will($this->returnValue($map_value));
115 $url_map->expects($this->once())
116 ->method('LookupAction')
117 ->with($this->equalTo($map_value))
118 ->will($this->returnValue($action));
120 $mock->set_url_map($url_map);
121 $mock->RouteRequest($fragment);
124 public function testRouteRequestInvalid()
127 $mock = $this->ConfigureMock(array('Stop'), $globals);
129 $fragment = 'another/action';
131 $mock->expects($this->once())
134 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
135 $url_map->expects($this->once())
137 ->with($this->equalTo($fragment));
139 $mock->set_url_map($url_map);
140 $mock->RouteRequest($fragment);
141 $this->assertEquals(http\ResponseCode
::NOT_FOUND
, $mock->response()->response_code
);
144 public function testStop()
147 $mock = $this->ConfigureMock(array('_Exit'), $globals);
149 $mock->expects($this->once())
152 $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock));
153 $output_filter->expects($this->once())
154 ->method('FilterOutput')
155 ->with($this->isInstanceOf('hoplite\http\Request'),
156 $this->isInstanceOf('hoplite\http\Response'));
158 $mock->set_output_filter($output_filter);