This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
AbstractControllerTestCaseTest.php
323 lines (268 loc) · 10.1 KB
/
AbstractControllerTestCaseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Test
*/
namespace ZendTest\Test\PHPUnit\Controller;
use Zend\Console\Console;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
/**
* @category Zend
* @package Zend_Test
* @subpackage UnitTests
* @group Zend_Test
*/
class AbstractControllerTestCaseTest extends AbstractHttpControllerTestCase
{
public function tearDownCacheDir()
{
$cacheDir = sys_get_temp_dir() . '/zf2-module-test';
if (is_dir($cacheDir)) {
static::rmdir($cacheDir);
}
}
public static function rmdir($dir)
{
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? static::rmdir("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
protected function setUp()
{
$this->tearDownCacheDir();
Console::overrideIsConsole(null);
$this->setApplicationConfig(
include __DIR__ . '/../../_files/application.config.php'
);
parent::setUp();
}
protected function tearDown()
{
$this->tearDownCacheDir();
parent::tearDown();
}
public function testModuleCacheIsDisabled()
{
$config = $this->getApplicationConfig();
$config = $config['module_listener_options']['cache_dir'];
$this->assertEquals(0, count(glob($config . '/*.php')));
}
public function testCanNotDefineApplicationConfigWhenApplicationIsBuilt()
{
// cosntruct app
$this->getApplication();
$this->setExpectedException('Zend\Stdlib\Exception\LogicException');
$this->setApplicationConfig(
include __DIR__ . '/../../_files/application.config.php'
);
}
public function testUseOfRouter()
{
// default value
$this->assertEquals(false, $this->useConsoleRequest);
}
public function testApplicationClass()
{
$applicationClass = get_class($this->getApplication());
$this->assertEquals($applicationClass, 'Zend\Mvc\Application');
}
public function testApplicationClassAndTestRestoredConsoleFlag()
{
$this->assertTrue(Console::isConsole(), '1. Console::isConsole returned false in initial test');
$this->getApplication();
$this->assertFalse(Console::isConsole(), '2. Console::isConsole returned true after retrieving application');
$this->tearDown();
$this->assertTrue(Console::isConsole(), '3. Console::isConsole returned false after tearDown');
Console::overrideIsConsole(false);
parent::setUp();
$this->assertFalse(Console::isConsole(), '4. Console::isConsole returned true after parent::setUp');
$this->getApplication();
$this->assertFalse(Console::isConsole(), '5. Console::isConsole returned true after retrieving application');
parent::tearDown();
$this->assertFalse(Console::isConsole(), '6. Console.isConsole returned true after parent::tearDown');
}
public function testApplicationServiceLocatorClass()
{
$smClass = get_class($this->getApplicationServiceLocator());
$this->assertEquals($smClass, 'Zend\ServiceManager\ServiceManager');
}
public function testAssertApplicationRequest()
{
$this->assertEquals(true, $this->getRequest() instanceof RequestInterface);
}
public function testAssertApplicationResponse()
{
$this->assertEquals(true, $this->getResponse() instanceof ResponseInterface);
}
public function testAssertModuleName()
{
$this->dispatch('/tests');
// tests with case insensitive
$this->assertModuleName('baz');
$this->assertModuleName('Baz');
$this->assertModuleName('BAz');
$this->setExpectedException(
'PHPUnit_Framework_ExpectationFailedException',
'actual module name is "baz"' // check actual module is display
);
$this->assertModuleName('Application');
}
public function testAssertNotModuleName()
{
$this->dispatch('/tests');
$this->assertNotModuleName('Application');
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotModuleName('baz');
}
public function testAssertControllerClass()
{
$this->dispatch('/tests');
// tests with case insensitive
$this->assertControllerClass('IndexController');
$this->assertControllerClass('Indexcontroller');
$this->assertControllerClass('indexcontroller');
$this->setExpectedException(
'PHPUnit_Framework_ExpectationFailedException',
'actual controller class is "indexcontroller"' // check actual controller class is display
);
$this->assertControllerClass('Index');
}
public function testAssertNotControllerClass()
{
$this->dispatch('/tests');
$this->assertNotControllerClass('Index');
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotControllerClass('IndexController');
}
public function testAssertControllerName()
{
$this->dispatch('/tests');
// tests with case insensitive
$this->assertControllerName('baz_index');
$this->assertControllerName('Baz_index');
$this->assertControllerName('BAz_index');
$this->setExpectedException(
'PHPUnit_Framework_ExpectationFailedException',
'actual controller name is "baz_index"' // check actual controller name is display
);
$this->assertControllerName('baz');
}
public function testAssertNotControllerName()
{
$this->dispatch('/tests');
$this->assertNotControllerName('baz');
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotControllerName('baz_index');
}
public function testAssertActionName()
{
$this->dispatch('/tests');
// tests with case insensitive
$this->assertActionName('unittests');
$this->assertActionName('unitTests');
$this->assertActionName('UnitTests');
$this->setExpectedException(
'PHPUnit_Framework_ExpectationFailedException',
'actual action name is "unittests"' // check actual action name is display
);
$this->assertActionName('unit');
}
public function testAssertNotActionName()
{
$this->dispatch('/tests');
$this->assertNotActionName('unit');
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotActionName('unittests');
}
public function testAssertMatchedRouteName()
{
$this->dispatch('/tests');
// tests with case insensitive
$this->assertMatchedRouteName('myroute');
$this->assertMatchedRouteName('myRoute');
$this->assertMatchedRouteName('MyRoute');
$this->setExpectedException(
'PHPUnit_Framework_ExpectationFailedException',
'actual matched route name is "myroute"' // check actual matched route name is display
);
$this->assertMatchedRouteName('route');
}
public function testAssertNotMatchedRouteName()
{
$this->dispatch('/tests');
$this->assertNotMatchedRouteName('route');
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotMatchedRouteName('myroute');
}
/**
* Sample tests on Application errors events
*/
public function testAssertApplicationErrorsEvents()
{
$this->url('/bad-url');
$result = $this->triggerApplicationEvent(MvcEvent::EVENT_ROUTE);
$this->assertEquals(true, $result->stopped());
$this->assertEquals(Application::ERROR_ROUTER_NO_MATCH, $this->getApplication()->getMvcEvent()->getError());
}
public function testDispatchRequestUri()
{
$this->dispatch('/tests');
$this->assertEquals('/tests', $this->getApplication()->getRequest()->getRequestUri());
}
public function testDefaultDispatchMethod()
{
$this->dispatch('/tests');
$this->assertEquals('GET', $this->getRequest()->getMethod());
}
public function testDispatchMethodSetOnRequest()
{
$this->getRequest()->setMethod('POST');
$this->dispatch('/tests');
$this->assertEquals('POST', $this->getRequest()->getMethod());
}
public function testExplicitDispatchMethodOverrideRequestMethod()
{
$this->getRequest()->setMethod('POST');
$this->dispatch('/tests', 'GET');
$this->assertEquals('GET', $this->getRequest()->getMethod());
}
public function testPutRequestParams()
{
$this->dispatch('/tests', 'PUT', array('a' => 1));
$this->assertEquals('a=1', $this->getRequest()->getContent());
}
public function testPreserveContentOfPutRequest()
{
$this->getRequest()->setMethod('PUT');
$this->getRequest()->setContent('my content');
$this->dispatch('/tests');
$this->assertEquals('my content', $this->getRequest()->getContent());
}
public function testExplicityPutParamsOverrideRequestContent()
{
$this->getRequest()->setContent('my content');
$this->dispatch('/tests', 'PUT', array('a' => 1));
$this->assertEquals('a=1', $this->getRequest()->getContent());
}
public function testAssertTemplateName()
{
$this->dispatch('/tests');
$this->assertTemplateName('layout/layout');
$this->assertTemplateName('baz/index/unittests');
}
public function testAssertNotTemplateName()
{
$this->dispatch('/tests');
$this->assertNotTemplateName('template/does/not/exist');
}
}