1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Filter; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
20
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; |
21
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter; |
22
|
|
|
use Sonata\Form\Type\EqualType; |
23
|
|
|
|
24
|
|
|
class DocumentStub |
25
|
|
|
{ |
26
|
|
|
private $id; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->id = new \MongoId(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getId() |
34
|
|
|
{ |
35
|
|
|
return (string) ($this->id); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
class ModelFilterTest extends TestCase |
40
|
|
|
{ |
41
|
|
|
private $queryBuilder; |
42
|
|
|
|
43
|
|
|
protected function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->queryBuilder = $this->createMock(Builder::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface |
50
|
|
|
*/ |
51
|
|
|
public function getFieldDescription(array $options) |
52
|
|
|
{ |
53
|
|
|
$fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
54
|
|
|
$fieldDescription->expects($this->once())->method('getOptions')->willReturn($options); |
55
|
|
|
$fieldDescription->expects($this->once())->method('getName')->willReturn('field_name'); |
56
|
|
|
|
57
|
|
|
return $fieldDescription; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testFilterEmpty(): void |
61
|
|
|
{ |
62
|
|
|
$filter = new ModelFilter(); |
63
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
64
|
|
|
|
65
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
68
|
|
|
->expects($this->never()) |
69
|
|
|
->method('field') |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
$filter->filter($builder, 'alias', 'field', null); |
73
|
|
|
$filter->filter($builder, 'alias', 'field', []); |
74
|
|
|
|
75
|
|
|
$this->assertFalse($filter->isActive()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFilterArray(): void |
79
|
|
|
{ |
80
|
|
|
$filter = new ModelFilter(); |
81
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]); |
82
|
|
|
|
83
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
86
|
|
|
->expects($this->once()) |
87
|
|
|
->method('field') |
88
|
|
|
->with('field.$id') |
89
|
|
|
->willReturnSelf() |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$oneDocument = new DocumentStub(); |
93
|
|
|
$otherDocument = new DocumentStub(); |
94
|
|
|
|
95
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('in') |
98
|
|
|
->with([new \MongoId($oneDocument->getId()), new \MongoId($otherDocument->getId())]) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$filter->filter($builder, 'alias', 'field', [ |
102
|
|
|
'type' => EqualType::TYPE_IS_EQUAL, |
103
|
|
|
'value' => [$oneDocument, $otherDocument], |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$this->assertTrue($filter->isActive()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testFilterScalar(): void |
110
|
|
|
{ |
111
|
|
|
$filter = new ModelFilter(); |
112
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]); |
113
|
|
|
|
114
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
117
|
|
|
->expects($this->once()) |
118
|
|
|
->method('field') |
119
|
|
|
->with('field.$id') |
120
|
|
|
->willReturnSelf() |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
$document1 = new DocumentStub(); |
124
|
|
|
|
125
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
126
|
|
|
->expects($this->once()) |
127
|
|
|
->method('equals') |
128
|
|
|
->with(new \MongoId($document1->getId())) |
129
|
|
|
; |
130
|
|
|
|
131
|
|
|
$filter->filter($builder, 'alias', 'field', ['type' => EqualType::TYPE_IS_EQUAL, 'value' => $document1]); |
132
|
|
|
|
133
|
|
|
$this->assertTrue($filter->isActive()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testAssociationWithInvalidMapping(): void |
137
|
|
|
{ |
138
|
|
|
$this->expectException(\RuntimeException::class); |
139
|
|
|
|
140
|
|
|
$filter = new ModelFilter(); |
141
|
|
|
$filter->initialize('field_name', ['mapping_type' => 'foo', 'field_mapping' => true]); |
142
|
|
|
|
143
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
144
|
|
|
|
145
|
|
|
$filter->apply($builder, 'asd'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testAssociationWithValidMappingAndEmptyFieldName(): void |
149
|
|
|
{ |
150
|
|
|
$this->expectException(\RuntimeException::class); |
151
|
|
|
|
152
|
|
|
$filter = new ModelFilter(); |
153
|
|
|
$filter->initialize('field_name', ['mapping_type' => ClassMetadata::ONE, 'field_mapping' => true]); |
154
|
|
|
|
155
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$filter->apply($builder, 'asd'); |
158
|
|
|
$this->assertTrue($filter->isActive()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testAssociationWithValidMapping(): void |
162
|
|
|
{ |
163
|
|
|
$filter = new ModelFilter(); |
164
|
|
|
$filter->initialize('field_name', [ |
165
|
|
|
'mapping_type' => ClassMetadata::ONE, |
166
|
|
|
'field_name' => 'field_name', |
167
|
|
|
'association_mapping' => [ |
168
|
|
|
'fieldName' => 'association_mapping', |
169
|
|
|
], 'field_mapping' => true, |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
175
|
|
|
->method('field') |
176
|
|
|
->with('field_name.$id') |
177
|
|
|
->willReturnSelf() |
178
|
|
|
; |
179
|
|
|
|
180
|
|
|
$filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]); |
181
|
|
|
|
182
|
|
|
$this->assertTrue($filter->isActive()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testAssociationWithValidParentAssociationMappings(): void |
186
|
|
|
{ |
187
|
|
|
$filter = new ModelFilter(); |
188
|
|
|
$filter->initialize('field_name', [ |
189
|
|
|
'mapping_type' => ClassMetadata::ONE, |
190
|
|
|
'field_name' => 'field_name', |
191
|
|
|
'parent_association_mappings' => [ |
192
|
|
|
[ |
193
|
|
|
'fieldName' => 'association_mapping', |
194
|
|
|
], |
195
|
|
|
[ |
196
|
|
|
'fieldName' => 'sub_association_mapping', |
197
|
|
|
], |
198
|
|
|
], |
199
|
|
|
'association_mapping' => [ |
200
|
|
|
'fieldName' => 'sub_sub_association_mapping', |
201
|
|
|
], 'field_mapping' => true, |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
$builder = new ProxyQuery($this->queryBuilder); |
|
|
|
|
205
|
|
|
|
206
|
|
|
$builder->getQueryBuilder() |
|
|
|
|
207
|
|
|
->method('field') |
208
|
|
|
->with('field_name.$id') |
209
|
|
|
->willReturnSelf() |
210
|
|
|
; |
211
|
|
|
|
212
|
|
|
$filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]); |
213
|
|
|
|
214
|
|
|
$this->assertTrue($filter->isActive()); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: