]> src.bluestatic.org Git - hoplite.git/blob - testing/tests/http/root_controller_test.php
Fix more bugs in TemplateLoader and a singleton helper
[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->request()->url = 'some/action/42';
70
71 $mock->expects($this->once())
72 ->method('RouteRequest')
73 ->with($this->equalTo($mock->request()));
74
75 $mock->expects($this->once())
76 ->method('Stop');
77
78 $mock->Run();
79 }
80
81 public function testInvokeAction()
82 {
83 $globals = array();
84 $fixture = new http\RootController($globals);
85
86 $action = new ActionReporter($fixture);
87
88 $this->assertFalse($action->did_filter_request);
89 $this->assertFalse($action->did_invoke);
90 $this->assertFalse($action->did_filter_response);
91
92 $fixture->InvokeAction($action);
93
94 $this->assertTrue($action->did_filter_request);
95 $this->assertTrue($action->did_invoke);
96 $this->assertTrue($action->did_filter_response);
97 }
98
99 public function testRouteRequest()
100 {
101 $globals = array();
102 $mock = $this->ConfigureMock(array('Stop', 'InvokeAction'), $globals);
103
104 $mock->request()->url = 'some/action/42';
105 $map_value = 'ActionReporter';
106 $action = new ActionReporter($mock);
107
108 $mock->expects($this->once())
109 ->method('InvokeAction')
110 ->with($this->isInstanceOf('hoplite\test\ActionReporter'));
111
112 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
113 $url_map->expects($this->once())
114 ->method('Evaluate')
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));
121
122 $mock->set_url_map($url_map);
123 $mock->RouteRequest($mock->request());
124 }
125
126 public function testRouteRequestInvalid()
127 {
128 $globals = array();
129 $mock = $this->ConfigureMock(array('Stop'), $globals);
130
131 $mock->request()->url = 'another/action';
132
133 $mock->expects($this->once())
134 ->method('Stop');
135
136 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
137 $url_map->expects($this->once())
138 ->method('Evaluate')
139 ->with($this->equalTo($mock->request()));
140
141 $mock->set_url_map($url_map);
142 $mock->RouteRequest($mock->request());
143 $this->assertEquals(http\ResponseCode::NOT_FOUND, $mock->response()->response_code);
144 }
145
146 public function testStop()
147 {
148 $globals = array();
149 $mock = $this->ConfigureMock(array('_Exit'), $globals);
150
151 $mock->expects($this->once())
152 ->method('_Exit');
153
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'));
159
160 $mock->set_output_filter($output_filter);
161 $mock->Stop();
162 }
163 }