-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModuleSpec.php
More file actions
270 lines (204 loc) · 12 KB
/
Copy pathModuleSpec.php
File metadata and controls
270 lines (204 loc) · 12 KB
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
<?php
namespace ErrorHeroModule\Spec;
use Pdo\Mysql;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDO\MySql\Driver;
use Doctrine\ORM\EntityManager;
use ErrorHeroModule\Module;
use Kahlan\Plugin\Double;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\EventManager\EventManagerInterface;
use Laminas\ModuleManager\Listener\ConfigListener;
use Laminas\ModuleManager\ModuleEvent;
use Laminas\ModuleManager\ModuleManager;
use Laminas\ServiceManager\ServiceManager;
use PDO;
describe('Module', function (): void {
given('module', fn() : Module => new Module());
given('config', fn() : array => [
'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [
[
'name' => 'db',
'options' => [
'db' => AdapterInterface::class,
'table' => 'error_log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data',
],
],
],
],
],
],
],
]);
describe('->getConfig()', function (): void {
it('return "config" array', function (): void {
$moduleConfig = include __DIR__.'/../config/module.config.php';
$actual = $this->module->getConfig();
expect($actual)->toBe($moduleConfig);
});
});
describe('->init()', function (): void {
it('receive ModuleManager that get Eventmanager that attach ModuleEvent::EVENT_LOAD_MODULES_POST', function (): void {
$moduleManager = Double::instance(['extends' => ModuleManager::class, 'methods' => '__construct']);
$eventManager = Double::instance(['implements' => EventManagerInterface::class]);
allow($moduleManager)->toReceive('getEventManager')->andReturn($eventManager);
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this->module, 'doctrineTransform']);
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_MERGE_CONFIG, [$this->module, 'errorPreviewPageHandler'], 101);
$this->module->init($moduleManager);
});
});
describe('->doctrineTransform()', function (): void {
it('does not has EntityManager service', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(false);
$this->module->doctrineTransform($moduleEvent);
expect($moduleEvent)->not->toReceive('getConfigListener');
});
it('has EntityManager service but already has db config', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
allow($serviceManager)->toReceive('get')->with('config')->andReturn([
'db' => [
'username' => 'root',
'password' => '',
'driver' => 'pdo_mysql',
'database' => 'mydb',
'host' => 'localhost',
],
]);
$this->module->doctrineTransform($moduleEvent);
expect($serviceManager)->not->toReceive('get')->with(EntityManager::class);
});
it('has EntityManager service but already does not has db config not isset driverOptions', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
allow($serviceManager)->toReceive('get')->with('config')->andReturn($this->config);
$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
$pdoMysqlInitCommandAttr = defined(Mysql::class . '::ATTR_INIT_COMMAND')
? Mysql::ATTR_INIT_COMMAND
: PDO::MYSQL_ATTR_INIT_COMMAND;
allow($connection)->toReceive('getParams')->andReturn([
'user' => 'mysqluser',
'password' => 'mysqlpassword',
'dbname' => 'mysqldbname',
'host' => 'mysqlhost',
'port' => '3306',
'driverOptions' => [
$pdoMysqlInitCommandAttr => "SET NAMES 'UTF8'",
],
'driverClass' => Driver::class,
]);
allow($connection)->toReceive('getUsername')->andReturn('root');
allow($connection)->toReceive('getPassword')->andReturn('');
allow($connection)->toReceive('getDriver')->andReturn($driver);
allow($connection)->toReceive('getDatabase')->andReturn('mydb');
allow($connection)->toReceive('getHost')->andReturn('localhost');
allow($connection)->toReceive('getPort')->andReturn('3306');
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);
expect($moduleEvent)->toReceive('getParam')->with('ServiceManager');
$this->module->doctrineTransform($moduleEvent);
});
it('has EntityManager service but already does not has db config with isset driverOptions', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
allow($serviceManager)->toReceive('get')->with('config')->andReturn($this->config);
$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
$pdoMysqlInitCommandAttr = defined(Mysql::class . '::ATTR_INIT_COMMAND')
? Mysql::ATTR_INIT_COMMAND
: PDO::MYSQL_ATTR_INIT_COMMAND;
allow($connection)->toReceive('getParams')->andReturn([
'user' => 'mysqluser',
'password' => 'mysqlpassword',
'dbname' => 'mysqldbname',
'host' => 'mysqlhost',
'port' => '3306',
'driverOptions' => [
$pdoMysqlInitCommandAttr => "SET NAMES 'UTF8'",
],
'driverClass' => Driver::class,
]);
allow($connection)->toReceive('getUsername')->andReturn('root');
allow($connection)->toReceive('getPassword')->andReturn('');
allow($connection)->toReceive('getDriver')->andReturn($driver);
allow($connection)->toReceive('getDatabase')->andReturn('mydb');
allow($connection)->toReceive('getHost')->andReturn('localhost');
allow($connection)->toReceive('getPort')->andReturn('3306');
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);
expect($moduleEvent)->toReceive('getParam')->with('ServiceManager');
$this->module->doctrineTransform($moduleEvent);
});
});
describe('->errorPreviewPageHandler()', function (): void {
it('does not has enable-error-preview-page', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
allow($configListener)->toReceive('getMergedConfig')->andReturn([
'error-hero-module' => [
'enable' => true,
],
]);
$this->module->errorPreviewPageHandler($moduleEvent);
expect($configListener)->not->toReceive('setMergedConfig');
});
it('has enable-error-preview-page and enabled', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
allow($configListener)->toReceive('getMergedConfig')->andReturn([
'error-hero-module' => [
'enable' => true,
'enable-error-preview-page' => true,
],
]);
$this->module->errorPreviewPageHandler($moduleEvent);
expect($configListener)->not->toReceive('setMergedConfig');
});
it('has enable-error-preview-page and disabled', function (): void {
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
$serviceManager = Double::instance(['extends' => ServiceManager::class]);
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
allow($configListener)->toReceive('getMergedConfig')->andReturn([
'error-hero-module' => [
'enable' => true,
'enable-error-preview-page' => false,
],
]);
$this->module->errorPreviewPageHandler($moduleEvent);
expect($configListener)->toReceive('setMergedConfig');
});
});
});