DynamoDbFailedJobProviderTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Illuminate\Tests\Queue;
  3. use Aws\DynamoDb\DynamoDbClient;
  4. use Carbon\CarbonImmutable;
  5. use DateTimeInterface;
  6. use Exception;
  7. use Illuminate\Queue\Failed\DynamoDbFailedJobProvider;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Str;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. class DynamoDbFailedJobProviderTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. }
  18. public function testCanProperlyLogFailedJob()
  19. {
  20. $uuid = Str::orderedUuid();
  21. Str::createUuidsUsing(function () use ($uuid) {
  22. return $uuid;
  23. });
  24. Carbon::setTestNow($now = CarbonImmutable::now());
  25. $exception = new Exception('Something went wrong.');
  26. $dynamoDbClient = m::mock(DynamoDbClient::class);
  27. $dynamoDbClient->shouldReceive('putItem')->once()->with([
  28. 'TableName' => 'table',
  29. 'Item' => [
  30. 'application' => ['S' => 'application'],
  31. 'uuid' => ['S' => (string) $uuid],
  32. 'connection' => ['S' => 'connection'],
  33. 'queue' => ['S' => 'queue'],
  34. 'payload' => ['S' => json_encode(['uuid' => (string) $uuid])],
  35. 'exception' => ['S' => (string) $exception],
  36. 'failed_at' => ['N' => (string) $now->getTimestamp()],
  37. 'expires_at' => ['N' => (string) $now->addDays(3)->getTimestamp()],
  38. ],
  39. ]);
  40. $provider = new DynamoDbFailedJobProvider($dynamoDbClient, 'application', 'table');
  41. $provider->log('connection', 'queue', json_encode(['uuid' => (string) $uuid]), $exception);
  42. Str::createUuidsNormally();
  43. }
  44. public function testCanRetrieveAllFailedJobs()
  45. {
  46. $dynamoDbClient = m::mock(DynamoDbClient::class);
  47. $time = time();
  48. $dynamoDbClient->shouldReceive('query')->once()->with([
  49. 'TableName' => 'table',
  50. 'Select' => 'ALL_ATTRIBUTES',
  51. 'KeyConditionExpression' => 'application = :application',
  52. 'ExpressionAttributeValues' => [
  53. ':application' => ['S' => 'application'],
  54. ],
  55. 'ScanIndexForward' => false,
  56. ])->andReturn([
  57. 'Items' => [
  58. [
  59. 'application' => ['S' => 'application'],
  60. 'uuid' => ['S' => 'uuid'],
  61. 'connection' => ['S' => 'connection'],
  62. 'queue' => ['S' => 'queue'],
  63. 'payload' => ['S' => 'payload'],
  64. 'exception' => ['S' => 'exception'],
  65. 'failed_at' => ['N' => (string) $time],
  66. 'expires_at' => ['N' => (string) $time],
  67. ],
  68. ],
  69. ]);
  70. $provider = new DynamoDbFailedJobProvider($dynamoDbClient, 'application', 'table');
  71. $response = $provider->all();
  72. $this->assertEquals([
  73. (object) [
  74. 'id' => 'uuid',
  75. 'connection' => 'connection',
  76. 'queue' => 'queue',
  77. 'payload' => 'payload',
  78. 'exception' => 'exception',
  79. 'failed_at' => Carbon::createFromTimestamp($time)->format(DateTimeInterface::ISO8601),
  80. ],
  81. ], $response);
  82. }
  83. public function testASingleJobCanBeFound()
  84. {
  85. $dynamoDbClient = m::mock(DynamoDbClient::class);
  86. $time = time();
  87. $dynamoDbClient->shouldReceive('getItem')->once()->with([
  88. 'TableName' => 'table',
  89. 'Key' => [
  90. 'application' => ['S' => 'application'],
  91. 'uuid' => ['S' => 'id'],
  92. ],
  93. ])->andReturn([
  94. 'Item' => [
  95. 'application' => ['S' => 'application'],
  96. 'uuid' => ['S' => 'uuid'],
  97. 'connection' => ['S' => 'connection'],
  98. 'queue' => ['S' => 'queue'],
  99. 'payload' => ['S' => 'payload'],
  100. 'exception' => ['S' => 'exception'],
  101. 'failed_at' => ['N' => (string) $time],
  102. 'expires_at' => ['N' => (string) $time],
  103. ],
  104. ]);
  105. $provider = new DynamoDbFailedJobProvider($dynamoDbClient, 'application', 'table');
  106. $response = $provider->find('id');
  107. $this->assertEquals(
  108. (object) [
  109. 'id' => 'uuid',
  110. 'connection' => 'connection',
  111. 'queue' => 'queue',
  112. 'payload' => 'payload',
  113. 'exception' => 'exception',
  114. 'failed_at' => Carbon::createFromTimestamp($time)->format(DateTimeInterface::ISO8601),
  115. ], $response
  116. );
  117. }
  118. public function testNullIsReturnedIfJobNotFound()
  119. {
  120. $dynamoDbClient = m::mock(DynamoDbClient::class);
  121. $dynamoDbClient->shouldReceive('getItem')->once()->with([
  122. 'TableName' => 'table',
  123. 'Key' => [
  124. 'application' => ['S' => 'application'],
  125. 'uuid' => ['S' => 'id'],
  126. ],
  127. ])->andReturn([]);
  128. $provider = new DynamoDbFailedJobProvider($dynamoDbClient, 'application', 'table');
  129. $response = $provider->find('id');
  130. $this->assertNull($response);
  131. }
  132. public function testJobsCanBeDeleted()
  133. {
  134. $dynamoDbClient = m::mock(DynamoDbClient::class);
  135. $dynamoDbClient->shouldReceive('deleteItem')->once()->with([
  136. 'TableName' => 'table',
  137. 'Key' => [
  138. 'application' => ['S' => 'application'],
  139. 'uuid' => ['S' => 'id'],
  140. ],
  141. ])->andReturn([]);
  142. $provider = new DynamoDbFailedJobProvider($dynamoDbClient, 'application', 'table');
  143. $provider->forget('id');
  144. }
  145. }