diff --git a/src/CloudTasksApiFake.php b/src/CloudTasksApiFake.php index c193e71..f1af5da 100644 --- a/src/CloudTasksApiFake.php +++ b/src/CloudTasksApiFake.php @@ -30,8 +30,6 @@ public function getRetryConfig(string $queueName): RetryConfig public function createTask(string $queueName, Task $task): Task { - $task->setName(Str::uuid()->toString()); - $this->createdTasks[] = compact('queueName', 'task'); return $task; @@ -56,24 +54,16 @@ public function getRetryUntilTimestamp(Task $task): ?int public function assertTaskDeleted(string $taskName): void { - $taskUuids = array_map(function ($fullTaskName) { - return Arr::last(explode('/', $fullTaskName)); - }, $this->deletedTasks); - Assert::assertTrue( - in_array($taskName, $taskUuids), + in_array($taskName, $this->deletedTasks), 'The task [' . $taskName . '] should have been deleted but it is not.' ); } public function assertTaskNotDeleted(string $taskName): void { - $taskUuids = array_map(function ($fullTaskName) { - return Arr::last(explode('/', $fullTaskName)); - }, $this->deletedTasks); - Assert::assertTrue( - ! in_array($taskName, $taskUuids), + ! in_array($taskName, $this->deletedTasks), 'The task [' . $taskName . '] should not have been deleted but it was.' ); } diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 33c8dba..c066687 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -139,19 +139,22 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0) $httpRequest->setUrl($this->getHandler()); $httpRequest->setHttpMethod(HttpMethod::POST); + $payload = json_decode($payload, true); + // Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it. // Since we are using and expecting the uuid in some places // we will add it manually here if it's not present yet. - [$payload, $uuid] = $this->withUuid($payload); + $payload = $this->withUuid($payload); // Since 3.x tasks are released back onto the queue after an exception has // been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header // value and need to manually set and update the number of times a task has been attempted. $payload = $this->withAttempts($payload); - $httpRequest->setBody($payload); + $httpRequest->setBody(json_encode($payload)); $task = $this->createTask(); + $task->setName($this->taskName($queue, $payload)); $task->setHttpRequest($httpRequest); // The deadline for requests sent to the app. If the app does not respond by @@ -174,34 +177,37 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0) event((new TaskCreated)->queue($queue)->task($task)); - return $uuid; + return $payload['uuid']; } - private function withUuid(string $payload): array + private function withUuid(array $payload): array { - /** @var array $decoded */ - $decoded = json_decode($payload, true); - - if (!isset($decoded['uuid'])) { - $decoded['uuid'] = (string) Str::uuid(); + if (!isset($payload['uuid'])) { + $payload['uuid'] = (string) Str::uuid(); } - return [ - json_encode($decoded), - $decoded['uuid'], - ]; + return $payload; } - private function withAttempts(string $payload): string + private function taskName(string $queueName, array $payload): string { - /** @var array $decoded */ - $decoded = json_decode($payload, true); + $displayName = str_replace("\\", '-', $payload['displayName']); + + return CloudTasksClient::taskName( + $this->config['project'], + $this->config['location'], + $queueName, + $displayName . '-' . $payload['uuid'] + ); + } - if (!isset($decoded['internal']['attempts'])) { - $decoded['internal']['attempts'] = 0; + private function withAttempts(array $payload): array + { + if (!isset($payload['internal']['attempts'])) { + $payload['internal']['attempts'] = 0; } - return json_encode($decoded); + return $payload; } /** diff --git a/tests/QueueTest.php b/tests/QueueTest.php index d4ef6aa..8ed8f67 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -478,4 +478,23 @@ public function test_ignoring_jobs_with_deleted_models() Log::assertLogged('UserJob:John'); CloudTasksApi::assertTaskNotDeleted($job->task->getName()); } + + /** + * @test + */ + public function it_adds_a_task_name_based_on_the_display_name() + { + // Arrange + CloudTasksApi::fake(); + + // Act + $this->dispatch((new SimpleJob())); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool { + $uuid = \Safe\json_decode($task->getHttpRequest()->getBody(), true)['uuid']; + + return $task->getName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob-' . $uuid; + }); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0c17bb7..587e802 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -133,7 +133,8 @@ public function dispatch($job) $payloadAsArray = json_decode($payload, true); $task = $event->task; - request()->headers->set('X-Cloudtasks-Taskname', $task->getName()); + [,,,,,,,$taskName] = explode('/', $task->getName()); + request()->headers->set('X-Cloudtasks-Taskname', $taskName); }); dispatch($job);