Completed
Push — master ( a7a349...3fc402 )
by Guillermo A.
03:23
created

DynamoDbAdapterTest::testFindWhere()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 26
c 0
b 0
f 0
rs 9.6333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GuillermoandraeTest\DynamoDb;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use Guillermoandrae\DynamoDb\Constant\Operators;
7
use Guillermoandrae\DynamoDb\DynamoDbAdapter;
8
use Guillermoandrae\DynamoDb\Exception;
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(Exception::class);
44
        $this->adapter->useTable('te\st')->createTable(['name' => ['attributeType' => 'S', 'keyType' => 'HASH']]);
45
    }
46
47
    public function testBadCreateTableBadKeySchema()
48
    {
49
        $this->expectException(Exception::class);
50
        $this->adapter->useTable('test')->createTable([]);
51
    }
52
53
    public function testBadDeleteTable()
54
    {
55
        $this->expectException(Exception::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(Exception::class);
70
        $this->adapter->useTable('juniper')->describeTable();
71
    }
72
73
    public function testBadFindAll()
74
    {
75
        $this->expectException(Exception::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(Exception::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' => Operators::GTE,
120
                'value' => 16
121
            ]
122
        ]);
123
        $adapter->deleteTable();
124
        $this->assertCount(2, $items);
125
    }
126
127
    public function testBadFind()
128
    {
129
        $this->expectException(Exception::class);
130
        $this->adapter->useTable('test')->find([]);
131
    }
132
133
    public function testFind()
134
    {
135
        $adapter = $this->adapter->useTable('test');
136
        $adapter->createTable([
137
            'name' => ['attributeType' => 'S', 'keyType' => 'HASH'],
138
            'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'],
139
        ]);
140
        $timestamp = time();
141
        $key = ['name' => 'Guillermo', 'date' => $timestamp];
142
        $adapter->insert(array_merge($key, ['lastName' => 'Fisher']));
143
        $item = $adapter->find($key);
144
        $this->assertSame($item['lastName'], 'Fisher');
145
        $adapter->deleteTable();
146
    }
147
148
    public function testBadInsert()
149
    {
150
        $this->expectException(Exception::class);
151
        $this->adapter->useTable('test')->insert([]);
152
    }
153
154
    public function testBadDelete()
155
    {
156
        $this->expectException(Exception::class);
157
        $this->adapter->useTable('test')->delete([]);
158
    }
159
160
    public function testDelete()
161
    {
162
        $adapter = $this->adapter->useTable('test');
163
        $adapter->createTable([
164
            'name' => ['attributeType' => 'S', 'keyType' => 'HASH'],
165
            'date' => ['attributeType' => 'N', 'keyType' => 'RANGE'],
166
        ]);
167
        $key = ['name' => 'Guillermo', 'date' => time()];
168
        $adapter->insert(array_merge(['name' => 'Guillermo', 'date' => time(), 'lastName' => 'Fisher']));
169
        $this->assertTrue($adapter->delete($key));
170
        $this->assertCount(0, $adapter->findAll());
171
        $adapter->deleteTable();
172
    }
173
174
    public function testGetClient()
175
    {
176
        $this->assertInstanceOf(DynamoDbClient::class, $this->adapter->getClient());
177
    }
178
179
    protected function setUp(): void
180
    {
181
        $this->adapter = new DynamoDbAdapter();
182
    }
183
184
    protected function tearDown(): void
185
    {
186
        $tables = $this->adapter->listTables();
187
        foreach ($tables as $table) {
188
            $this->adapter->useTable($table)->deleteTable();
189
        }
190
    }
191
}
192