1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GuillermoandraeTest\Db\DynamoDb; |
4
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
6
|
|
|
use Guillermoandrae\Db\DbException; |
7
|
|
|
use Guillermoandrae\Db\DynamoDb\DynamoDbAdapter; |
8
|
|
|
use Guillermoandrae\Db\DynamoDb\RequestOperators; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class DynamoDbAdapterTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DynamoDbAdapter |
15
|
|
|
*/ |
16
|
|
|
private $adapter; |
17
|
|
|
|
18
|
|
|
public function testCreateDeleteListTable() |
19
|
|
|
{ |
20
|
|
|
$this->adapter->useTable('widgets')->createTable([ |
21
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
22
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
23
|
|
|
]); |
24
|
|
|
$this->assertTrue($this->adapter->useTable('widgets')->tableExists()); |
25
|
|
|
$this->adapter->useTable('widgets')->deleteTable(); |
26
|
|
|
$this->assertFalse($this->adapter->useTable('widgets')->tableExists()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testTableExists() |
30
|
|
|
{ |
31
|
|
|
$this->adapter->useTable('widgets')->createTable([ |
32
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
33
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
34
|
|
|
]); |
35
|
|
|
$this->assertFalse($this->adapter->tableExists('nonexistent')); |
36
|
|
|
$this->assertTrue($this->adapter->useTable('nonexistent')->tableExists('widgets')); |
37
|
|
|
$this->adapter->useTable('widgets')->deleteTable(); |
38
|
|
|
$this->assertFalse($this->adapter->tableExists('widgets')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testBadCreateTable() |
42
|
|
|
{ |
43
|
|
|
$this->expectException(DbException::class); |
44
|
|
|
$this->adapter->useTable('te\st')->createTable(['name' => ['attributeType' => 'S', 'keyType' => 'HASH']]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testBadCreateTableBadKeySchema() |
48
|
|
|
{ |
49
|
|
|
$this->expectException(DbException::class); |
50
|
|
|
$this->adapter->useTable('test')->createTable([]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testBadDeleteTable() |
54
|
|
|
{ |
55
|
|
|
$this->expectException(DbException::class); |
56
|
|
|
$this->adapter->useTable('test')->deleteTable(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testDescribeTable() |
60
|
|
|
{ |
61
|
|
|
$this->adapter->useTable('test')->createTable(['name' => ['attributeType' => 'S', 'keyType' => 'HASH']]); |
62
|
|
|
$results = $this->adapter->useTable('test')->describeTable(); |
63
|
|
|
$this->assertSame(5, $results['ProvisionedThroughput']['ReadCapacityUnits']); |
64
|
|
|
$this->adapter->useTable('test')->deleteTable(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testBadDescribeTable() |
68
|
|
|
{ |
69
|
|
|
$this->expectException(DbException::class); |
70
|
|
|
$this->adapter->useTable('juniper')->describeTable(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testBadFindAll() |
74
|
|
|
{ |
75
|
|
|
$this->expectException(DbException::class); |
76
|
|
|
$this->adapter->useTable('test')->findAll(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFindAll() |
80
|
|
|
{ |
81
|
|
|
$adapter = $this->adapter->useTable('test'); |
82
|
|
|
$adapter->createTable([ |
83
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
84
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
85
|
|
|
]); |
86
|
|
|
$adapter->insert(['name' => 'Guillermo', 'date' => time()]); |
87
|
|
|
$adapter->insert(['name' => 'Fisher', 'date' => time()]); |
88
|
|
|
$items = $adapter->findAll(); |
89
|
|
|
$adapter->deleteTable(); |
90
|
|
|
$this->assertCount(2, $items); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testBadFindWhere() |
94
|
|
|
{ |
95
|
|
|
$this->expectException(DbException::class); |
96
|
|
|
$this->adapter->useTable('test')->findWhere([]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testFindWhere() |
100
|
|
|
{ |
101
|
|
|
$adapter = $this->adapter->useTable('test'); |
102
|
|
|
$adapter->createTable([ |
103
|
|
|
'firstName' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
104
|
|
|
'age' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
105
|
|
|
]); |
106
|
|
|
$adapter->insert(['firstName' => 'Guillermo', 'hobby' => 'sleeping', 'age' => 40]); |
107
|
|
|
$adapter->insert(['firstName' => 'Guillermo', 'hobby' => 'coding', 'age' => 40]); |
108
|
|
|
$adapter->insert(['firstName' => 'William', 'hobby' => 'drawing', 'age' => 24]); |
109
|
|
|
$adapter->insert(['firstName' => 'William', 'hobby' => 'playing', 'age' => 15]); |
110
|
|
|
$adapter->insert(['firstName' => 'William', 'hobby' => 'writing', 'age' => 20]); |
111
|
|
|
|
112
|
|
|
$items = $adapter->findWhere([ |
113
|
|
|
'partition' => [ |
114
|
|
|
'name' => 'firstName', |
115
|
|
|
'value' => 'William' |
116
|
|
|
], |
117
|
|
|
'sort' => [ |
118
|
|
|
'name' => 'age', |
119
|
|
|
'operator' => RequestOperators::GTE, |
120
|
|
|
'value' => 16 |
121
|
|
|
] |
122
|
|
|
]); |
123
|
|
|
$adapter->deleteTable(); |
124
|
|
|
$this->assertCount(2, $items); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testBadFindLatest() |
128
|
|
|
{ |
129
|
|
|
$this->expectException(DbException::class); |
130
|
|
|
$this->adapter->useTable('test')->findLatest(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testFindLatest() |
134
|
|
|
{ |
135
|
|
|
$adapter = $this->adapter->useTable('test'); |
136
|
|
|
$adapter->createTable([ |
137
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
138
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
139
|
|
|
]); |
140
|
|
|
$adapter->insert(['name' => 'Guillermo', 'date' => time()]); |
141
|
|
|
$adapter->insert(['name' => 'Fisher', 'date' => time()]); |
142
|
|
|
$item = $adapter->findLatest(); |
143
|
|
|
$adapter->deleteTable(); |
144
|
|
|
$this->assertSame($item['name'], 'Fisher'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testBadFindById() |
148
|
|
|
{ |
149
|
|
|
$this->expectException(DbException::class); |
150
|
|
|
$this->adapter->useTable('test')->findById([]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testFindById() |
154
|
|
|
{ |
155
|
|
|
$adapter = $this->adapter->useTable('test'); |
156
|
|
|
$adapter->createTable([ |
157
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
158
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
159
|
|
|
]); |
160
|
|
|
$key = ['name' => 'Guillermo', 'date' => time()]; |
161
|
|
|
$adapter->insert(array_merge(['name' => 'Guillermo', 'date' => time(), 'lastName' => 'Fisher'])); |
162
|
|
|
$item = $adapter->findById($key); |
163
|
|
|
$this->assertSame($item['lastName'], 'Fisher'); |
164
|
|
|
$adapter->deleteTable(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testBadInsert() |
168
|
|
|
{ |
169
|
|
|
$this->expectException(DbException::class); |
170
|
|
|
$this->adapter->useTable('test')->insert([]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testBadDelete() |
174
|
|
|
{ |
175
|
|
|
$this->expectException(DbException::class); |
176
|
|
|
$this->adapter->useTable('test')->delete([]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testDelete() |
180
|
|
|
{ |
181
|
|
|
$adapter = $this->adapter->useTable('test'); |
182
|
|
|
$adapter->createTable([ |
183
|
|
|
'name' => ['attributeType' => 'S', 'keyType' => 'HASH'], |
184
|
|
|
'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'], |
185
|
|
|
]); |
186
|
|
|
$key = ['name' => 'Guillermo', 'date' => time()]; |
187
|
|
|
$adapter->insert(array_merge(['name' => 'Guillermo', 'date' => time(), 'lastName' => 'Fisher'])); |
188
|
|
|
$this->assertTrue($adapter->delete($key)); |
189
|
|
|
$this->assertCount(0, $adapter->findAll()); |
190
|
|
|
$adapter->deleteTable(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testGetClient() |
194
|
|
|
{ |
195
|
|
|
$this->assertInstanceOf(DynamoDbClient::class, $this->adapter->getClient()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function setUp(): void |
199
|
|
|
{ |
200
|
|
|
$this->adapter = new DynamoDbAdapter(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
protected function tearDown(): void |
204
|
|
|
{ |
205
|
|
|
$tables = $this->adapter->listTables(); |
206
|
|
|
foreach ($tables as $table) { |
207
|
|
|
$this->adapter->useTable($table)->deleteTable(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|