Completed
Push — master ( ebeab2...e57d07 )
by Guillermo A.
02:03
created

DynamoDbAdapterTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 20
eloc 79
c 3
b 1
f 0
dl 0
loc 164
rs 10

19 Methods

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