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