StringFilterTest::testContains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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\Query\Builder;
17
use MongoDB\BSON\Regex;
18
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;
19
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
20
use Sonata\DoctrineMongoDBAdminBundle\Filter\Filter;
21
use Sonata\DoctrineMongoDBAdminBundle\Filter\StringFilter;
22
23
class StringFilterTest extends FilterWithQueryBuilderTest
24
{
25
    public function testEmpty(): void
26
    {
27
        $filter = new StringFilter();
28
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
29
30
        $builder = new ProxyQuery($this->getQueryBuilder());
31
32
        $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...
33
            ->expects($this->never())
34
            ->method('field')
35
        ;
36
37
        $filter->filter($builder, 'alias', 'field', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
38
        $filter->filter($builder, 'alias', 'field', '');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

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...
39
40
        $this->assertFalse($filter->isActive());
41
    }
42
43
    public function testContains(): void
44
    {
45
        $filter = new StringFilter();
46
        $filter->initialize('field_name', ['format' => '%s']);
47
48
        $builder = new ProxyQuery($this->getQueryBuilder());
49
50
        $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...
51
            ->expects($this->exactly(2))
52
            ->method('equals')
53
            ->with($this->getMongoRegex('asd'))
54
        ;
55
56
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
57
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => null]);
58
        $this->assertTrue($filter->isActive());
59
    }
60
61
    public function testNotContains(): void
62
    {
63
        $filter = new StringFilter();
64
        $filter->initialize('field_name', ['format' => '%s']);
65
66
        $builder = new ProxyQuery($this->getQueryBuilder());
67
68
        $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...
69
            ->expects($this->once())
70
            ->method('not')
71
            ->with($this->getMongoRegex('asd'))
72
        ;
73
74
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]);
75
        $this->assertTrue($filter->isActive());
76
    }
77
78
    public function testEquals(): void
79
    {
80
        $filter = new StringFilter();
81
        $filter->initialize('field_name', ['format' => '%s']);
82
83
        $builder = new ProxyQuery($this->getQueryBuilder());
84
85
        $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...
86
            ->expects($this->once())
87
            ->method('equals')
88
            ->with('asd')
89
        ;
90
91
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_EQUAL]);
92
        $this->assertTrue($filter->isActive());
93
    }
94
95
    public function testEqualsWithValidParentAssociationMappings(): void
96
    {
97
        $filter = new StringFilter();
98
        $filter->initialize('field_name', [
99
            'format' => '%s',
100
            'field_name' => 'field_name',
101
            'parent_association_mappings' => [
102
                [
103
                    'fieldName' => 'association_mapping',
104
                ],
105
                [
106
                    'fieldName' => 'sub_association_mapping',
107
                ],
108
                [
109
                    'fieldName' => 'sub_sub_association_mapping',
110
                ],
111
            ],
112
        ]);
113
114
        $queryBuilder = $this->createMock(Builder::class);
115
116
        $builder = new ProxyQuery($queryBuilder);
117
118
        $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...
119
            ->method('field')
120
            ->with('field_name')
121
            ->willReturnSelf()
122
        ;
123
124
        $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...
125
            ->expects($this->once())
126
            ->method('equals')
127
            ->with('asd')
128
        ;
129
130
        $filter->apply($builder, ['type' => ContainsOperatorType::TYPE_EQUAL, 'value' => 'asd']);
131
        $this->assertTrue($filter->isActive());
132
    }
133
134
    public function testOr(): void
135
    {
136
        $filter = new StringFilter();
137
        $filter->initialize('field_name', ['format' => '%s']);
138
        $filter->setCondition(Filter::CONDITION_OR);
139
140
        $builder = new ProxyQuery($this->getQueryBuilder());
141
        $builder->getQueryBuilder()->expects($this->once())->method('addOr');
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...
142
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
143
        $this->assertTrue($filter->isActive());
144
145
        $filter->setCondition(Filter::CONDITION_AND);
146
147
        $builder = new ProxyQuery($this->getQueryBuilder());
148
        $builder->getQueryBuilder()->expects($this->never())->method('addOr');
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...
149
        $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
150
        $this->assertTrue($filter->isActive());
151
    }
152
153
    /**
154
     * NEXT_MAJOR: Use only Regex when dropping support for doctrine/mongodb-odm 1.x.
155
     *
156
     * @return Regex|\MongoRegex
157
     */
158
    private function getMongoRegex(string $pattern)
159
    {
160
        if (class_exists(Regex::class)) {
161
            return new Regex($pattern, 'i');
162
        }
163
164
        return new \MongoRegex(sprintf('/%s/i', $pattern));
165
    }
166
}
167