* Write a unittest for RootControler
[hoplite.git] / testing / tests / http / root_controller_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
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.
8 //
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
12 // more details.
13 //
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/>.
16
17 namespace hoplite\test;
18 use hoplite\http as http;
19
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';
24
25 class ActionReporter extends http\Action
26 {
27 public $did_filter_request = FALSE;
28 public $did_invoke = FALSE;
29 public $did_filter_response = FALSE;
30
31 public function FilterRequest(http\Request $q, http\Response $s)
32 {
33 $this->did_filter_request = TRUE;
34 }
35
36 public function Invoke(http\Request $q, http\Response $s)
37 {
38 $this->did_invoke = TRUE;
39 }
40
41 public function FilterResponse(http\Request $q, http\Response $s)
42 {
43 $this->did_filter_response = TRUE;
44 }
45 }
46
47 class RootControllerTest extends \PHPUnit_Framework_TestCase
48 {
49 /*!
50 Configures a mock RootControler.
51 @param array|NULL Array of methods to mock
52 @param varargs Constructor parameters.
53 @return Mock RootControler
54 */
55 public function ConfigureMock()
56 {
57 $args = func_get_args();
58 return $this->getMock('hoplite\http\RootController', $args[0], array_slice($args, 1));
59 }
60
61 public function testRun()
62 {
63 $globals = array('_SERVER' => array(
64 'REQUEST_METHOD' => 'GET',
65 'PATH_INFO' => '/some/action/42'
66 ));
67 $mock = $this->ConfigureMock(array('RouteRequest', 'Stop'), $globals);
68
69 $mock->expects($this->once())
70 ->method('RouteRequest')
71 ->with($this->equalTo('some/action/42'));
72
73 $mock->expects($this->once())
74 ->method('Stop');
75
76 $mock->Run();
77 }
78
79 public function testInvokeAction()
80 {
81 $globals = array();
82 $fixture = new http\RootController($globals);
83
84 $action = new ActionReporter($fixture);
85
86 $this->assertFalse($action->did_filter_request);
87 $this->assertFalse($action->did_invoke);
88 $this->assertFalse($action->did_filter_response);
89
90 $fixture->InvokeAction($action);
91
92 $this->assertTrue($action->did_filter_request);
93 $this->assertTrue($action->did_invoke);
94 $this->assertTrue($action->did_filter_response);
95 }
96
97 public function testRouteRequest()
98 {
99 $globals = array();
100 $mock = $this->ConfigureMock(array('Stop', 'InvokeAction'), $globals);
101
102 $fragment = 'some/action/42';
103 $map_value = 'ActionReporter';
104 $action = new ActionReporter($mock);
105
106 $mock->expects($this->once())
107 ->method('InvokeAction')
108 ->with($this->isInstanceOf('hoplite\test\ActionReporter'));
109
110 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
111 $url_map->expects($this->once())
112 ->method('Evaluate')
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));
119
120 $mock->set_url_map($url_map);
121 $mock->RouteRequest($fragment);
122 }
123
124 public function testRouteRequestInvalid()
125 {
126 $globals = array();
127 $mock = $this->ConfigureMock(array('Stop'), $globals);
128
129 $fragment = 'another/action';
130
131 $mock->expects($this->once())
132 ->method('Stop');
133
134 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
135 $url_map->expects($this->once())
136 ->method('Evaluate')
137 ->with($this->equalTo($fragment));
138
139 $mock->set_url_map($url_map);
140 $mock->RouteRequest($fragment);
141 $this->assertEquals(http\ResponseCode::NOT_FOUND, $mock->response()->response_code);
142 }
143
144 public function testStop()
145 {
146 $globals = array();
147 $mock = $this->ConfigureMock(array('_Exit'), $globals);
148
149 $mock->expects($this->once())
150 ->method('_Exit');
151
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'));
157
158 $mock->set_output_filter($output_filter);
159 $mock->Stop();
160 }
161 }