|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Google\Cloud\Tasks\V2\CloudTasksClient; |
| 7 | +use Google\Cloud\Tasks\V2\HttpMethod; |
| 8 | +use Google\Cloud\Tasks\V2\HttpRequest; |
| 9 | +use Google\Cloud\Tasks\V2\Task; |
| 10 | +use Google\Protobuf\Timestamp; |
| 11 | +use Mockery; |
| 12 | +use Tests\Support\SimpleJob; |
| 13 | + |
| 14 | +class QueueTest extends TestCase |
| 15 | +{ |
| 16 | + private $client; |
| 17 | + private $http; |
| 18 | + private $task; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->client = Mockery::mock(CloudTasksClient::class)->makePartial(); |
| 25 | + $this->http = Mockery::mock(HttpRequest::class)->makePartial(); |
| 26 | + $this->task = Mockery::mock(new Task); |
| 27 | + |
| 28 | + $this->app->instance(CloudTasksClient::class, $this->client); |
| 29 | + $this->app->instance(HttpRequest::class, $this->http); |
| 30 | + $this->app->instance(Task::class, $this->task); |
| 31 | + |
| 32 | + // ensure we don't actually call the Google API |
| 33 | + $this->client->shouldReceive('createTask')->andReturnNull(); |
| 34 | + } |
| 35 | + |
| 36 | + /** @test */ |
| 37 | + public function a_http_request_with_the_handler_url_is_made() |
| 38 | + { |
| 39 | + SimpleJob::dispatch(); |
| 40 | + |
| 41 | + $this->http |
| 42 | + ->shouldHaveReceived('setUrl') |
| 43 | + ->with('https://localhost/my-handler') |
| 44 | + ->once(); |
| 45 | + } |
| 46 | + |
| 47 | + /** @test */ |
| 48 | + public function it_posts_to_the_handler() |
| 49 | + { |
| 50 | + SimpleJob::dispatch(); |
| 51 | + |
| 52 | + $this->http->shouldHaveReceived('setHttpMethod')->with(HttpMethod::POST)->once(); |
| 53 | + } |
| 54 | + |
| 55 | + /** @test */ |
| 56 | + public function it_posts_the_serialized_job_payload_to_the_handler() |
| 57 | + { |
| 58 | + $job = new SimpleJob(); |
| 59 | + $job->dispatch(); |
| 60 | + |
| 61 | + $this->http->shouldHaveReceived('setBody')->with(Mockery::on(function ($payload) use ($job) { |
| 62 | + $decoded = json_decode($payload, true); |
| 63 | + |
| 64 | + if ($decoded['displayName'] != 'Tests\Support\SimpleJob') { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if ($decoded['job'] != 'Illuminate\Queue\CallQueuedHandler@call') { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if ($decoded['data']['commandName'] != 'Tests\Support\SimpleJob') { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if ($decoded['data']['command'] != serialize($job)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + })); |
| 82 | + } |
| 83 | + |
| 84 | + /** @test */ |
| 85 | + public function it_creates_a_task_containing_the_http_request() |
| 86 | + { |
| 87 | + $this->task->shouldReceive('setHttpRequest')->once()->with($this->http); |
| 88 | + |
| 89 | + SimpleJob::dispatch(); |
| 90 | + } |
| 91 | + |
| 92 | + /** @test */ |
| 93 | + public function it_will_set_the_scheduled_time_when_dispatching_later() |
| 94 | + { |
| 95 | + $inFiveMinutes = Carbon::now()->addMinutes(5); |
| 96 | + |
| 97 | + SimpleJob::dispatch()->delay($inFiveMinutes); |
| 98 | + |
| 99 | + $this->task->shouldHaveReceived('setScheduleTime')->once()->with(Mockery::on(function (Timestamp $timestamp) use ($inFiveMinutes) { |
| 100 | + return $timestamp->getSeconds() === $inFiveMinutes->timestamp; |
| 101 | + })); |
| 102 | + } |
| 103 | + |
| 104 | + /** @test */ |
| 105 | + public function it_posts_the_task_the_correct_queue() |
| 106 | + { |
| 107 | + SimpleJob::dispatch(); |
| 108 | + |
| 109 | + $this->client |
| 110 | + ->shouldHaveReceived('createTask') |
| 111 | + ->withArgs(function ($queueName) { |
| 112 | + return $queueName === 'projects/test-project/locations/europe-west6/queues/test-queue'; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + /** @test */ |
| 117 | + public function it_posts_the_correct_task_the_queue() |
| 118 | + { |
| 119 | + SimpleJob::dispatch(); |
| 120 | + |
| 121 | + $this->client |
| 122 | + ->shouldHaveReceived('createTask') |
| 123 | + ->withArgs(function ($queueName, $task) { |
| 124 | + return $task === $this->task; |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
0 commit comments