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\FilterInterface; |
9
|
|
|
use Doctrine\ORM\Query\Expr; |
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Alex Patterson <[email protected]> |
14
|
|
|
* @package ArpTest\DoctrineQueryFilter\Filter |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractComparisonTest extends AbstractFilterTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FilterInterface |
20
|
|
|
*/ |
21
|
|
|
protected FilterInterface $filter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected string $filterClassName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected string $expressionMethodName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected string $expressionSymbol; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Prepare the test case dependencies |
40
|
|
|
*/ |
41
|
|
|
public function setUp(): void |
42
|
|
|
{ |
43
|
|
|
parent::setUp(); |
44
|
|
|
|
45
|
|
|
$this->filter = new $this->filterClassName($this->queryFilterManager, $this->typecaster); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Assert that the filter implements FilterInterface |
50
|
|
|
*/ |
51
|
|
|
public function testImplementsFilterInterface(): void |
52
|
|
|
{ |
53
|
|
|
$this->assertInstanceOf(FilterInterface::class, $this->filter); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Assert that the IsGreaterThan query filter can be applied with the provided $criteria |
58
|
|
|
* |
59
|
|
|
* @param array $criteria |
60
|
|
|
* |
61
|
|
|
* @dataProvider getFilterWillApplyFilteringData |
62
|
|
|
*/ |
63
|
|
|
public function testFilterWillApplyFiltering(array $criteria): void |
64
|
|
|
{ |
65
|
|
|
$fieldName = $criteria['field'] ?? 'testFieldName'; |
66
|
|
|
$alias = $criteria['alias'] ?? null; |
67
|
|
|
|
68
|
|
|
$this->metadata->expects($this->once()) |
69
|
|
|
->method('hasField') |
70
|
|
|
->with($fieldName) |
71
|
|
|
->willReturn(true); |
72
|
|
|
|
73
|
|
|
/** @var Expr|MockObject $expr */ |
74
|
|
|
$expr = $this->createMock(Expr::class); |
75
|
|
|
|
76
|
|
|
$this->queryBuilder->expects($this->once()) |
77
|
|
|
->method('expr') |
78
|
|
|
->willReturn($expr); |
79
|
|
|
|
80
|
|
|
/** @var Expr\Comparison|MockObject $comparisonExpr */ |
81
|
|
|
$comparisonExpr = $this->createMock(Expr\Comparison::class); |
82
|
|
|
|
83
|
|
|
if (null === $alias) { |
84
|
|
|
$alias = 'entity'; |
85
|
|
|
$this->queryBuilder->expects($this->once()) |
86
|
|
|
->method('getRootAlias') |
87
|
|
|
->willReturn($alias); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$expressionString = $alias . '.' . $fieldName . ' ' . $this->expressionSymbol; |
91
|
|
|
if (array_key_exists('value', $criteria)) { |
92
|
|
|
$expressionString .= ' :param_name'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$expr->expects($this->once()) |
96
|
|
|
->method($this->expressionMethodName) |
97
|
|
|
->with($alias . '.' . $fieldName) |
98
|
|
|
->willReturn($comparisonExpr); |
99
|
|
|
|
100
|
|
|
$comparisonExpr->expects($this->once()) |
101
|
|
|
->method('__toString') |
102
|
|
|
->willReturn($expressionString); |
103
|
|
|
|
104
|
|
|
$methodName = (!isset($criteria['where']) || WhereType::AND === $criteria['where']) |
105
|
|
|
? 'andWhere' |
106
|
|
|
: 'orWhere'; |
107
|
|
|
|
108
|
|
|
$this->queryBuilder->expects($this->once())->method($methodName); |
109
|
|
|
|
110
|
|
|
if (array_key_exists('value', $criteria)) { |
111
|
|
|
$this->typecaster->expects($this->once()) |
112
|
|
|
->method('typecast') |
113
|
|
|
->with($this->metadata, $fieldName, $criteria['value']) |
114
|
|
|
->willReturn($criteria['value']); |
115
|
|
|
|
116
|
|
|
$this->queryBuilder->expects($this->once()) |
117
|
|
|
->method('setParameter') |
118
|
|
|
->with($this->callback(static function ($argument) { |
119
|
|
|
return is_string($argument); |
120
|
|
|
}), $criteria['value']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->filter->filter($this->queryBuilder, $this->metadata, $criteria); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
abstract public function getFilterWillApplyFilteringData(): array; |
130
|
|
|
} |
131
|
|
|
|