|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stackkit\LaravelGoogleCloudTasksQueue; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Google\Cloud\Tasks\V2\RetryConfig; |
| 9 | +use Google\Cloud\Tasks\V2\Task; |
| 10 | +use Google\Protobuf\Duration; |
| 11 | +use Illuminate\Support\Arr; |
| 12 | +use Illuminate\Support\Str; |
| 13 | +use PHPUnit\Framework\Assert; |
| 14 | + |
| 15 | +class CloudTasksApiFake implements CloudTasksApiContract |
| 16 | +{ |
| 17 | + public array $createdTasks = []; |
| 18 | + public array $deletedTasks = []; |
| 19 | + |
| 20 | + public function getRetryConfig(string $queueName): RetryConfig |
| 21 | + { |
| 22 | + $retryConfig = new RetryConfig(); |
| 23 | + |
| 24 | + $retryConfig |
| 25 | + ->setMinBackoff((new Duration(['seconds' => 0]))) |
| 26 | + ->setMaxBackoff((new Duration(['seconds' => 0]))); |
| 27 | + |
| 28 | + return $retryConfig; |
| 29 | + } |
| 30 | + |
| 31 | + public function createTask(string $queueName, Task $task): Task |
| 32 | + { |
| 33 | + $task->setName(Str::uuid()->toString()); |
| 34 | + |
| 35 | + $this->createdTasks[] = compact('queueName', 'task'); |
| 36 | + |
| 37 | + return $task; |
| 38 | + } |
| 39 | + |
| 40 | + public function deleteTask(string $taskName): void |
| 41 | + { |
| 42 | + $this->deletedTasks[] = $taskName; |
| 43 | + } |
| 44 | + |
| 45 | + public function getRetryUntilTimestamp(CloudTasksJob $job): ?int |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + public function assertTaskDeleted(string $taskName): void |
| 51 | + { |
| 52 | + $taskUuids = array_map(function ($fullTaskName) { |
| 53 | + return Arr::last(explode('/', $fullTaskName)); |
| 54 | + }, $this->deletedTasks); |
| 55 | + |
| 56 | + Assert::assertTrue( |
| 57 | + in_array($taskName, $taskUuids), |
| 58 | + 'The task [' . $taskName . '] should have been deleted but it is not.' |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function assertTaskNotDeleted(string $taskName): void |
| 63 | + { |
| 64 | + $taskUuids = array_map(function ($fullTaskName) { |
| 65 | + return Arr::last(explode('/', $fullTaskName)); |
| 66 | + }, $this->deletedTasks); |
| 67 | + |
| 68 | + Assert::assertTrue( |
| 69 | + ! in_array($taskName, $taskUuids), |
| 70 | + 'The task [' . $taskName . '] should not have been deleted but it was.' |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function assertDeletedTaskCount(int $count): void |
| 75 | + { |
| 76 | + Assert::assertCount($count, $this->deletedTasks); |
| 77 | + } |
| 78 | + |
| 79 | + public function assertTaskCreated(Closure $closure): void |
| 80 | + { |
| 81 | + $count = count(array_filter($this->createdTasks, function ($createdTask) use ($closure) { |
| 82 | + return $closure($createdTask['task'], $createdTask['queueName']); |
| 83 | + })); |
| 84 | + |
| 85 | + Assert::assertTrue($count > 0, 'Task was not created.'); |
| 86 | + } |
| 87 | +} |
0 commit comments