QueryOperationTest::testSetProjectionExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GuillermoandraeTest\DynamoDb\Operation;
4
5
use Guillermoandrae\DynamoDb\Constant\Operators;
6
use Guillermoandrae\DynamoDb\Constant\ReturnConsumedCapacityOptions;
7
use Guillermoandrae\DynamoDb\Constant\Select;
8
use Guillermoandrae\DynamoDb\Factory\DynamoDbClientFactory;
9
use Guillermoandrae\DynamoDb\Factory\MarshalerFactory;
10
use Guillermoandrae\DynamoDb\Operation\QueryOperation;
11
use PHPUnit\Framework\TestCase;
12
13
final class QueryOperationTest extends TestCase
14
{
15
    /**
16
     * @var QueryOperation The operation.
17
     */
18
    private $operation;
19
20
    public function testSetLimit()
21
    {
22
        $expectedLimit = 50;
23
        $this->operation->setLimit($expectedLimit);
24
        $this->assertEquals($expectedLimit, $this->operation->toArray()['Limit']);
25
    }
26
27
    public function testPagination()
28
    {
29
        $offset = 5;
30
        $expectedLimit = 50;
31
        $expectedPaginationLimit = $offset + $expectedLimit;
32
        $this->operation->setOffset($offset);
33
        $this->operation->setLimit($expectedLimit);
34
        $this->assertEquals($expectedPaginationLimit, $this->operation->toArray()['Limit']);
35
    }
36
37
    public function testFilterExpressionGT()
38
    {
39
        $expectedExpression = 'width > :width';
40
        $this->operation->setExpression([
41
            'width' => [
42
                'operator' => Operators::GT,
43
                'value' => '10',
44
            ]
45
        ]);
46
        $this->assertEquals($expectedExpression, $this->operation->toArray()['FilterExpression']);
47
    }
48
49
    public function testFilterExpressionGTE()
50
    {
51
        $expectedExpression = 'width >= :width';
52
        $this->operation->setExpression([
53
            'width' => [
54
                'operator' => Operators::GTE,
55
                'value' => '10',
56
            ]
57
        ]);
58
        $this->assertEquals($expectedExpression, $this->operation->toArray()['FilterExpression']);
59
    }
60
61
    public function testFilterExpressionLT()
62
    {
63
        $expectedExpression = 'width < :width';
64
        $this->operation->setExpression([
65
            'width' => [
66
                'operator' => Operators::LT,
67
                'value' => '10',
68
            ]
69
        ]);
70
        $this->assertEquals($expectedExpression, $this->operation->toArray()['FilterExpression']);
71
    }
72
73
    public function testFilterExpressionLTE()
74
    {
75
        $expectedExpression = 'width <= :width';
76
        $this->operation->setExpression([
77
            'width' => [
78
                'operator' => Operators::LTE,
79
                'value' => '10',
80
            ]
81
        ]);
82
        $this->assertEquals($expectedExpression, $this->operation->toArray()['FilterExpression']);
83
    }
84
85
    public function testFilterExpressionBadOperator()
86
    {
87
        $this->expectException(\ErrorException::class);
88
        $this->operation->setExpression([
89
            'width' => [
90
                'operator' => 'TEST',
91
                'value' => '10',
92
            ]
93
        ]);
94
    }
95
96
    public function testSetFilterExpressionAndExpressionAttributeValues()
97
    {
98
        $this->operation->setExpression([
99
            'color' => [
100
                'operator' => Operators::EQ,
101
                'value' => 'black',
102
            ],
103
            'shape' => [
104
                'operator' => Operators::CONTAINS,
105
                'value' => 'square'
106
            ],
107
            'width' => [
108
                'operator' => Operators::GTE,
109
                'value' => 10
110
            ]
111
        ]);
112
        $expectedFilterExpression = 'color = :color and contains(shape, :shape) and width >= :width';
113
        $expectedExpressionAttributeValues = [
114
            ':color' => [
115
                'S' => 'black'
116
            ],
117
            ':shape' => [
118
                'S' => 'square',
119
            ],
120
            ':width' => [
121
                'N' => 10
122
            ]
123
        ];
124
        $this->assertEquals($expectedFilterExpression, $this->operation->toArray()['FilterExpression']);
125
        $this->assertEquals(
126
            $expectedExpressionAttributeValues,
127
            $this->operation->toArray()['ExpressionAttributeValues']
128
        );
129
    }
130
131
    public function testSetReturnConsumedCapacity()
132
    {
133
        $expectedReturnConsumedCapacity = ReturnConsumedCapacityOptions::INDEXES;
134
        $this->operation->setReturnConsumedCapacity($expectedReturnConsumedCapacity);
135
        $this->assertEquals($expectedReturnConsumedCapacity, $this->operation->toArray()['ReturnConsumedCapacity']);
136
    }
137
138
    public function testSetScanIndexForward()
139
    {
140
        $this->operation->setScanIndexForward(true);
141
        $expectedQuery = [
142
            'TableName' => 'test',
143
            'ScanIndexForward' => true,
144
            'ConsistentRead' => false,
145
            'ReturnConsumedCapacity' => ReturnConsumedCapacityOptions::NONE
146
        ];
147
        $this->assertEquals($expectedQuery, $this->operation->toArray());
148
    }
149
150
    public function testSetConsistentRead()
151
    {
152
        $this->operation->setConsistentRead(true);
153
        $expectedQuery = [
154
            'TableName' => 'test',
155
            'ScanIndexForward' => false,
156
            'ConsistentRead' => true,
157
            'ReturnConsumedCapacity' => 'NONE'
158
        ];
159
        $this->assertEquals($expectedQuery, $this->operation->toArray());
160
    }
161
162
    public function testSetIndexName()
163
    {
164
        $this->operation->setIndexName('test');
165
        $this->assertEquals('test', $this->operation->toArray()['IndexName']);
166
    }
167
168
    public function testSetSelect()
169
    {
170
        $this->operation->setSelect(Select::ALL_ATTRIBUTES);
171
        $this->assertEquals('ALL_ATTRIBUTES', $this->operation->toArray()['Select']);
172
    }
173
174
    public function testSetProjectionExpression()
175
    {
176
        $this->operation->setProjectionExpression('test');
177
        $this->assertEquals('test', $this->operation->toArray()['ProjectionExpression']);
178
    }
179
180
    public function testSetPartitionKeyConditionExpression()
181
    {
182
        $this->operation->setPartitionKeyConditionExpression('test', 'something');
183
        $requestArray = $this->operation->toArray();
184
        $this->assertEquals('test = :test', $requestArray['KeyConditionExpression']);
185
        $this->assertEquals(['S' => 'something'], $requestArray['ExpressionAttributeValues'][':test']);
186
    }
187
188
    public function testSetSortKeyConditionExpression()
189
    {
190
        $this->operation->setPartitionKeyConditionExpression('test', 'something');
191
        $this->operation->setSortKeyConditionExpression('anotherTest', Operators::BEGINS_WITH, 'somethingElse');
192
        $requestArray = $this->operation->toArray();
193
        $this->assertEquals(
194
            'test = :test AND begins_with(anotherTest, :anotherTest)',
195
            $requestArray['KeyConditionExpression']
196
        );
197
        $this->assertEquals(['S' => 'something'], $requestArray['ExpressionAttributeValues'][':test']);
198
        $this->assertEquals(['S' => 'somethingElse'], $requestArray['ExpressionAttributeValues'][':anotherTest']);
199
    }
200
201
    protected function setUp(): void
202
    {
203
        $this->operation = new QueryOperation(DynamoDbClientFactory::factory(), MarshalerFactory::factory(), 'test');
204
    }
205
206
    protected function tearDown(): void
207
    {
208
        unset($this->operation);
209
    }
210
}
211