1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PHPChunkit; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @testClass PHPChunkit\Test\ConfigurationTest |
12
|
|
|
*/ |
13
|
|
|
class Configuration |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $rootDir = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $watchDirectories = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $testsDirectory = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $bootstrapPath = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $phpunitPath = 'vendor/bin/phpunit'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var null|EventDispatcher |
42
|
|
|
*/ |
43
|
|
|
private $eventDispatcher; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var null|DatabaseSandbox |
47
|
|
|
*/ |
48
|
|
|
private $databaseSandbox; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $memoryLimit = '256M'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
private $numChunks = 1; |
59
|
|
|
|
60
|
2 |
|
public static function createFromXmlFile(string $path) : self |
61
|
|
|
{ |
62
|
2 |
|
if (!file_exists($path)) { |
63
|
1 |
|
throw new \InvalidArgumentException(sprintf('XML file count not be found at path "%s"', $path)); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$configuration = new self(); |
67
|
|
|
|
68
|
1 |
|
$xml = simplexml_load_file($path); |
69
|
1 |
|
$attributes = $xml->attributes(); |
70
|
|
|
|
71
|
|
|
$xmlMappings = [ |
72
|
|
|
'root-dir' => [ |
73
|
|
|
'type' => 'string', |
74
|
|
|
'setter' => 'setRootDir' |
75
|
1 |
|
], |
76
|
|
|
'bootstrap' => [ |
77
|
|
|
'type' => 'string', |
78
|
|
|
'setter' => 'setBootstrapPath' |
79
|
|
|
], |
80
|
|
|
'tests-dir' => [ |
81
|
|
|
'type' => 'string', |
82
|
|
|
'setter' => 'setTestsDirectory' |
83
|
|
|
], |
84
|
|
|
'phpunit-path' => [ |
85
|
|
|
'type' => 'string', |
86
|
|
|
'setter' => 'setPhpunitPath' |
87
|
|
|
], |
88
|
|
|
'memory-limit' => [ |
89
|
|
|
'type' => 'string', |
90
|
|
|
'setter' => 'setMemoryLimit' |
91
|
|
|
], |
92
|
|
|
'num-chunks' => [ |
93
|
|
|
'type' => 'integer', |
94
|
|
|
'setter' => 'setNumChunks' |
95
|
|
|
], |
96
|
|
|
'watch-directories' => [ |
97
|
|
|
'type' => 'array', |
98
|
|
|
'setter' => 'setWatchDirectories', |
99
|
|
|
'xmlName' => 'watch-directory', |
100
|
|
|
], |
101
|
|
|
'database-names' => [ |
102
|
|
|
'type' => 'array', |
103
|
|
|
'setter' => 'setDatabaseNames', |
104
|
|
|
'xmlName' => 'database-name', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
1 |
|
foreach ($xmlMappings as $name => $mapping) { |
109
|
1 |
|
$value = null; |
110
|
|
|
|
111
|
1 |
|
if ($mapping['type'] === 'array') { |
112
|
1 |
|
$value = (array) $xml->{$name}->{$mapping['xmlName']}; |
113
|
1 |
|
} elseif (isset($attributes[$name])) { |
114
|
1 |
|
$value = $attributes[$name]; |
115
|
|
|
|
116
|
1 |
|
settype($value, $mapping['type']); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
if ($value !== null) { |
120
|
1 |
|
$configuration->{$mapping['setter']}($value); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$events = (array) $xml->{'events'}; |
125
|
1 |
|
$listeners = $events['listener'] ?? null; |
126
|
|
|
|
127
|
1 |
|
if ($listeners) { |
128
|
1 |
|
foreach ($listeners as $listener) { |
129
|
1 |
|
$configuration->addListener( |
130
|
1 |
|
(string) $listener->attributes()['event'], |
131
|
1 |
|
(string) $listener->class |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return $configuration; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function addListener( |
140
|
|
|
string $eventName, |
141
|
|
|
string $className, |
142
|
|
|
int $priority = 0) : ListenerInterface |
143
|
|
|
{ |
144
|
1 |
|
$listener = new $className($this); |
145
|
|
|
|
146
|
1 |
|
if (!$listener instanceof ListenerInterface) { |
147
|
|
|
throw new InvalidArgumentException( |
148
|
|
|
sprintf('%s does not implement %s', $className, ListenerInterface::class) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
$this->getEventDispatcher()->addListener( |
153
|
1 |
|
$eventName, [$listener, 'execute'], $priority |
154
|
|
|
); |
155
|
|
|
|
156
|
1 |
|
return $listener; |
157
|
|
|
} |
158
|
|
|
|
159
|
9 |
View Code Duplication |
public function setRootDir(string $rootDir) : self |
160
|
|
|
{ |
161
|
9 |
|
if (!is_dir($rootDir)) { |
162
|
1 |
|
throw new \InvalidArgumentException( |
163
|
1 |
|
sprintf('Root directory "%s" does not exist.', $rootDir) |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
8 |
|
$this->rootDir = realpath($rootDir); |
168
|
|
|
|
169
|
8 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
public function getRootDir() : string |
173
|
|
|
{ |
174
|
3 |
|
return $this->rootDir; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
public function setWatchDirectories(array $watchDirectories) : self |
178
|
|
|
{ |
179
|
4 |
|
foreach ($watchDirectories as $key => $watchDirectory) { |
180
|
4 |
|
if (!is_dir($watchDirectory)) { |
181
|
1 |
|
throw new \InvalidArgumentException( |
182
|
1 |
|
sprintf('Watch directory "%s" does not exist.', $watchDirectory) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
$watchDirectories[$key] = realpath($watchDirectory); |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
$this->watchDirectories = $watchDirectories; |
190
|
|
|
|
191
|
3 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
3 |
|
public function getWatchDirectories() : array |
195
|
|
|
{ |
196
|
3 |
|
return $this->watchDirectories; |
197
|
|
|
} |
198
|
|
|
|
199
|
4 |
View Code Duplication |
public function setTestsDirectory(string $testsDirectory) : self |
200
|
|
|
{ |
201
|
4 |
|
if (!is_dir($testsDirectory)) { |
202
|
1 |
|
throw new \InvalidArgumentException( |
203
|
1 |
|
sprintf('Tests directory "%s" does not exist.', $testsDirectory) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
3 |
|
$this->testsDirectory = realpath($testsDirectory); |
208
|
|
|
|
209
|
3 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
public function getTestsDirectory() : string |
213
|
|
|
{ |
214
|
2 |
|
return $this->testsDirectory; |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
View Code Duplication |
public function setBootstrapPath(string $bootstrapPath) : self |
218
|
|
|
{ |
219
|
3 |
|
if (!file_exists($bootstrapPath)) { |
220
|
1 |
|
throw new \InvalidArgumentException( |
221
|
1 |
|
sprintf('Bootstrap path "%s" does not exist.', $bootstrapPath) |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
$this->bootstrapPath = realpath($bootstrapPath); |
226
|
|
|
|
227
|
2 |
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
public function getBootstrapPath() : string |
231
|
|
|
{ |
232
|
2 |
|
return $this->bootstrapPath; |
233
|
|
|
} |
234
|
|
|
|
235
|
9 |
View Code Duplication |
public function setPhpunitPath(string $phpunitPath) : self |
236
|
|
|
{ |
237
|
9 |
|
if (!file_exists($phpunitPath)) { |
238
|
1 |
|
throw new \InvalidArgumentException( |
239
|
1 |
|
sprintf('PHPUnit path "%s" does not exist.', $phpunitPath) |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
8 |
|
$this->phpunitPath = realpath($phpunitPath); |
244
|
|
|
|
245
|
8 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
3 |
|
public function getPhpunitPath() : string |
249
|
|
|
{ |
250
|
3 |
|
return $this->phpunitPath; |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self |
254
|
|
|
{ |
255
|
1 |
|
$this->databaseSandbox = $databaseSandbox; |
256
|
|
|
|
257
|
1 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
3 |
|
public function getDatabaseSandbox() : DatabaseSandbox |
261
|
|
|
{ |
262
|
3 |
|
if ($this->databaseSandbox === null) { |
263
|
3 |
|
$this->databaseSandbox = new DatabaseSandbox(); |
264
|
|
|
} |
265
|
|
|
|
266
|
3 |
|
return $this->databaseSandbox; |
267
|
|
|
} |
268
|
|
|
|
269
|
2 |
|
public function setDatabaseNames(array $databaseNames) : self |
270
|
|
|
{ |
271
|
2 |
|
$this->getDatabaseSandbox()->setDatabaseNames($databaseNames); |
272
|
|
|
|
273
|
2 |
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function setSandboxEnabled(bool $sandboxEnabled) : self |
277
|
|
|
{ |
278
|
|
|
$this->getDatabaseSandbox()->setSandboxEnabled($sandboxEnabled); |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
1 |
|
public function setMemoryLimit(string $memoryLimit) : self |
284
|
|
|
{ |
285
|
1 |
|
$this->memoryLimit = $memoryLimit; |
286
|
|
|
|
287
|
1 |
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
2 |
|
public function getMemoryLimit() : string |
291
|
|
|
{ |
292
|
2 |
|
return $this->memoryLimit; |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
public function setNumChunks(int $numChunks) : self |
296
|
|
|
{ |
297
|
1 |
|
$this->numChunks = $numChunks; |
298
|
|
|
|
299
|
1 |
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
public function getNumChunks() : int |
303
|
|
|
{ |
304
|
1 |
|
return $this->numChunks; |
305
|
|
|
} |
306
|
|
|
|
307
|
1 |
|
public function setEventDispatcher(EventDispatcher $eventDispatcher) : self |
308
|
|
|
{ |
309
|
1 |
|
$this->eventDispatcher = $eventDispatcher; |
310
|
|
|
|
311
|
1 |
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
2 |
|
public function getEventDispatcher() : EventDispatcher |
315
|
|
|
{ |
316
|
2 |
|
if ($this->eventDispatcher === null) { |
317
|
2 |
|
$this->eventDispatcher = new EventDispatcher(); |
318
|
|
|
} |
319
|
|
|
|
320
|
2 |
|
return $this->eventDispatcher; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function isSetup() |
324
|
|
|
{ |
325
|
|
|
if (!$this->rootDir) { |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (!$this->testsDirectory) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (!$this->phpunitPath) { |
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return true; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|