1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManagerTest; |
4
|
|
|
|
5
|
|
|
use AssetManager\Module; |
6
|
|
|
use AssetManager\Core\Resolver\ResolverInterface; |
7
|
|
|
use AssetManager\Core\Service\AssetManager; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
use Zend\Console\Response as ConsoleResponse; |
10
|
|
|
use Zend\EventManager\Event; |
11
|
|
|
use Zend\EventManager\EventManager; |
12
|
|
|
use Zend\EventManager\Test\EventListenerIntrospectionTrait; |
13
|
|
|
use Zend\Http\Request; |
14
|
|
|
use Zend\Http\Response; |
15
|
|
|
use Zend\Mvc\ApplicationInterface; |
16
|
|
|
use Zend\Mvc\MvcEvent; |
17
|
|
|
use Zend\Psr7Bridge\Psr7Response; |
18
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers \AssetManager\Module |
22
|
|
|
*/ |
23
|
|
|
class ModuleTest extends PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
use EventListenerIntrospectionTrait; |
26
|
|
|
|
27
|
|
|
public function testGetAutoloaderConfig() |
28
|
|
|
{ |
29
|
|
|
$module = new Module(); |
30
|
|
|
// just testing ZF specification requirements |
31
|
|
|
$this->assertInternalType('array', $module->getAutoloaderConfig()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetConfig() |
35
|
|
|
{ |
36
|
|
|
$module = new Module(); |
37
|
|
|
// just testing ZF specification requirements |
38
|
|
|
$this->assertInternalType('array', $module->getConfig()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Verifies that dispatch listener does nothing on other repsponse codes |
43
|
|
|
*/ |
44
|
|
|
public function testDispatchListenerIgnoresOtherResponseCodes() |
45
|
|
|
{ |
46
|
|
|
$event = new MvcEvent(); |
47
|
|
|
$response = new Response(); |
48
|
|
|
$module = new Module(); |
49
|
|
|
|
50
|
|
|
$response->setStatusCode(500); |
51
|
|
|
$event->setResponse($response); |
52
|
|
|
|
53
|
|
|
$response = $module->onDispatch($event); |
54
|
|
|
|
55
|
|
|
$this->assertNull($response); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testOnDispatchDoesntResolveToAsset() |
59
|
|
|
{ |
60
|
|
|
$resolver = $this->createMock(ResolverInterface::class); |
61
|
|
|
$assetManager = $this->getMockBuilder(AssetManager::class) |
62
|
|
|
->setMethods(['resolvesToAsset']) |
63
|
|
|
->setConstructorArgs([$resolver]) |
64
|
|
|
->getMock(); |
65
|
|
|
|
66
|
|
|
$assetManager |
67
|
|
|
->expects($this->once()) |
68
|
|
|
->method('resolvesToAsset') |
69
|
|
|
->will($this->returnValue(false)); |
70
|
|
|
|
71
|
|
|
$serviceManager = $this->createMock(ServiceLocatorInterface::class); |
72
|
|
|
$serviceManager |
73
|
|
|
->expects($this->any()) |
74
|
|
|
->method('get') |
75
|
|
|
->will($this->returnValue($assetManager)); |
76
|
|
|
|
77
|
|
|
$application = $this->createMock(ApplicationInterface::class); |
78
|
|
|
$application |
79
|
|
|
->expects($this->once()) |
80
|
|
|
->method('getServiceManager') |
81
|
|
|
->will($this->returnValue($serviceManager)); |
82
|
|
|
|
83
|
|
|
$event = new MvcEvent(); |
84
|
|
|
$response = new Response(); |
85
|
|
|
$request = new Request(); |
86
|
|
|
$module = new Module(); |
87
|
|
|
|
88
|
|
|
$event->setApplication($application); |
89
|
|
|
$response->setStatusCode(404); |
90
|
|
|
$event->setResponse($response); |
91
|
|
|
$event->setRequest($request); |
92
|
|
|
|
93
|
|
|
$return = $module->onDispatch($event); |
94
|
|
|
|
95
|
|
|
$this->assertNull($return); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testOnDispatchStatus200() |
99
|
|
|
{ |
100
|
|
|
$resolver = $this->createMock(ResolverInterface::class); |
101
|
|
|
$assetManager = $this->getMockBuilder(AssetManager::class) |
102
|
|
|
->setMethods(['resolvesToAsset', 'setAssetOnResponse']) |
103
|
|
|
->setConstructorArgs([$resolver]) |
104
|
|
|
->getMock(); |
105
|
|
|
|
106
|
|
|
$assetManager |
107
|
|
|
->expects($this->once()) |
108
|
|
|
->method('resolvesToAsset') |
109
|
|
|
->will($this->returnValue(true)); |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
$zfResponse = new Response(); |
113
|
|
|
$zfResponse->setContent('bacon'); |
114
|
|
|
|
115
|
|
|
$amResponse = Psr7Response::fromZend($zfResponse); |
116
|
|
|
|
117
|
|
|
$assetManager |
118
|
|
|
->expects($this->once()) |
119
|
|
|
->method('setAssetOnResponse') |
120
|
|
|
->will($this->returnValue($amResponse)); |
121
|
|
|
|
122
|
|
|
$serviceManager = $this->createMock(ServiceLocatorInterface::class); |
123
|
|
|
$serviceManager |
124
|
|
|
->expects($this->any()) |
125
|
|
|
->method('get') |
126
|
|
|
->will($this->returnValue($assetManager)); |
127
|
|
|
|
128
|
|
|
$application = $this->createMock(ApplicationInterface::class); |
129
|
|
|
$application |
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('getServiceManager') |
132
|
|
|
->will($this->returnValue($serviceManager)); |
133
|
|
|
|
134
|
|
|
$event = new MvcEvent(); |
135
|
|
|
$response = new Response(); |
136
|
|
|
$request = new Request(); |
137
|
|
|
$module = new Module(); |
138
|
|
|
|
139
|
|
|
$event->setApplication($application); |
140
|
|
|
$response->setStatusCode(404); |
141
|
|
|
$event->setResponse($response); |
142
|
|
|
$event->setRequest($request); |
143
|
|
|
|
144
|
|
|
$return = $module->onDispatch($event); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(200, $return->getStatusCode()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers \AssetManager\Module::onDispatch |
151
|
|
|
*/ |
152
|
|
|
public function testWillIgnoreInvalidResponseType() |
153
|
|
|
{ |
154
|
|
|
$cliResponse = $this->getMockBuilder(ConsoleResponse::class) |
155
|
|
|
->disableOriginalConstructor() |
156
|
|
|
->getMock(); |
157
|
|
|
|
158
|
|
|
$mvcEvent = $this->createMock(MvcEvent::class); |
159
|
|
|
$module = new Module(); |
160
|
|
|
|
161
|
|
|
$mvcEvent->expects($this->once())->method('getResponse')->will($this->returnValue($cliResponse)); |
162
|
|
|
|
163
|
|
|
$this->assertNull($module->onDispatch($mvcEvent)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testOnBootstrap() |
167
|
|
|
{ |
168
|
|
|
$applicationEventManager = new EventManager(); |
169
|
|
|
|
170
|
|
|
$application = $this->createMock(ApplicationInterface::class); |
171
|
|
|
$application |
172
|
|
|
->expects($this->any()) |
173
|
|
|
->method('getEventManager') |
174
|
|
|
->will($this->returnValue($applicationEventManager)); |
175
|
|
|
|
176
|
|
|
$event = new Event(); |
177
|
|
|
$event->setTarget($application); |
178
|
|
|
|
179
|
|
|
$module = new Module(); |
180
|
|
|
$module->onBootstrap($event); |
181
|
|
|
|
182
|
|
|
$this->assertListenerAtPriority( |
183
|
|
|
[$module, 'onDispatch'], |
184
|
|
|
-9999999, |
185
|
|
|
MvcEvent::EVENT_DISPATCH, |
186
|
|
|
$applicationEventManager |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$this->assertListenerAtPriority( |
190
|
|
|
[$module, 'onDispatch'], |
191
|
|
|
-9999999, |
192
|
|
|
MvcEvent::EVENT_DISPATCH_ERROR, |
193
|
|
|
$applicationEventManager |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|