These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Filter\ChoiceType; |
||
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() |
||
33 | ->expects($this->never()) |
||
34 | ->method('field') |
||
35 | ; |
||
36 | |||
37 | $filter->filter($builder, 'alias', 'field', null); |
||
38 | $filter->filter($builder, 'alias', 'field', ''); |
||
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() |
||
51 | ->expects($this->exactly(2)) |
||
52 | ->method('equals') |
||
53 | ->with($this->getMongoRegex('asd')) |
||
54 | ; |
||
55 | |||
56 | $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::TYPE_CONTAINS]); |
||
0 ignored issues
–
show
|
|||
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() |
||
69 | ->expects($this->once()) |
||
70 | ->method('not') |
||
71 | ->with($this->getMongoRegex('asd')) |
||
72 | ; |
||
73 | |||
74 | $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::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() |
||
86 | ->expects($this->once()) |
||
87 | ->method('equals') |
||
88 | ->with('asd') |
||
89 | ; |
||
90 | |||
91 | $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::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() |
||
119 | ->method('field') |
||
120 | ->with('field_name') |
||
121 | ->willReturnSelf() |
||
122 | ; |
||
123 | |||
124 | $builder->getQueryBuilder() |
||
125 | ->expects($this->once()) |
||
126 | ->method('equals') |
||
127 | ->with('asd') |
||
128 | ; |
||
129 | |||
130 | $filter->apply($builder, ['type' => ChoiceType::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'); |
||
142 | $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::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'); |
||
149 | $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ChoiceType::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 |
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.