DynamoDbStoreTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Illuminate\Tests\Integration\Cache;
  3. use Aws\DynamoDb\DynamoDbClient;
  4. use Aws\Exception\AwsException;
  5. use Illuminate\Contracts\Cache\Repository;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Str;
  8. use Orchestra\Testbench\TestCase;
  9. class DynamoDbStoreTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. if (! env('DYNAMODB_CACHE_TABLE')) {
  14. $this->markTestSkipped('DynamoDB not configured.');
  15. }
  16. parent::setUp();
  17. }
  18. public function testItemsCanBeStoredAndRetrieved()
  19. {
  20. Cache::driver('dynamodb')->put('name', 'Taylor', 10);
  21. $this->assertSame('Taylor', Cache::driver('dynamodb')->get('name'));
  22. Cache::driver('dynamodb')->put(['name' => 'Abigail', 'age' => 28], 10);
  23. $this->assertSame('Abigail', Cache::driver('dynamodb')->get('name'));
  24. $this->assertEquals(28, Cache::driver('dynamodb')->get('age'));
  25. $this->assertEquals([
  26. 'name' => 'Abigail',
  27. 'age' => 28,
  28. 'height' => null,
  29. ], Cache::driver('dynamodb')->many(['name', 'age', 'height']));
  30. Cache::driver('dynamodb')->forget('name');
  31. $this->assertNull(Cache::driver('dynamodb')->get('name'));
  32. }
  33. public function testItemsCanBeAtomicallyAdded()
  34. {
  35. $key = Str::random(6);
  36. $this->assertTrue(Cache::driver('dynamodb')->add($key, 'Taylor', 10));
  37. $this->assertFalse(Cache::driver('dynamodb')->add($key, 'Taylor', 10));
  38. }
  39. public function testItemsCanBeIncrementedAndDecremented()
  40. {
  41. Cache::driver('dynamodb')->put('counter', 0, 10);
  42. Cache::driver('dynamodb')->increment('counter');
  43. Cache::driver('dynamodb')->increment('counter', 4);
  44. $this->assertEquals(5, Cache::driver('dynamodb')->get('counter'));
  45. Cache::driver('dynamodb')->decrement('counter', 5);
  46. $this->assertEquals(0, Cache::driver('dynamodb')->get('counter'));
  47. }
  48. public function testLocksCanBeAcquired()
  49. {
  50. Cache::driver('dynamodb')->lock('lock', 10)->get(function () {
  51. $this->assertFalse(Cache::driver('dynamodb')->lock('lock', 10)->get());
  52. });
  53. }
  54. /**
  55. * Define environment setup.
  56. *
  57. * @param \Illuminate\Foundation\Application $app
  58. * @return void
  59. */
  60. protected function getEnvironmentSetUp($app)
  61. {
  62. if (! env('DYNAMODB_CACHE_TABLE')) {
  63. $this->markTestSkipped('DynamoDB not configured.');
  64. }
  65. $app['config']->set('cache.default', 'dynamodb');
  66. $config = $app['config']->get('cache.stores.dynamodb');
  67. /** @var \Aws\DynamoDb\DynamoDbClient $client */
  68. $client = $app->make(Repository::class)->getStore()->getClient();
  69. if ($this->dynamoTableExists($client, $config['table'])) {
  70. return;
  71. }
  72. $client->createTable([
  73. 'TableName' => $config['table'],
  74. 'KeySchema' => [
  75. [
  76. 'AttributeName' => $config['attributes']['key'] ?? 'key',
  77. 'KeyType' => 'HASH',
  78. ],
  79. ],
  80. 'AttributeDefinitions' => [
  81. [
  82. 'AttributeName' => $config['attributes']['key'] ?? 'key',
  83. 'AttributeType' => 'S',
  84. ],
  85. ],
  86. 'ProvisionedThroughput' => [
  87. 'ReadCapacityUnits' => 1,
  88. 'WriteCapacityUnits' => 1,
  89. ],
  90. ]);
  91. }
  92. /**
  93. * Determine if the given DynamoDB table exists.
  94. *
  95. * @param \Aws\DynamoDb\DynamoDbClient $client
  96. * @param string $table
  97. * @return bool
  98. */
  99. public function dynamoTableExists(DynamoDbClient $client, $table)
  100. {
  101. try {
  102. $client->describeTable([
  103. 'TableName' => $table,
  104. ]);
  105. return true;
  106. } catch (AwsException $e) {
  107. if (Str::contains($e->getAwsErrorMessage(), ['resource not found', 'Cannot do operations on a non-existent table'])) {
  108. return false;
  109. }
  110. throw $e;
  111. }
  112. }
  113. }