Completed
Push — master ( 1b95ce...a8eec6 )
by Guillermo A.
02:21
created

CreateTableOperationTest::testSSESpecification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GuillermoandraeTest\DynamoDb\Operation;
4
5
use Guillermoandrae\DynamoDb\Constant\AttributeTypes;
6
use Guillermoandrae\DynamoDb\Constant\BillingModes;
7
use Guillermoandrae\DynamoDb\Constant\KeyTypes;
8
use Guillermoandrae\DynamoDb\Constant\ProjectionTypes;
9
use Guillermoandrae\DynamoDb\Factory\DynamoDbClientFactory;
10
use Guillermoandrae\DynamoDb\Factory\MarshalerFactory;
11
use Guillermoandrae\DynamoDb\Operation\CreateTableOperation;
12
use GuillermoandraeTest\DynamoDb\TestCase;
13
14
final class CreateTableOperationTest extends TestCase
15
{
16
    /**
17
     * @var string The table name;
18
     */
19
    private $tableName = 'test';
20
21
    /**
22
     * @var array The primary key.
23
     */
24
    private $keySchema = ['name' => [AttributeTypes::STRING, KeyTypes::HASH]];
25
26
    public function testSetPartitionKey()
27
    {
28
        $operation = new CreateTableOperation(
29
            DynamoDbClientFactory::factory(),
30
            MarshalerFactory::factory(),
31
            $this->tableName
32
        );
33
        $operation->setPartitionKey('test', AttributeTypes::STRING);
34
        $this->assertEquals(KeyTypes::HASH, $operation->toArray()['KeySchema'][0]['KeyType']);
35
    }
36
37
    public function testSetSortKey()
38
    {
39
        $operation = new CreateTableOperation(
40
            DynamoDbClientFactory::factory(),
41
            MarshalerFactory::factory(),
42
            $this->tableName
43
        );
44
        $operation->setSortKey('test', AttributeTypes::STRING);
45
        $this->assertEquals(KeyTypes::RANGE, $operation->toArray()['KeySchema'][0]['KeyType']);
46
    }
47
48
    public function testSetReadCapacityUnits()
49
    {
50
        $operation = new CreateTableOperation(
51
            DynamoDbClientFactory::factory(),
52
            MarshalerFactory::factory(),
53
            $this->tableName
54
        );
55
        $operation->setReadCapacityUnits(10);
56
        $this->assertEquals(10, $operation->toArray()['ProvisionedThroughput']['ReadCapacityUnits']);
57
    }
58
    
59
    public function testWriteCapacityUnits()
60
    {
61
        $operation = new CreateTableOperation(
62
            DynamoDbClientFactory::factory(),
63
            MarshalerFactory::factory(),
64
            $this->tableName
65
        );
66
        $operation->setWriteCapacityUnits(20);
67
        $this->assertEquals(20, $operation->toArray()['ProvisionedThroughput']['WriteCapacityUnits']);
68
    }
69
70
    public function testSetBillingMode()
71
    {
72
        $operation = new CreateTableOperation(
73
            DynamoDbClientFactory::factory(),
74
            MarshalerFactory::factory(),
75
            $this->tableName
76
        );
77
        $operation->setBillingMode(BillingModes::PAY_PER_REQUEST);
78
        $this->assertEquals(BillingModes::PAY_PER_REQUEST, $operation->toArray()['BillingMode']);
79
    }
80
81
    public function testSSESpecification()
82
    {
83
        $operation = new CreateTableOperation(
84
            DynamoDbClientFactory::factory(),
85
            MarshalerFactory::factory(),
86
            $this->tableName
87
        );
88
        $operation->setSSESpecification(true, 'someKey');
89
        $this->assertEquals('someKey', $operation->toArray()['SSESpecification']['KMSMasterKeyId']);
90
        $operation->setSSESpecification(false);
91
        $this->assertArrayNotHasKey('SSESpecification', $operation->toArray());
92
    }
93
94
    public function testAddGlobalSecondaryIndex()
95
    {
96
        $operation = new CreateTableOperation(
97
            DynamoDbClientFactory::factory(),
98
            MarshalerFactory::factory(),
99
            $this->tableName
100
        );
101
        $expectedIndexName = 'test';
102
        $expectedKeySchema = [['class', KeyTypes::HASH]];
103
        $expectedProjection = ['thing', ProjectionTypes::INCLUDE];
104
        $operation->addGlobalSecondaryIndex($expectedIndexName, $expectedKeySchema, $expectedProjection);
105
        $request = $operation->toArray()['GlobalSecondaryIndexes'][0];
106
        $this->assertEquals($expectedIndexName, $request['IndexName']);
107
        $this->assertEquals($expectedKeySchema[0][0], $request['KeySchema'][0]['AttributeName']);
108
        $this->assertEquals($expectedKeySchema[0][1], $request['KeySchema'][0]['KeyType']);
109
        $this->assertEquals($expectedProjection[0], $request['Projection']['NonKeyAttributes']);
110
        $this->assertEquals($expectedProjection[1], $request['Projection']['ProjectionType']);
111
    }
112
113
    public function testSetGlobalSecondaryIndexWithProvisionedThroughput()
114
    {
115
        $operation = new CreateTableOperation(
116
            DynamoDbClientFactory::factory(),
117
            MarshalerFactory::factory(),
118
            $this->tableName
119
        );
120
        $expectedIndexName = 'test';
121
        $expectedKeySchema = [['class', KeyTypes::HASH]];
122
        $expectedProjection = ['thing', ProjectionTypes::INCLUDE];
123
        $expectedProvisionedThroughput = [5, 5];
124
        $operation->addGlobalSecondaryIndex(
125
            $expectedIndexName,
126
            $expectedKeySchema,
127
            $expectedProjection,
128
            $expectedProvisionedThroughput
129
        );
130
        $this->assertEquals(
131
            $expectedProvisionedThroughput[0],
132
            $operation->toArray()['GlobalSecondaryIndexes'][0]['ProvisionedThroughput']['ReadCapacityUnits']
133
        );
134
        $this->assertEquals(
135
            $expectedProvisionedThroughput[1],
136
            $operation->toArray()['GlobalSecondaryIndexes'][0]['ProvisionedThroughput']['WriteCapacityUnits']
137
        );
138
    }
139
140
    public function testAddLocalSecondaryIndex()
141
    {
142
        $operation = new CreateTableOperation(
143
            DynamoDbClientFactory::factory(),
144
            MarshalerFactory::factory(),
145
            $this->tableName
146
        );
147
        $expectedIndexName = 'test';
148
        $expectedKeySchema = [['class', KeyTypes::HASH]];
149
        $expectedProjection = ['thing', ProjectionTypes::INCLUDE];
150
        $operation->addLocalSecondaryIndex($expectedIndexName, $expectedKeySchema, $expectedProjection);
151
        $request = $operation->toArray()['LocalSecondaryIndexes'][0];
152
        $this->assertEquals($expectedIndexName, $request['IndexName']);
153
        $this->assertEquals($expectedKeySchema[0][0], $request['KeySchema'][0]['AttributeName']);
154
        $this->assertEquals($expectedKeySchema[0][1], $request['KeySchema'][0]['KeyType']);
155
        $this->assertEquals($expectedProjection[0], $request['Projection']['NonKeyAttributes']);
156
        $this->assertEquals($expectedProjection[1], $request['Projection']['ProjectionType']);
157
    }
158
159
    public function testAddTag()
160
    {
161
        $operation = new CreateTableOperation(
162
            DynamoDbClientFactory::factory(),
163
            MarshalerFactory::factory(),
164
            $this->tableName,
165
            $this->keySchema
166
        );
167
        $operation->addTag('someKey', 'someValue');
168
        $operation->addTag('anotherKey', 'anotherValue');
169
        $this->assertEquals('someValue', $operation->toArray()['Tags'][0]['Value']);
170
        $this->assertEquals('anotherValue', $operation->toArray()['Tags'][1]['Value']);
171
    }
172
}
173