|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\DoctrineQueryFilter\Filter; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Constant\WhereType; |
|
8
|
|
|
use Arp\DoctrineQueryFilter\Filter\IsMemberOf; |
|
9
|
|
|
use Doctrine\ORM\Query\Expr; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\IsMemberOf |
|
14
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AbstractExpression |
|
15
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AbstractFilter |
|
16
|
|
|
* |
|
17
|
|
|
* @author Alex Patterson <[email protected]> |
|
18
|
|
|
* @package ArpTest\DoctrineQueryFilter\Filter |
|
19
|
|
|
*/ |
|
20
|
|
|
final class IsMemberOfTest extends AbstractComparisonTest |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected string $filterClassName = IsMemberOf::class; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected string $expressionMethodName = 'isMemberOf'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected string $expressionSymbol = 'MEMBER OF'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Assert that the IsMemberOf query filter can be applied with the provided $criteria |
|
39
|
|
|
* |
|
40
|
|
|
* @param array<mixed>$criteria |
|
41
|
|
|
* |
|
42
|
|
|
* @dataProvider getFilterWillApplyFilteringData |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testFilterWillApplyFiltering(array $criteria): void |
|
45
|
|
|
{ |
|
46
|
|
|
$fieldName = $criteria['field'] ?? 'testFieldName'; |
|
47
|
|
|
$alias = $criteria['alias'] ?? null; |
|
48
|
|
|
|
|
49
|
|
|
$mapping = [ |
|
50
|
|
|
'type' => 4 |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$this->metadata->expects($this->once()) |
|
54
|
|
|
->method('hasField') |
|
55
|
|
|
->with($fieldName) |
|
56
|
|
|
->willReturn(true); |
|
57
|
|
|
|
|
58
|
|
|
$this->metadata->expects($this->once()) |
|
59
|
|
|
->method('hasAssociation') |
|
60
|
|
|
->with($fieldName) |
|
61
|
|
|
->willReturn(true); |
|
62
|
|
|
|
|
63
|
|
|
$this->metadata->expects($this->once()) |
|
64
|
|
|
->method('getAssociationMapping') |
|
65
|
|
|
->with($fieldName) |
|
66
|
|
|
->willReturn($mapping); |
|
67
|
|
|
|
|
68
|
|
|
/** @var Expr&MockObject $expr */ |
|
69
|
|
|
$expr = $this->createMock(Expr::class); |
|
70
|
|
|
|
|
71
|
|
|
$this->queryBuilder->expects($this->once()) |
|
72
|
|
|
->method('expr') |
|
73
|
|
|
->willReturn($expr); |
|
74
|
|
|
|
|
75
|
|
|
/** @var Expr\Comparison&MockObject $comparisonExpr */ |
|
76
|
|
|
$comparisonExpr = $this->createMock(Expr\Comparison::class); |
|
77
|
|
|
|
|
78
|
|
|
if (null === $alias) { |
|
79
|
|
|
$alias = 'entity'; |
|
80
|
|
|
$this->queryBuilder->expects($this->once()) |
|
81
|
|
|
->method('getRootAlias') |
|
82
|
|
|
->willReturn($alias); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$expressionString = $this->getExpressionString($fieldName, $alias, $criteria); |
|
86
|
|
|
|
|
87
|
|
|
$expr->expects($this->once()) |
|
88
|
|
|
->method($this->expressionMethodName) |
|
89
|
|
|
->willReturn($comparisonExpr); |
|
90
|
|
|
|
|
91
|
|
|
$comparisonExpr->expects($this->once()) |
|
92
|
|
|
->method('__toString') |
|
93
|
|
|
->willReturn($expressionString); |
|
94
|
|
|
|
|
95
|
|
|
$methodName = (!isset($criteria['where']) || WhereType::AND === $criteria['where']) |
|
96
|
|
|
? 'andWhere' |
|
97
|
|
|
: 'orWhere'; |
|
98
|
|
|
|
|
99
|
|
|
$this->queryBuilder->expects($this->once())->method($methodName); |
|
100
|
|
|
|
|
101
|
|
|
if (array_key_exists('value', $criteria)) { |
|
102
|
|
|
$this->typecaster->expects($this->once()) |
|
103
|
|
|
->method('typecast') |
|
104
|
|
|
->with($this->metadata, $fieldName, $criteria['value']) |
|
105
|
|
|
->willReturn($criteria['value']); |
|
106
|
|
|
|
|
107
|
|
|
$this->queryBuilder->expects($this->once()) |
|
108
|
|
|
->method('setParameter') |
|
109
|
|
|
->with($this->callback(static function ($argument) { |
|
110
|
|
|
return is_string($argument); |
|
111
|
|
|
}), $criteria['value']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->filter->filter($this->queryBuilder, $this->metadata, $criteria); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $fieldName |
|
119
|
|
|
* @param string|null $alias |
|
120
|
|
|
* @param array<mixed> $criteria |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getExpressionString(string $fieldName, ?string $alias, array $criteria): string |
|
125
|
|
|
{ |
|
126
|
|
|
return ':param_name ' . $this->expressionSymbol . $alias . '.' . $fieldName; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array<mixed> |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getFilterWillApplyFilteringData(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return [ |
|
135
|
|
|
[ |
|
136
|
|
|
[ |
|
137
|
|
|
'name' => 'test', |
|
138
|
|
|
'field' => 'hello', |
|
139
|
|
|
'value' => 123, |
|
140
|
|
|
], |
|
141
|
|
|
], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|