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