1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DoctrineQueryFilter\Filter; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Enum\WhereType; |
8
|
|
|
use Arp\DoctrineQueryFilter\Filter\AndX; |
9
|
|
|
use Arp\DoctrineQueryFilter\Filter\Exception\FilterException; |
10
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilderInterface; |
11
|
|
|
use Doctrine\ORM\Query\Expr; |
12
|
|
|
use Doctrine\ORM\Query\Expr\Andx as DoctrineAndx; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AndX |
17
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AbstractComposite |
18
|
|
|
*/ |
19
|
|
|
final class AndXTest extends AbstractFilterTest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Assert that the filter implements FilterInterface |
23
|
|
|
* |
24
|
|
|
* @throws FilterException |
25
|
|
|
*/ |
26
|
|
|
public function testImplementsFilterInterface(): void |
27
|
|
|
{ |
28
|
|
|
$criteria = [ |
29
|
|
|
// no filters provided |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$filter = new AndX($this->queryFilterManager, $this->typecaster, $this->paramNameGenerator); |
33
|
|
|
|
34
|
|
|
$this->queryBuilder->expects($this->never())->method('createQueryBuilder'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$filter->filter($this->queryBuilder, $this->metadata, $criteria); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Assert that the andX filter will correctly apply the required filters |
41
|
|
|
* |
42
|
|
|
* @param string|WhereType|null $whereType |
43
|
|
|
* |
44
|
|
|
* @dataProvider getFilterWillApplyTheProvidedConditionsData |
45
|
|
|
* |
46
|
|
|
* @throws FilterException |
47
|
|
|
*/ |
48
|
|
|
public function testFilterWillApplyTheProvidedConditions(WhereType|string|null $whereType): void |
49
|
|
|
{ |
50
|
|
|
$criteria = [ |
51
|
|
|
'conditions' => [ |
52
|
|
|
[ |
53
|
|
|
'name' => 'eq', |
54
|
|
|
'field' => 'test', |
55
|
|
|
'value' => 1, |
56
|
|
|
], |
57
|
|
|
[ |
58
|
|
|
'name' => 'meq', |
59
|
|
|
'field' => 'hello', |
60
|
|
|
'value' => 'test', |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
if (isset($whereType)) { |
66
|
|
|
$criteria['where'] = $whereType; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$filter = new AndX($this->queryFilterManager, $this->typecaster, $this->paramNameGenerator); |
70
|
|
|
|
71
|
|
|
$className = 'FooEntity'; |
72
|
|
|
|
73
|
|
|
/** @var QueryBuilderInterface&MockObject $qb */ |
74
|
|
|
$qb = $this->createMock(QueryBuilderInterface::class); |
75
|
|
|
|
76
|
|
|
$this->queryBuilder->expects($this->once()) |
77
|
|
|
->method('createQueryBuilder') |
78
|
|
|
->willReturn($qb); |
79
|
|
|
|
80
|
|
|
$this->metadata->expects($this->once()) |
|
|
|
|
81
|
|
|
->method('getName') |
82
|
|
|
->willReturn($className); |
83
|
|
|
|
84
|
|
|
$this->queryFilterManager->expects($this->once()) |
|
|
|
|
85
|
|
|
->method('filter') |
86
|
|
|
->with($qb, $className, ['filters' => $criteria['conditions']]); |
87
|
|
|
|
88
|
|
|
/** @var DoctrineAndx&MockObject $where */ |
89
|
|
|
$where = $this->createMock(DoctrineAndx::class); |
90
|
|
|
|
91
|
|
|
$queryParts = [ |
92
|
|
|
'where' => $where, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$qb->expects($this->once()) |
96
|
|
|
->method('getQueryParts') |
97
|
|
|
->willReturn($queryParts); |
98
|
|
|
|
99
|
|
|
/** @var Expr&MockObject $expr */ |
100
|
|
|
$expr = $this->createMock(Expr::class); |
101
|
|
|
|
102
|
|
|
$this->queryBuilder->expects($this->once()) |
103
|
|
|
->method('expr') |
104
|
|
|
->willReturn($expr); |
105
|
|
|
|
106
|
|
|
/** @var DoctrineAndx&MockObject $doctrineAndX */ |
107
|
|
|
$doctrineAndX = $this->createMock(DoctrineAndx::class); |
108
|
|
|
|
109
|
|
|
$expr->expects($this->once()) |
110
|
|
|
->method('andX') |
111
|
|
|
->willReturn($doctrineAndX); |
112
|
|
|
|
113
|
|
|
/** @var Expr\Comparison[] $whereParts */ |
114
|
|
|
$whereParts = [ |
115
|
|
|
$this->createMock(Expr\Comparison::class), |
116
|
|
|
$this->createMock(Expr\Comparison::class), |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$where->expects($this->once()) |
120
|
|
|
->method('getParts') |
121
|
|
|
->willReturn($whereParts); |
122
|
|
|
|
123
|
|
|
$doctrineAndX->expects($this->once()) |
124
|
|
|
->method('addMultiple') |
125
|
|
|
->with($whereParts); |
126
|
|
|
|
127
|
|
|
$this->queryBuilder->expects($this->once()) |
128
|
|
|
->method('andWhere') |
129
|
|
|
->with($doctrineAndX); |
130
|
|
|
|
131
|
|
|
$this->queryBuilder->expects($this->once()) |
132
|
|
|
->method('mergeParameters') |
133
|
|
|
->with($qb); |
134
|
|
|
|
135
|
|
|
$filter->filter($this->queryBuilder, $this->metadata, $criteria); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array<mixed> |
140
|
|
|
*/ |
141
|
|
|
public function getFilterWillApplyTheProvidedConditionsData(): array |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
[null], |
145
|
|
|
[WhereType::AND], |
146
|
|
|
[WhereType::AND->value], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.