Completed
Pull Request — 3.x (#329)
by
unknown
01:19
created

ModelFilterTest::getMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
70
        $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...
71
            ->expects($this->never())
72
            ->method('field')
73
        ;
74
75
        $filter->filter($builder, 'alias', 'field', null);
76
        $filter->filter($builder, 'alias', 'field', []);
77
78
        $this->assertFalse($filter->isActive());
79
    }
80
81
    public function testFilterArray()
82
    {
83
        $filter = new ModelFilter();
84
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
85
86
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
88
        $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...
89
            ->expects($this->once())
90
            ->method('field')
91
            ->with('field._id')
92
            ->willReturnSelf()
93
        ;
94
95
        $oneDocument = new DocumentStub();
96
        $otherDocument = new DocumentStub();
97
98
        $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...
99
            ->expects($this->once())
100
            ->method('in')
101
            ->with([$this->getMongoIdentifier($oneDocument->getId()), $this->getMongoIdentifier($otherDocument->getId())])
102
        ;
103
104
        $filter->filter($builder, 'alias', 'field', [
105
            'type' => EqualType::TYPE_IS_EQUAL,
106
            'value' => [$oneDocument, $otherDocument],
107
        ]);
108
109
        $this->assertTrue($filter->isActive());
110
    }
111
112
    public function testFilterScalar()
113
    {
114
        $filter = new ModelFilter();
115
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
116
117
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
119
        $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...
120
            ->expects($this->once())
121
            ->method('field')
122
            ->with('field._id')
123
            ->willReturnSelf()
124
        ;
125
126
        $document1 = new DocumentStub();
127
128
        $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...
129
            ->expects($this->once())
130
            ->method('equals')
131
            ->with($this->getMongoIdentifier($document1->getId()))
132
        ;
133
134
        $filter->filter($builder, 'alias', 'field', ['type' => EqualType::TYPE_IS_EQUAL, 'value' => $document1]);
135
136
        $this->assertTrue($filter->isActive());
137
    }
138
139
    public function testAssociationWithInvalidMapping()
140
    {
141
        $this->expectException(\RuntimeException::class);
142
143
        $filter = new ModelFilter();
144
        $filter->initialize('field_name', ['mapping_type' => 'foo', 'field_mapping' => true]);
145
146
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
148
        $filter->apply($builder, 'asd');
149
    }
150
151
    public function testAssociationWithValidMappingAndEmptyFieldName()
152
    {
153
        $this->expectException(\RuntimeException::class);
154
155
        $filter = new ModelFilter();
156
        $filter->initialize('field_name', ['mapping_type' => ClassMetadata::ONE, 'field_mapping' => true]);
157
158
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
160
        $filter->apply($builder, 'asd');
161
        $this->assertTrue($filter->isActive());
162
    }
163
164
    public function testAssociationWithValidMapping()
165
    {
166
        $filter = new ModelFilter();
167
        $filter->initialize('field_name', [
168
            'mapping_type' => ClassMetadata::ONE,
169
            'field_name' => 'field_name',
170
            'association_mapping' => [
171
                'fieldName' => 'association_mapping',
172
            ], 'field_mapping' => true,
173
        ]);
174
175
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
177
        $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...
178
            ->method('field')
179
            ->with('field_name._id')
180
            ->willReturnSelf()
181
        ;
182
183
        $filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
184
185
        $this->assertTrue($filter->isActive());
186
    }
187
188
    public function testAssociationWithValidParentAssociationMappings()
189
    {
190
        $filter = new ModelFilter();
191
        $filter->initialize('field_name', [
192
            'mapping_type' => ClassMetadata::ONE,
193
            'field_name' => 'field_name',
194
            'parent_association_mappings' => [
195
                [
196
                    'fieldName' => 'association_mapping',
197
                ],
198
                [
199
                    'fieldName' => 'sub_association_mapping',
200
                ],
201
            ],
202
            'association_mapping' => [
203
                'fieldName' => 'sub_sub_association_mapping',
204
            ], 'field_mapping' => true,
205
        ]);
206
207
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
208
209
        $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...
210
            ->method('field')
211
            ->with('field_name._id')
212
            ->willReturnSelf()
213
        ;
214
215
        $filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
216
217
        $this->assertTrue($filter->isActive());
218
    }
219
220
    /**
221
     * @dataProvider getMappings
222
     */
223
    public function testDifferentIdentifiersBasedOnMapping(string $storeAs, string $fieldIdentifier)
224
    {
225
        $filter = new ModelFilter();
226
        $filter->initialize('field_name', [
227
            'mapping_type' => ClassMetadata::ONE,
228
            'field_name' => 'field_name',
229
            'field_mapping' => [
230
                'storeAs' => $storeAs,
231
            ],
232
        ]);
233
234
        $builder = new ProxyQuery($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\MongoDB\Query\Builder>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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