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