ModelFilterTest::getMongoIdentifier()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 MongoDB\BSON\ObjectId;
19
use PHPUnit\Framework\TestCase;
20
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
21
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
22
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
23
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter;
24
25
class DocumentStub
26
{
27
    private $id;
28
29
    public function __construct()
30
    {
31
        // NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x
32
        $this->id = class_exists(ObjectId::class) ? new ObjectId() : new MongoId();
33
    }
34
35
    public function getId()
36
    {
37
        return (string) ($this->id);
38
    }
39
}
40
41
class ModelFilterTest extends TestCase
42
{
43
    private $queryBuilder;
44
45
    protected function setUp(): void
46
    {
47
        $this->queryBuilder = $this->createMock(Builder::class);
48
    }
49
50
    /**
51
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
52
     */
53
    public function getFieldDescription(array $options)
54
    {
55
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
56
        $fieldDescription->expects($this->once())->method('getOptions')->willReturn($options);
57
        $fieldDescription->expects($this->once())->method('getName')->willReturn('field_name');
58
59
        return $fieldDescription;
60
    }
61
62
    public function testFilterEmpty(): void
63
    {
64
        $filter = new ModelFilter();
65
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
66
67
        $builder = new ProxyQuery($this->queryBuilder);
68
69
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            ->expects($this->never())
71
            ->method('field')
72
        ;
73
74
        $filter->filter($builder, 'alias', 'field', null);
75
        $filter->filter($builder, 'alias', 'field', []);
76
77
        $this->assertFalse($filter->isActive());
78
    }
79
80
    public function testFilterArray(): void
81
    {
82
        $filter = new ModelFilter();
83
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
84
85
        $builder = new ProxyQuery($this->queryBuilder);
86
87
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            ->expects($this->once())
89
            ->method('field')
90
            ->with('field._id')
91
            ->willReturnSelf()
92
        ;
93
94
        $oneDocument = new DocumentStub();
95
        $otherDocument = new DocumentStub();
96
97
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
            ->expects($this->once())
99
            ->method('in')
100
            ->with([$this->getMongoIdentifier($oneDocument->getId()), $this->getMongoIdentifier($otherDocument->getId())])
101
        ;
102
103
        $filter->filter($builder, 'alias', 'field', [
104
            'type' => EqualOperatorType::TYPE_EQUAL,
105
            'value' => [$oneDocument, $otherDocument],
106
        ]);
107
108
        $this->assertTrue($filter->isActive());
109
    }
110
111
    public function testFilterScalar(): void
112
    {
113
        $filter = new ModelFilter();
114
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
115
116
        $builder = new ProxyQuery($this->queryBuilder);
117
118
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
            ->expects($this->once())
120
            ->method('field')
121
            ->with('field._id')
122
            ->willReturnSelf()
123
        ;
124
125
        $document1 = new DocumentStub();
126
127
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            ->expects($this->once())
129
            ->method('equals')
130
            ->with($this->getMongoIdentifier($document1->getId()))
131
        ;
132
133
        $filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => $document1]);
134
135
        $this->assertTrue($filter->isActive());
136
    }
137
138
    public function testAssociationWithInvalidMapping(): void
139
    {
140
        $this->expectException(\RuntimeException::class);
141
142
        $filter = new ModelFilter();
143
        $filter->initialize('field_name', ['mapping_type' => 'foo', 'field_mapping' => true]);
144
145
        $builder = new ProxyQuery($this->queryBuilder);
146
147
        $filter->apply($builder, 'asd');
148
    }
149
150
    public function testAssociationWithValidMappingAndEmptyFieldName(): void
151
    {
152
        $this->expectException(\RuntimeException::class);
153
154
        $filter = new ModelFilter();
155
        $filter->initialize('field_name', ['mapping_type' => ClassMetadata::ONE, 'field_mapping' => true]);
156
157
        $builder = new ProxyQuery($this->queryBuilder);
158
159
        $filter->apply($builder, 'asd');
160
        $this->assertTrue($filter->isActive());
161
    }
162
163
    public function testAssociationWithValidMapping(): void
164
    {
165
        $filter = new ModelFilter();
166
        $filter->initialize('field_name', [
167
            'mapping_type' => ClassMetadata::ONE,
168
            'field_name' => 'field_name',
169
            'association_mapping' => [
170
                'fieldName' => 'association_mapping',
171
            ], 'field_mapping' => true,
172
        ]);
173
174
        $builder = new ProxyQuery($this->queryBuilder);
175
176
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
            ->method('field')
178
            ->with('field_name._id')
179
            ->willReturnSelf()
180
        ;
181
182
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
183
184
        $this->assertTrue($filter->isActive());
185
    }
186
187
    public function testAssociationWithValidParentAssociationMappings(): void
188
    {
189
        $filter = new ModelFilter();
190
        $filter->initialize('field_name', [
191
            'mapping_type' => ClassMetadata::ONE,
192
            'field_name' => 'field_name',
193
            'parent_association_mappings' => [
194
                [
195
                    'fieldName' => 'association_mapping',
196
                ],
197
                [
198
                    'fieldName' => 'sub_association_mapping',
199
                ],
200
            ],
201
            'association_mapping' => [
202
                'fieldName' => 'sub_sub_association_mapping',
203
            ], 'field_mapping' => true,
204
        ]);
205
206
        $builder = new ProxyQuery($this->queryBuilder);
207
208
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
            ->method('field')
210
            ->with('field_name._id')
211
            ->willReturnSelf()
212
        ;
213
214
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
215
216
        $this->assertTrue($filter->isActive());
217
    }
218
219
    /**
220
     * @dataProvider getMappings
221
     */
222
    public function testDifferentIdentifiersBasedOnMapping(string $storeAs, string $fieldIdentifier): void
223
    {
224
        $filter = new ModelFilter();
225
        $filter->initialize('field_name', [
226
            'mapping_type' => ClassMetadata::ONE,
227
            'field_name' => 'field_name',
228
            'field_mapping' => [
229
                'storeAs' => $storeAs,
230
            ],
231
        ]);
232
233
        $builder = new ProxyQuery($this->queryBuilder);
234
235
        $builder->getQueryBuilder()
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\ODM\MongoDB\Query\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
            ->method('field')
237
            ->with('field_name'.$fieldIdentifier)
238
            ->willReturnSelf()
239
        ;
240
241
        $filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);
242
243
        $this->assertTrue($filter->isActive());
244
    }
245
246
    public function getMappings(): array
247
    {
248
        return [
249
            [ClassMetadata::REFERENCE_STORE_AS_REF, '.id'],
250
            [ClassMetadata::REFERENCE_STORE_AS_ID, ''],
251
            [ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB, '.$id'],
252
            [ClassMetadata::REFERENCE_STORE_AS_DB_REF, '.$id'],
253
        ];
254
    }
255
256
    /**
257
     * NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x.
258
     *
259
     * @return ObjectId|\MongoId
260
     */
261
    private function getMongoIdentifier(string $id)
262
    {
263
        return class_exists(ObjectId::class) ? new ObjectId($id) : new \MongoId($id);
264
    }
265
}
266