Skip to content

Commit 06ced2c

Browse files
authored
Add package tests for JobWithChildJobs (#103)
* Add package tests for JobWithChildJobs * Add documentation for JobWithChildJobs factory method * Fixed code style
1 parent 6155350 commit 06ced2c

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/batch/src/Job/JobWithChildJobs.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Yokai\Batch\Job;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use Yokai\Batch\BatchStatus;
89
use Yokai\Batch\JobExecution;
10+
use Yokai\Batch\Registry\JobRegistry;
911
use Yokai\Batch\Storage\JobExecutionStorageInterface;
1012

1113
/**
@@ -26,6 +28,24 @@ public function __construct(
2628
) {
2729
}
2830

31+
/**
32+
* When creating {@see JobWithChildJobs}, you might prefer that child jobs are not available from the outside.
33+
* You can do it by yourself, but this method will do it for you in one line.
34+
*
35+
* @param array<string, JobInterface> $children
36+
*/
37+
public static function withAnonymousChildren(
38+
array $children,
39+
JobExecutionStorageInterface $executionStorage,
40+
EventDispatcherInterface $eventDispatcher = null,
41+
): self {
42+
return new self(
43+
$executionStorage,
44+
new JobExecutor(JobRegistry::fromJobArray($children), $executionStorage, $eventDispatcher),
45+
\array_keys($children),
46+
);
47+
}
48+
2949
final public function execute(JobExecution $jobExecution): void
3050
{
3151
$logger = $jobExecution->getLogger();
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Job;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yokai\Batch\BatchStatus;
9+
use Yokai\Batch\Job\JobExecutor;
10+
use Yokai\Batch\Job\JobInterface;
11+
use Yokai\Batch\Job\JobWithChildJobs;
12+
use Yokai\Batch\JobExecution;
13+
use Yokai\Batch\Registry\JobRegistry;
14+
use Yokai\Batch\Test\Storage\InMemoryJobExecutionStorage;
15+
16+
class JobWithChildJobsTest extends TestCase
17+
{
18+
private InMemoryJobExecutionStorage $storage;
19+
20+
protected function setUp(): void
21+
{
22+
$this->storage = new InMemoryJobExecutionStorage();
23+
}
24+
25+
public function test(): void
26+
{
27+
$execution = $this->execute([
28+
'import' => new class() implements JobInterface {
29+
public function execute(JobExecution $jobExecution): void
30+
{
31+
$jobExecution->getSummary()->set('executed', true);
32+
}
33+
},
34+
'report' => new class() implements JobInterface {
35+
public function execute(JobExecution $jobExecution): void
36+
{
37+
$jobExecution->getSummary()->set('executed', true);
38+
}
39+
},
40+
]);
41+
42+
self::assertStatusSame(BatchStatus::COMPLETED, $execution);
43+
self::assertCount(2, $execution->getChildExecutions());
44+
self::assertNotNull($import = $execution->getChildExecution('import'));
45+
self::assertStatusSame(BatchStatus::COMPLETED, $import);
46+
self::assertTrue($import->getSummary()->get('executed'));
47+
self::assertLogsContains('DEBUG: Starting child job {"job":"import"}', $execution);
48+
self::assertLogsContains('INFO: Child job executed successfully {"job":"import"}', $execution);
49+
self::assertNotNull($report = $execution->getChildExecution('report'));
50+
self::assertStatusSame(BatchStatus::COMPLETED, $report);
51+
self::assertTrue($report->getSummary()->get('executed'));
52+
self::assertLogsContains('DEBUG: Starting child job {"job":"report"}', $execution);
53+
self::assertLogsContains('INFO: Child job executed successfully {"job":"report"}', $execution);
54+
}
55+
56+
public function testNoChildren(): void
57+
{
58+
$execution = $this->execute([
59+
]);
60+
61+
self::assertStatusSame(BatchStatus::COMPLETED, $execution);
62+
self::assertCount(0, $execution->getChildExecutions());
63+
self::assertLogsNotContains('Child job executed successfully', $execution);
64+
self::assertLogsNotContains('Child job did not executed successfully', $execution);
65+
}
66+
67+
public function testFirstChildFailing(): void
68+
{
69+
$execution = $this->execute([
70+
'import' => new class() implements JobInterface {
71+
public function execute(JobExecution $jobExecution): void
72+
{
73+
throw new \RuntimeException('Expected failure');
74+
}
75+
},
76+
'report' => new class() implements JobInterface {
77+
public function execute(JobExecution $jobExecution): void
78+
{
79+
throw new \LogicException('Should never be executed');
80+
}
81+
},
82+
]);
83+
84+
self::assertStatusSame(BatchStatus::FAILED, $execution);
85+
self::assertCount(2, $execution->getChildExecutions());
86+
self::assertNotNull($import = $execution->getChildExecution('import'));
87+
self::assertStatusSame(BatchStatus::FAILED, $import);
88+
self::assertLogsContains('ERROR: Child job did not executed successfully {"job":"import"', $execution);
89+
self::assertNotNull($report = $execution->getChildExecution('report'));
90+
self::assertStatusSame(BatchStatus::ABANDONED, $report);
91+
self::assertLogsContains('WARNING: Child job will not be executed {"job":"report"}', $execution);
92+
}
93+
94+
/**
95+
* @param array<string, JobInterface> $children
96+
*/
97+
private function execute(array $children): JobExecution
98+
{
99+
(new JobExecutor(
100+
JobRegistry::fromJobArray(['test' => JobWithChildJobs::withAnonymousChildren($children, $this->storage)]),
101+
$this->storage,
102+
null,
103+
))->execute($execution = JobExecution::createRoot('650c0f7907774', 'test'));
104+
105+
return $execution;
106+
}
107+
108+
private static function assertStatusSame(int $expected, JobExecution $execution): void
109+
{
110+
self::assertSame($expected, $execution->getStatus()->getValue());
111+
}
112+
113+
private static function assertLogsContains(string $expected, JobExecution $execution): void
114+
{
115+
self::assertStringContainsString($expected, (string)$execution->getLogs());
116+
}
117+
118+
private static function assertLogsNotContains(string $expected, JobExecution $execution): void
119+
{
120+
self::assertStringNotContainsString($expected, (string)$execution->getLogs());
121+
}
122+
}

0 commit comments

Comments
 (0)