-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ArrayFilterTypeTest.php
129 lines (113 loc) · 4.08 KB
/
ArrayFilterTypeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Form\Filter\Type;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\Type\ArrayFilterType;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\ComparisonType;
class ArrayFilterTypeTest extends FilterTypeTest
{
protected function setUp(): void
{
parent::setUp();
// reset counter (only for test purpose)
$m = new \ReflectionProperty(ArrayFilterType::class, 'uniqueAliasId');
$m->setAccessible(true);
$m->setValue(0);
}
/**
* @dataProvider getDataProvider
*/
public function testSubmit($submittedData, $data, array $options = [])
{
$form = $this->factory->create(ArrayFilterType::class, null, $options);
$form->submit($submittedData);
$this->assertSame($data, $form->getData());
$this->assertSame($submittedData, $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
}
/**
* @dataProvider getDataProvider
*/
public function testFilter($submittedData, $data, array $options = [])
{
$qb = $this->getMockBuilder(QueryBuilder::class)
->disableOriginalConstructor()
->getMock()
;
$qb->expects($this->once())
->method('getRootAliases')
->willReturn(['o'])
;
if (null === $data['value'] || [] === $data['value']) {
$qb->expects($this->once())
->method('andWhere')
->with(\sprintf('o.foo %s', $data['comparison']))
->willReturn($qb)
;
} else {
$orX = new Expr\Orx();
$orX->add(\sprintf('o.foo %s :foo_1', $data['comparison']));
if (ComparisonType::NOT_CONTAINS === $data['comparison']) {
$orX->add('o.foo IS NULL');
}
$qb->expects($this->once())
->method('andWhere')
->with($orX)
->willReturn($qb)
;
$qb->expects($this->once())
->method('setParameter')
->with('foo_1', '%"'.$data['value'][0].'"%')
;
}
$form = $this->factory->create(ArrayFilterType::class, null, $options);
$form->submit($submittedData);
$filter = $this->filterRegistry->resolveType($form);
$this->assertSame(ArrayFilterType::class, \get_class($filter));
$filter->filter($qb, $form, ['property' => 'foo', 'dataType' => 'array']);
}
public function getDataProvider(): iterable
{
yield [
['comparison' => ComparisonType::CONTAINS, 'value' => ['bar']],
['comparison' => 'like', 'value' => ['bar']],
];
yield [
['comparison' => ComparisonType::NOT_CONTAINS, 'value' => ['bar']],
['comparison' => 'not like', 'value' => ['bar']],
];
yield [
['comparison' => ComparisonType::CONTAINS, 'value' => []],
['comparison' => 'IS NULL', 'value' => []],
];
yield [
['comparison' => ComparisonType::CONTAINS, 'value' => null],
['comparison' => 'IS NULL', 'value' => null],
[
'value_type_options' => [
'choices' => ['a' => 'a', 'b' => 'b', 'c' => 'c'],
],
],
];
yield [
['comparison' => ComparisonType::CONTAINS, 'value' => 'b'],
['comparison' => 'like', 'value' => ['b']],
[
'value_type_options' => [
'choices' => ['a' => 'a', 'b' => 'b', 'c' => 'c'],
],
],
];
yield [
['comparison' => ComparisonType::NOT_CONTAINS, 'value' => ['c']],
['comparison' => 'not like', 'value' => ['c']],
[
'value_type_options' => [
'multiple' => true,
'choices' => ['a' => 'a', 'b' => 'b', 'c' => 'c'],
],
],
];
}
}