1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GuillermoandraeTest\DynamoDb\Contract; |
4
|
|
|
|
5
|
|
|
use Guillermoandrae\DynamoDb\Constant\Operators; |
6
|
|
|
use Guillermoandrae\DynamoDb\Contract\AbstractFilterExpressionAwareOperation; |
7
|
|
|
use Guillermoandrae\DynamoDb\Factory\DynamoDbClientFactory; |
8
|
|
|
use Guillermoandrae\DynamoDb\Factory\MarshalerFactory; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class FilterExpressionAwareOperationTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var AbstractFilterExpressionAwareOperation The request. |
15
|
|
|
*/ |
16
|
|
|
private $request; |
17
|
|
|
|
18
|
|
|
public function testFilterExpressionGT() |
19
|
|
|
{ |
20
|
|
|
$expectedExpression = 'width > :width'; |
21
|
|
|
$this->request->setFilterExpression([ |
22
|
|
|
'width' => [ |
23
|
|
|
'operator' => Operators::GT, |
24
|
|
|
'value' => '10', |
25
|
|
|
] |
26
|
|
|
]); |
27
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testFilterExpressionGTE() |
31
|
|
|
{ |
32
|
|
|
$expectedExpression = 'width >= :width'; |
33
|
|
|
$this->request->setFilterExpression([ |
34
|
|
|
'width' => [ |
35
|
|
|
'operator' => Operators::GTE, |
36
|
|
|
'value' => '10', |
37
|
|
|
] |
38
|
|
|
]); |
39
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testFilterExpressionLT() |
43
|
|
|
{ |
44
|
|
|
$expectedExpression = 'width < :width'; |
45
|
|
|
$this->request->setFilterExpression([ |
46
|
|
|
'width' => [ |
47
|
|
|
'operator' => Operators::LT, |
48
|
|
|
'value' => '10', |
49
|
|
|
] |
50
|
|
|
]); |
51
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testFilterExpressionLTE() |
55
|
|
|
{ |
56
|
|
|
$expectedExpression = 'width <= :width'; |
57
|
|
|
$this->request->setFilterExpression([ |
58
|
|
|
'width' => [ |
59
|
|
|
'operator' => Operators::LTE, |
60
|
|
|
'value' => '10', |
61
|
|
|
] |
62
|
|
|
]); |
63
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testFilterExpressionBadOperator() |
67
|
|
|
{ |
68
|
|
|
$this->expectException(\ErrorException::class); |
69
|
|
|
$this->request->setFilterExpression([ |
70
|
|
|
'width' => [ |
71
|
|
|
'operator' => 'TEST', |
72
|
|
|
'value' => '10', |
73
|
|
|
] |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testSetFilterExpressionAndExpressionAttributeValues() |
78
|
|
|
{ |
79
|
|
|
$this->request->setFilterExpression([ |
80
|
|
|
'color' => [ |
81
|
|
|
'operator' => Operators::EQ, |
82
|
|
|
'value' => 'black', |
83
|
|
|
], |
84
|
|
|
'shape' => [ |
85
|
|
|
'operator' => Operators::CONTAINS, |
86
|
|
|
'value' => 'square' |
87
|
|
|
], |
88
|
|
|
'width' => [ |
89
|
|
|
'operator' => Operators::GTE, |
90
|
|
|
'value' => 10 |
91
|
|
|
] |
92
|
|
|
]); |
93
|
|
|
$expectedQuery = [ |
94
|
|
|
'TableName' => 'test', |
95
|
|
|
'ReturnConsumedCapacity' => 'NONE', |
96
|
|
|
'FilterExpression' => 'color = :color and contains(shape, :shape) and width >= :width', |
97
|
|
|
'ExpressionAttributeValues' => [ |
98
|
|
|
':color' => [ |
99
|
|
|
'S' => 'black' |
100
|
|
|
], |
101
|
|
|
':shape' => [ |
102
|
|
|
'S' => 'square', |
103
|
|
|
], |
104
|
|
|
':width' => [ |
105
|
|
|
'N' => 10 |
106
|
|
|
] |
107
|
|
|
], |
108
|
|
|
]; |
109
|
|
|
$this->assertEquals($expectedQuery, $this->request->toArray()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testSetReturnConsumedCapacity() |
113
|
|
|
{ |
114
|
|
|
$this->request->setReturnConsumedCapacity('INDEXES'); |
115
|
|
|
$expectedQuery = [ |
116
|
|
|
'TableName' => 'test', |
117
|
|
|
'ReturnConsumedCapacity' => 'INDEXES' |
118
|
|
|
]; |
119
|
|
|
$this->assertEquals($expectedQuery, $this->request->toArray()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testSetLimit() |
123
|
|
|
{ |
124
|
|
|
$this->request->setLimit(2); |
125
|
|
|
$expectedQuery = [ |
126
|
|
|
'TableName' => 'test', |
127
|
|
|
'ReturnConsumedCapacity' => 'NONE', |
128
|
|
|
'Limit' => 2 |
129
|
|
|
]; |
130
|
|
|
$this->assertEquals($expectedQuery, $this->request->toArray()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function setUp(): void |
134
|
|
|
{ |
135
|
|
|
$this->request = $this->getMockForAbstractClass( |
|
|
|
|
136
|
|
|
AbstractFilterExpressionAwareOperation::class, |
137
|
|
|
[DynamoDbClientFactory::factory(), MarshalerFactory::factory(), 'test'] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function tearDown(): void |
142
|
|
|
{ |
143
|
|
|
$this->request = null; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..