Completed
Push — master ( 65b043...dd1ab4 )
by Guillermo A.
02:55
created

DynamoDbAdapterTest::testDescribeTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 testFindBatch()
148
    {
149
        $adapter = $this->adapter->useTable('test');
150
        $adapter->createTable([
151
            'name' => [AttributeTypes::STRING, KeyTypes::HASH],
152
            'date' => [AttributeTypes::NUMBER, KeyTypes::RANGE],
153
        ]);
154
        $timestamp1 = time();
155
        $timestamp2 = strtotime('tomorrow');
156
        $adapter->insert(['name' => 'Guillermo', 'date' => $timestamp1, 'lastName' => 'Fisher']);
157
        $adapter->insert(['name' => 'Andrae', 'date' => $timestamp2, 'lastName' => 'Fisher']);
158
        $adapter->insert(['name' => 'Guillermo', 'date' => time(), 'lastName' => 'Fisher']);
159
        $items = $adapter->find([
160
            'test' => [
161
                ['name' => 'Guillermo', 'date' => $timestamp1],
162
                ['name' => 'Andrae', 'date' => $timestamp2],
163
            ]
164
        ]);
165
        $this->assertEquals($items[0]['lastName'], $items[1]['lastName']);
166
        $this->assertNotEquals($items[0]['name'], $items[1]['name']);
167
        $adapter->deleteTable();
168
    }
169
170
    public function testBadFindBatch()
171
    {
172
        $this->expectException(Exception::class);
173
        $this->adapter->useTable('test')->find([
174
            'test' => [
175
                ['name' => 'Guillermo', 'date' => time()],
176
                ['name' => 'Andrae', 'date' => time()],
177
            ]
178
        ]);
179
    }
180
181
    public function testBadInsert()
182
    {
183
        $this->expectException(Exception::class);
184
        $this->adapter->useTable('test')->insert([]);
185
    }
186
187
    public function testBadUpdate()
188
    {
189
        $this->expectException(Exception::class);
190
        $this->adapter->useTable('test')->update([], []);
191
    }
192
193
    public function testUpdate()
194
    {
195
        $adapter = $this->adapter->useTable('test');
196
        $adapter->createTable([
197
            'name' => [AttributeTypes::STRING, KeyTypes::HASH],
198
            'date' => [AttributeTypes::NUMBER, KeyTypes::RANGE],
199
        ]);
200
        $timestamp = time();
201
        $expectedLastName = 'del Toro';
202
        $primaryKey = ['name' => 'Guillermo', 'date' => $timestamp];
203
        $adapter->insert(array_merge($primaryKey, ['lastName' => 'Fisher']));
204
        $adapter->update($primaryKey, [
205
            'lastName' => [
206
                'operator' => Operators::EQ,
207
                'value' => $expectedLastName,
208
            ]
209
        ]);
210
        $this->assertEquals($expectedLastName, $adapter->find($primaryKey)['lastName']);
211
    }
212
213
    public function testBadDelete()
214
    {
215
        $this->expectException(Exception::class);
216
        $this->adapter->useTable('test')->delete([]);
217
    }
218
219
    public function testDelete()
220
    {
221
        $adapter = $this->adapter->useTable('test');
222
        $adapter->createTable([
223
            'name' => [AttributeTypes::STRING, KeyTypes::HASH],
224
            'date' => [AttributeTypes::NUMBER, KeyTypes::RANGE],
225
        ]);
226
        $key = ['name' => 'Guillermo', 'date' => time()];
227
        $adapter->insert(array_merge(['name' => 'Guillermo', 'date' => time(), 'lastName' => 'Fisher']));
228
        $this->assertTrue($adapter->delete($key));
229
        $this->assertCount(0, $adapter->findAll());
230
        $adapter->deleteTable();
231
    }
232
233
    public function testGetClient()
234
    {
235
        $this->assertInstanceOf(DynamoDbClient::class, $this->adapter->getClient());
236
    }
237
}
238