-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoctrineDBALJobExecutionStorageTest.php
More file actions
461 lines (410 loc) · 16.9 KB
/
DoctrineDBALJobExecutionStorageTest.php
File metadata and controls
461 lines (410 loc) · 16.9 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
declare(strict_types=1);
namespace Yokai\Batch\Tests\Bridge\Doctrine\DBAL;
use DateTimeImmutable;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;
use Throwable;
use Yokai\Batch\BatchStatus;
use Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage;
use Yokai\Batch\Exception\CannotRemoveJobExecutionException;
use Yokai\Batch\Exception\CannotStoreJobExecutionException;
use Yokai\Batch\Exception\JobExecutionNotFoundException;
use Yokai\Batch\Exception\UnexpectedValueException;
use Yokai\Batch\JobExecution;
use Yokai\Batch\Storage\Query;
use Yokai\Batch\Storage\QueryBuilder;
use Yokai\Batch\Test\Storage\JobExecutionStorageTestTrait;
use Yokai\Batch\Warning;
class DoctrineDBALJobExecutionStorageTest extends DoctrineDBALTestCase
{
use JobExecutionStorageTestTrait;
private function createStorage(array $options = []): DoctrineDBALJobExecutionStorage
{
return new DoctrineDBALJobExecutionStorage($this->doctrine, $options);
}
public function testCreateStandardTable(): void
{
$schemaManager = $this->connection->createSchemaManager();
self::assertFalse($schemaManager->tablesExist(['yokai_batch_job_execution']));
$this->createStorage()->setup();
self::assertTrue($schemaManager->tablesExist(['yokai_batch_job_execution']));
$columns = $schemaManager->listTableColumns('yokai_batch_job_execution');
self::assertSame(
[
'id',
'job_name',
'status',
'parameters',
'start_time',
'end_time',
'summary',
'failures',
'warnings',
'child_executions',
'logs',
],
\array_keys($columns),
);
}
public function testCreateCustomTable(): void
{
$schemaManager = $this->connection->createSchemaManager();
self::assertFalse($schemaManager->tablesExist(['acme_job_executions']));
$this->createStorage(['table' => 'acme_job_executions'])->setup();
self::assertTrue($schemaManager->tablesExist(['acme_job_executions']));
$columns = $schemaManager->listTableColumns('acme_job_executions');
self::assertSame(
[
'id',
'job_name',
'status',
'parameters',
'start_time',
'end_time',
'summary',
'failures',
'warnings',
'child_executions',
'logs',
],
\array_keys($columns),
);
}
public function testSetupPreserveOtherTables(): void
{
$schemaManager = $this->connection->createSchemaManager();
$table = new Table('user');
$table->addColumn('username', Types::STRING);
$schemaManager->createTable($table);
self::assertTrue($schemaManager->tablesExist(['user']));
self::assertFalse($schemaManager->tablesExist(['yokai_batch_job_execution']));
$this->createStorage()->setup();
self::assertTrue($schemaManager->tablesExist(['user']));
self::assertTrue($schemaManager->tablesExist(['yokai_batch_job_execution']));
}
public function testStoreInsert(): void
{
$storage = $this->createStorage();
$storage->setup();
$export = JobExecution::createRoot('123', 'export', new BatchStatus(BatchStatus::RUNNING));
$export->setStartTime(new DateTimeImmutable('2021-09-23 11:05:00'));
$export->addChildExecution($extract = JobExecution::createChild($export, 'extract'));
$extract->setStartTime(new DateTimeImmutable('2021-09-23 11:05:01'));
$export->addChildExecution($upload = JobExecution::createChild($export, 'upload'));
$extract->addWarning(new Warning('Test warning'));
$upload->addFailureException(new RuntimeException('Test failure'));
$storage->store($export);
$retrievedExport = $storage->retrieve('export', '123');
self::assertSame('export', $retrievedExport->getJobName());
self::assertSame('123', $retrievedExport->getId());
self::assertSame('2021-09-23 11:05:00', $retrievedExport->getStartTime()->format('Y-m-d H:i:s'));
self::assertNull($retrievedExport->getEndTime());
self::assertSame(BatchStatus::RUNNING, $retrievedExport->getStatus()->getValue());
$retrievedExtract = $retrievedExport->getChildExecution('extract');
self::assertNotNull($retrievedExtract);
self::assertSame('2021-09-23 11:05:01', $retrievedExtract->getStartTime()->format('Y-m-d H:i:s'));
self::assertNull($retrievedExtract->getEndTime());
self::assertCount(1, $retrievedExtract->getWarnings());
self::assertSame('Test warning', $retrievedExtract->getWarnings()[0]->getMessage());
self::assertCount(0, $retrievedExtract->getFailures());
$retrievedUpload = $retrievedExport->getChildExecution('upload');
self::assertNotNull($retrievedUpload);
self::assertNull($retrievedUpload->getStartTime());
self::assertNull($retrievedUpload->getEndTime());
self::assertCount(0, $retrievedUpload->getWarnings());
self::assertCount(1, $retrievedUpload->getFailures());
self::assertSame('Test failure', $retrievedUpload->getFailures()[0]->getMessage());
}
public function testStoreUpdate(): void
{
$storage = $this->createStorage();
$storage->setup();
$storage->store($execution = JobExecution::createRoot('123', 'export'));
$execution->setStatus(BatchStatus::COMPLETED);
$storage->store($execution);
$retrievedExecution = $storage->retrieve('export', '123');
self::assertSame('export', $retrievedExecution->getJobName());
self::assertSame('123', $retrievedExecution->getId());
self::assertSame(BatchStatus::COMPLETED, $retrievedExecution->getStatus()->getValue());
}
public function testStoreFailing(): void
{
$this->expectException(CannotStoreJobExecutionException::class);
$storage = $this->createStorage();
/** not calling {@see DoctrineDBALJobExecutionStorage::setup} will cause table to not exists */
$storage->store(JobExecution::createRoot('123', 'export'));
}
public function testRemove(): void
{
$this->expectException(JobExecutionNotFoundException::class);
$storage = $this->createStorage();
$storage->setup();
$storage->store($execution = JobExecution::createRoot('123', 'export'));
$storage->remove($execution);
$storage->retrieve('export', '123');
}
public function testRemoveFailing(): void
{
$this->expectException(CannotRemoveJobExecutionException::class);
$storage = $this->createStorage();
/** not calling {@see DoctrineDBALJobExecutionStorage::setup} will cause table to not exists */
$storage->remove(JobExecution::createRoot('123', 'export'));
}
public function testRetrieve(): void
{
$storage = $this->createStorage();
$storage->setup();
$storage->store(JobExecution::createRoot('123', 'export'));
$storage->store(JobExecution::createRoot('456', 'import'));
$execution123 = $storage->retrieve('export', '123');
self::assertSame('export', $execution123->getJobName());
self::assertSame('123', $execution123->getId());
$execution456 = $storage->retrieve('import', '456');
self::assertSame('import', $execution456->getJobName());
self::assertSame('456', $execution456->getId());
}
public function testRetrieveNotFound(): void
{
$this->expectException(JobExecutionNotFoundException::class);
$storage = $this->createStorage();
$storage->setup();
$storage->store(JobExecution::createRoot('123', 'export'));
$storage->retrieve('export', '456');
}
public function testRetrieveFailing(): void
{
$this->expectException(JobExecutionNotFoundException::class);
$storage = $this->createStorage();
/** not calling {@see DoctrineDBALJobExecutionStorage::setup} will cause table to not exists */
$storage->retrieve('export', '456');
}
#[DataProvider('retrieveInvalid')]
public function testRetrieveInvalid(array $data, Throwable $error): void
{
$this->expectExceptionObject($error);
$data['id'] = '123';
$data['job_name'] = 'export';
$data['status'] ??= BatchStatus::COMPLETED;
$data['parameters'] ??= '[]';
$data['summary'] ??= '[]';
$data['failures'] ??= '[]';
$data['warnings'] ??= '[]';
$data['child_executions'] ??= '[]';
$data['logs'] ??= '';
$storage = $this->createStorage();
$storage->setup();
$this->connection->insert('yokai_batch_job_execution', $data);
$storage->retrieve('export', '123');
}
public static function retrieveInvalid(): \Generator
{
yield '"parameters" column value is expected to be array' => [
['parameters' => '"string"'],
UnexpectedValueException::type('array', 'string'),
];
yield '"summary" column value is expected to be array' => [
['summary' => '"string"'],
UnexpectedValueException::type('array', 'string'),
];
yield '"failures" column value is expected to be array' => [
['failures' => '"string"'],
UnexpectedValueException::type('array', 'string'),
];
yield '"warnings" column value is expected to be array' => [
['warnings' => '"string"'],
UnexpectedValueException::type('array', 'string'),
];
yield '"child_executions" column value is expected to be array' => [
['child_executions' => '"string"'],
UnexpectedValueException::type('array', 'string'),
];
}
public function testList(): void
{
$storage = $this->createStorage();
$storage->setup();
$this->loadFixtures($storage);
self::assertExecutionIds(['123'], $storage->list('export'));
self::assertExecutionIds(['456', '789', '987'], $storage->list('import'));
}
#[DataProvider('queries')]
public function testQuery(QueryBuilder $queryBuilder, array $expectedCouples): void
{
$storage = $this->createStorage();
$storage->setup();
$this->loadFixtures($storage);
self::assertExecutions($expectedCouples, $storage->query($queryBuilder->getQuery()));
self::assertCount($storage->count($queryBuilder->getQuery()), $expectedCouples);
}
public static function queries(): Generator
{
yield 'No filter' => [
new QueryBuilder(),
[
['export', '123'],
['import', '456'],
['import', '789'],
['import', '987'],
],
];
yield 'Filter ids' => [
(new QueryBuilder())
->ids(['123', '987']),
[
['export', '123'],
['import', '987'],
],
];
yield 'Filter job names' => [
(new QueryBuilder())
->jobs(['export']),
[
['export', '123'],
],
];
yield 'Filter statuses' => [
(new QueryBuilder())
->statuses([BatchStatus::FAILED]),
[
['import', '456'],
],
];
yield 'Order by start ASC' => [
(new QueryBuilder())
->sort(Query::SORT_BY_START_ASC),
[
['import', '987'],
['import', '789'],
['export', '123'],
['import', '456'],
],
];
yield 'Order by start DESC' => [
(new QueryBuilder())
->sort(Query::SORT_BY_START_DESC),
[
['import', '456'],
['export', '123'],
['import', '789'],
['import', '987'],
],
];
yield 'Order by end ASC' => [
(new QueryBuilder())
->sort(Query::SORT_BY_END_ASC),
[
['import', '789'],
['import', '987'],
['export', '123'],
['import', '456'],
],
];
yield 'Order by end DESC' => [
(new QueryBuilder())
->sort(Query::SORT_BY_END_DESC),
[
['import', '456'],
['export', '123'],
['import', '987'],
['import', '789'],
],
];
yield 'Filter start time lower boundary' => [
(new QueryBuilder())
->startTime(new \DateTimeImmutable('2019-07-01T13:00:01+0200'), null),
[
['import', '456'],
],
];
yield 'Filter start time upper boundary' => [
(new QueryBuilder())
->startTime(null, new \DateTimeImmutable('2019-06-30T22:00:00+0200')),
[
['import', '789'],
],
];
yield 'Filter start time boundaries' => [
(new QueryBuilder())
->startTime(
new \DateTimeImmutable('2019-07-01T13:00:01+0200'),
new \DateTimeImmutable('2019-07-01T17:29:29+0200'),
),
[
// none
],
];
yield 'Filter end time lower boundary' => [
(new QueryBuilder())
->endTime(new \DateTimeImmutable('2019-07-01T13:30:01+0200'), null),
[
['import', '456'],
],
];
yield 'Filter end time upper boundary' => [
(new QueryBuilder())
->endTime(null, new \DateTimeImmutable('2019-07-01T18:29:59+0200')),
[
['export', '123'],
],
];
yield 'Filter end time boundaries' => [
(new QueryBuilder())
->endTime(
new \DateTimeImmutable('2019-07-01T13:30:01+0200'),
new \DateTimeImmutable('2019-07-01T18:29:59+0200'),
),
[
],
];
}
public static function assertExecutionIds(array $ids, iterable $executions): void
{
$actualIds = [];
/** @var JobExecution $execution */
foreach ($executions as $execution) {
self::assertInstanceOf(JobExecution::class, $execution);
$actualIds[] = $execution->getId();
}
self::assertSame($ids, $actualIds);
}
private static function assertExecutions(array $expectedCouples, iterable $executions): void
{
$expected = [];
foreach ($expectedCouples as [$jobName, $executionId]) {
$expected[] = $jobName . '/' . $executionId;
}
$actual = [];
/** @var JobExecution $execution */
foreach ($executions as $execution) {
$actual[] = $execution->getJobName() . '/' . $execution->getId();
}
self::assertSame($expected, $actual);
}
private function loadFixtures(DoctrineDBALJobExecutionStorage $storage): void
{
// completed export started at 2019-07-01 13:00 and ended at 2019-07-01 13:30
$completedExport = JobExecution::createRoot('123', 'export', new BatchStatus(BatchStatus::COMPLETED));
$completedExport->setStartTime(\DateTimeImmutable::createFromFormat(DATE_ISO8601, '2019-07-01T13:00:00+0200'));
$completedExport->setEndTime(\DateTimeImmutable::createFromFormat(DATE_ISO8601, '2019-07-01T13:30:00+0200'));
$storage->store($completedExport);
// failed import started at 2019-07-01 17:30 and ended at 2019-07-01 18:30
$failedImport = JobExecution::createRoot('456', 'import', new BatchStatus(BatchStatus::FAILED));
$failedImport->setStartTime(\DateTimeImmutable::createFromFormat(DATE_ISO8601, '2019-07-01T17:30:00+0200'));
$failedImport->setEndTime(\DateTimeImmutable::createFromFormat(DATE_ISO8601, '2019-07-01T18:30:00+0200'));
$storage->store($failedImport);
// running import started at 2019-06-30 22:00 and not ended
$runningImport = JobExecution::createRoot('789', 'import', new BatchStatus(BatchStatus::RUNNING));
$runningImport->setStartTime(\DateTimeImmutable::createFromFormat(DATE_ISO8601, '2019-06-30T22:00:00+0200'));
$runningImport->getLogger()->debug('Importing things');
$runningImport->getLogger()->info('Thing imported');
$runningImport->getLogger()->warning('Weird thing imported');
$storage->store($runningImport);
// pending import not started and not ended
$pendingImport = JobExecution::createRoot('987', 'import', new BatchStatus(BatchStatus::PENDING));
$storage->store($pendingImport);
}
}