1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GuillermoandraeTest\DynamoDb\Contract; |
4
|
|
|
|
5
|
|
|
use Guillermoandrae\DynamoDb\Constant\Operators; |
6
|
|
|
use Guillermoandrae\DynamoDb\Contract\FilterExpressionAwareOperationTrait; |
7
|
|
|
use Guillermoandrae\DynamoDb\Factory\MarshalerFactory; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
final class FilterExpressionAwareOperationTraitTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var FilterExpressionAwareOperationTrait The request. |
14
|
|
|
*/ |
15
|
|
|
private $request; |
16
|
|
|
|
17
|
|
|
public function testFilterExpressionGT() |
18
|
|
|
{ |
19
|
|
|
$expectedExpression = 'width > :width'; |
20
|
|
|
$this->request->setFilterExpression([ |
21
|
|
|
'width' => [ |
22
|
|
|
'operator' => Operators::GT, |
23
|
|
|
'value' => '10', |
24
|
|
|
] |
25
|
|
|
]); |
26
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testFilterExpressionGTE() |
30
|
|
|
{ |
31
|
|
|
$expectedExpression = 'width >= :width'; |
32
|
|
|
$this->request->setFilterExpression([ |
33
|
|
|
'width' => [ |
34
|
|
|
'operator' => Operators::GTE, |
35
|
|
|
'value' => '10', |
36
|
|
|
] |
37
|
|
|
]); |
38
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testFilterExpressionLT() |
42
|
|
|
{ |
43
|
|
|
$expectedExpression = 'width < :width'; |
44
|
|
|
$this->request->setFilterExpression([ |
45
|
|
|
'width' => [ |
46
|
|
|
'operator' => Operators::LT, |
47
|
|
|
'value' => '10', |
48
|
|
|
] |
49
|
|
|
]); |
50
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFilterExpressionLTE() |
54
|
|
|
{ |
55
|
|
|
$expectedExpression = 'width <= :width'; |
56
|
|
|
$this->request->setFilterExpression([ |
57
|
|
|
'width' => [ |
58
|
|
|
'operator' => Operators::LTE, |
59
|
|
|
'value' => '10', |
60
|
|
|
] |
61
|
|
|
]); |
62
|
|
|
$this->assertEquals($expectedExpression, $this->request->toArray()['FilterExpression']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFilterExpressionBadOperator() |
66
|
|
|
{ |
67
|
|
|
$this->expectException(\ErrorException::class); |
68
|
|
|
$this->request->setFilterExpression([ |
69
|
|
|
'width' => [ |
70
|
|
|
'operator' => 'TEST', |
71
|
|
|
'value' => '10', |
72
|
|
|
] |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testSetFilterExpressionAndExpressionAttributeValues() |
77
|
|
|
{ |
78
|
|
|
$this->request->setFilterExpression([ |
79
|
|
|
'color' => [ |
80
|
|
|
'operator' => Operators::EQ, |
81
|
|
|
'value' => 'black', |
82
|
|
|
], |
83
|
|
|
'shape' => [ |
84
|
|
|
'operator' => Operators::CONTAINS, |
85
|
|
|
'value' => 'square' |
86
|
|
|
], |
87
|
|
|
'width' => [ |
88
|
|
|
'operator' => Operators::GTE, |
89
|
|
|
'value' => 10 |
90
|
|
|
] |
91
|
|
|
]); |
92
|
|
|
$expectedQuery = [ |
93
|
|
|
'FilterExpression' => 'color = :color and contains(shape, :shape) and width >= :width', |
94
|
|
|
'ExpressionAttributeValues' => [ |
95
|
|
|
':color' => [ |
96
|
|
|
'S' => 'black' |
97
|
|
|
], |
98
|
|
|
':shape' => [ |
99
|
|
|
'S' => 'square', |
100
|
|
|
], |
101
|
|
|
':width' => [ |
102
|
|
|
'N' => 10 |
103
|
|
|
] |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
$this->assertEquals($expectedQuery, $this->request->toArray()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function setUp(): void |
110
|
|
|
{ |
111
|
|
|
$this->request = $this->getMockForTrait( |
|
|
|
|
112
|
|
|
FilterExpressionAwareOperationTrait::class |
113
|
|
|
); |
114
|
|
|
$this->request->setMarshaler(MarshalerFactory::factory()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function tearDown(): void |
118
|
|
|
{ |
119
|
|
|
$this->request = null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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..