|
| 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