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\Datagrid\ProxyQueryInterface; |
20
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; |
21
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\Filter; |
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
23
|
|
|
|
24
|
|
|
class FilterTest_Filter extends Filter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Apply the filter to the QueryBuilder instance. |
28
|
|
|
* |
29
|
|
|
* @param $queryBuilder |
30
|
|
|
* @param string $alias |
31
|
|
|
* @param string $field |
32
|
|
|
* @param string $value |
33
|
|
|
*/ |
34
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value): void |
35
|
|
|
{ |
36
|
|
|
$queryBuilder->field($field)->equals($value); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getDefaultOptions() |
40
|
|
|
{ |
41
|
|
|
return ['option1' => 2]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getRenderSettings() |
45
|
|
|
{ |
46
|
|
|
return ['sonata_type_filter_default', [ |
47
|
|
|
'type' => $this->getFieldType(), |
48
|
|
|
'options' => $this->getFieldOptions(), |
49
|
|
|
]]; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
class FilterTest extends TestCase |
54
|
|
|
{ |
55
|
|
|
public function testFieldDescription(): void |
56
|
|
|
{ |
57
|
|
|
$filter = new FilterTest_Filter(); |
58
|
|
|
$this->assertSame(['option1' => 2], $filter->getDefaultOptions()); |
59
|
|
|
$this->assertNull($filter->getOption('1')); |
60
|
|
|
|
61
|
|
|
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); |
62
|
|
|
|
63
|
|
|
$this->assertSame(2, $filter->getOption('option1')); |
64
|
|
|
$this->assertNull($filter->getOption('foo')); |
65
|
|
|
$this->assertSame('bar', $filter->getOption('foo', 'bar')); |
66
|
|
|
|
67
|
|
|
$this->assertSame('field_name', $filter->getName()); |
68
|
|
|
$this->assertSame(TextType::class, $filter->getFieldType()); |
69
|
|
|
$this->assertSame(['class' => 'FooBar'], $filter->getFieldOptions()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testValues(): void |
73
|
|
|
{ |
74
|
|
|
$filter = new FilterTest_Filter(); |
75
|
|
|
$this->assertEmpty($filter->getValue()); |
76
|
|
|
|
77
|
|
|
$filter->setValue(42); |
78
|
|
|
$this->assertSame(42, $filter->getValue()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testExceptionOnEmptyFieldName(): void |
82
|
|
|
{ |
83
|
|
|
$this->expectException(\RuntimeException::class); |
84
|
|
|
|
85
|
|
|
$filter = new FilterTest_Filter(); |
86
|
|
|
$filter->getFieldName(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testIsActive(): void |
90
|
|
|
{ |
91
|
|
|
$filter = new FilterTest_Filter(); |
92
|
|
|
$this->assertFalse($filter->isActive()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testUseNameWithParentAssociationMappings(): void |
96
|
|
|
{ |
97
|
|
|
$filter = new FilterTest_Filter(); |
98
|
|
|
$filter->initialize('field.name', [ |
99
|
|
|
'mapping_type' => ClassMetadata::ONE, |
100
|
|
|
'field_name' => 'field_name', |
101
|
|
|
'parent_association_mappings' => [ |
102
|
|
|
[ |
103
|
|
|
'fieldName' => 'field', |
104
|
|
|
], |
105
|
|
|
], 'field_mapping' => true, |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$queryBuilder = $this->createMock(Builder::class); |
109
|
|
|
|
110
|
|
|
$builder = new ProxyQuery($queryBuilder); |
111
|
|
|
|
112
|
|
|
$queryBuilder |
113
|
|
|
->expects($this->once()) |
114
|
|
|
->method('field') |
115
|
|
|
->with('field.name') |
116
|
|
|
->willReturnSelf() |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
$queryBuilder |
120
|
|
|
->expects($this->once()) |
121
|
|
|
->method('equals') |
122
|
|
|
->with('foo') |
123
|
|
|
; |
124
|
|
|
|
125
|
|
|
$filter->apply($builder, 'foo'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testUseFieldNameWithoutParentAssociationMappings(): void |
129
|
|
|
{ |
130
|
|
|
$filter = new FilterTest_Filter(); |
131
|
|
|
$filter->initialize('field_name', [ |
132
|
|
|
'mapping_type' => ClassMetadata::ONE, |
133
|
|
|
'field_name' => 'field_name', |
134
|
|
|
'field_mapping' => true, |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
$queryBuilder = $this->createMock(Builder::class); |
138
|
|
|
|
139
|
|
|
$builder = new ProxyQuery($queryBuilder); |
140
|
|
|
|
141
|
|
|
$queryBuilder |
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('field') |
144
|
|
|
->with('field_name') |
145
|
|
|
->willReturnSelf() |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$queryBuilder |
149
|
|
|
->expects($this->once()) |
150
|
|
|
->method('equals') |
151
|
|
|
->with('foo') |
152
|
|
|
; |
153
|
|
|
|
154
|
|
|
$filter->apply($builder, 'foo'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|