1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\AbstractNode; |
8
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintAndx; |
9
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintComparison; |
10
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintFieldIsset; |
11
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintNot; |
12
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintOrx; |
13
|
|
|
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\ExpressionVisitor; |
14
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
15
|
|
|
use Psi\Component\ObjectAgent\Tests\Functional\Model\Page; |
16
|
|
|
|
17
|
|
|
class ExpressionVisitorTest extends PhpcrOdmTestCase |
18
|
|
|
{ |
19
|
|
|
private $queryBuilder; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
$container = $this->getContainer(); |
24
|
|
|
$this->queryBuilder = $container->get('phpcr_odm') |
25
|
|
|
->getRepository(Page::class)->createQueryBuilder('a'); |
26
|
|
|
|
27
|
|
|
$this->visitor = new ExpressionVisitor($this->queryBuilder, 'a'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* It should visit all comparators. |
32
|
|
|
* |
33
|
|
|
* @dataProvider provideComparator |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function testComparator(string $type, string $expectedOperator) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$this->visitor->dispatch(Query::comparison($type, 'title', 42)); |
38
|
|
|
$query = $this->queryBuilder; |
39
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
40
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
41
|
|
|
$this->assertEquals($expectedOperator, $children[0]->getOperator()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function provideComparator() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
[ |
48
|
|
|
'eq', |
49
|
|
|
'jcr.operator.equal.to', |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
'neq', |
53
|
|
|
'jcr.operator.not.equal.to', |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'gt', |
57
|
|
|
'jcr.operator.greater.than', |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'gte', |
61
|
|
|
'jcr.operator.greater.than.or.equal.to', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'lte', |
65
|
|
|
'jcr.operator.less.than.or.equal.to', |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
'lt', |
69
|
|
|
'jcr.operator.less.than', |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'contains', |
73
|
|
|
'jcr.operator.like', |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* It should visit complex comparators. |
80
|
|
|
* |
81
|
|
|
* @dataProvider provideComplexComparator |
82
|
|
|
*/ |
83
|
|
|
public function testComplexComparator(string $type, $value, \Closure $assertion) |
84
|
|
|
{ |
85
|
|
|
$this->visitor->dispatch(Query::comparison($type, 'title', $value)); |
86
|
|
|
$query = $this->queryBuilder; |
87
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
88
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
89
|
|
|
$assertion($children); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function provideComplexComparator() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
[ |
96
|
|
|
'in', |
97
|
|
|
[10, 20, 30], |
98
|
|
|
function ($nodes) { |
99
|
|
|
$this->assertInstanceOf(ConstraintOrx::class, $nodes[0]); |
100
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
101
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $nodes[0]); |
102
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $nodes[1]); |
103
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $nodes[2]); |
104
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_OPERAND_STATIC); |
105
|
|
|
$this->assertEquals(10, $nodes[0]->getValue()); |
106
|
|
|
}, |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
'nin', |
110
|
|
|
[10, 20, 30], |
111
|
|
|
function ($nodes) { |
112
|
|
|
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]); |
113
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
114
|
|
|
$this->assertInstanceOf(ConstraintOrx::class, $nodes[0]); |
115
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
116
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $nodes[1]); |
117
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $nodes[2]); |
118
|
|
|
}, |
119
|
|
|
], |
120
|
|
|
[ |
121
|
|
|
'not_contains', |
122
|
|
|
'hello', |
123
|
|
View Code Duplication |
function ($nodes) { |
|
|
|
|
124
|
|
|
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]); |
125
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
126
|
|
|
$this->assertEquals('jcr.operator.like', $nodes[0]->getOperator()); |
127
|
|
|
}, |
128
|
|
|
], |
129
|
|
|
[ |
130
|
|
|
'null', |
131
|
|
|
null, |
132
|
|
View Code Duplication |
function ($nodes) { |
|
|
|
|
133
|
|
|
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]); |
134
|
|
|
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
135
|
|
|
$this->assertInstanceOf(ConstraintFieldIsset::class, $nodes[0]); |
136
|
|
|
}, |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
'not_null', |
140
|
|
|
null, |
141
|
|
|
function ($nodes) { |
142
|
|
|
$this->assertInstanceOf(ConstraintFieldIsset::class, $nodes[0]); |
143
|
|
|
}, |
144
|
|
|
], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* It should process "empty" composites. |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function testVisitEmptyComposite() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$this->visitor->dispatch( |
154
|
|
|
Query::composite('and') |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$query = $this->queryBuilder; |
158
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
159
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
160
|
|
|
$this->assertInstanceOf(ConstraintAndx::class, $children[0]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* It should visit and composites. |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testVisitCompositeAnd() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->visitor->dispatch( |
169
|
|
|
Query::composite('and', Query::comparison('eq', 'title', 42)) |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$query = $this->queryBuilder; |
173
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
174
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
175
|
|
|
$this->assertInstanceOf(ConstraintAndx::class, $children[0]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* It should visit or composites. |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function testVisitCompositeOr() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$this->visitor->dispatch( |
184
|
|
|
Query::composite('or', Query::comparison('eq', 'title', 42)) |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$query = $this->queryBuilder; |
188
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
189
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
190
|
|
|
$this->assertInstanceOf(ConstraintOrx::class, $children[0]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Test nested composites. |
195
|
|
|
*/ |
196
|
|
|
public function testNestedComposites() |
197
|
|
|
{ |
198
|
|
|
$this->visitor->dispatch( |
199
|
|
|
Query::composite( |
200
|
|
|
'or', |
201
|
|
|
Query::comparison('eq', 'title', 42), |
202
|
|
|
QUery::composite( |
203
|
|
|
'and', |
204
|
|
|
Query::comparison('eq', 'title', 'boobar'), |
205
|
|
|
Query::composite( |
206
|
|
|
'or', |
207
|
|
|
Query::comparison('eq', 'title', 67) |
208
|
|
|
) |
209
|
|
|
) |
210
|
|
|
) |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$query = $this->queryBuilder; |
214
|
|
|
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE); |
215
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
216
|
|
|
$this->assertInstanceOf(ConstraintOrx::class, $children[0]); |
217
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
218
|
|
|
$this->assertInstanceOf(ConstraintAndx::class, $children[1]); |
219
|
|
|
$children = $children[1]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
220
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $children[0]); |
221
|
|
|
$this->assertInstanceOf(ConstraintOrx::class, $children[1]); |
222
|
|
|
$children = $children[1]->getChildrenOfType(AbstractNode::NT_CONSTRAINT); |
223
|
|
|
$this->assertInstanceOf(ConstraintComparison::class, $children[0]); |
224
|
|
|
$children = $children[0]->getChildrenOfType(AbstractNode::NT_OPERAND_STATIC); |
225
|
|
|
$this->assertEquals(67, $children[0]->getValue()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: