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