Completed
Push — master ( d500cb...2a8cc2 )
by Guillermo A.
01:59
created

QueryOperationTest::testFilterExpressionGT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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