Passed
Pull Request — master (#9)
by Alex
02:41
created

AbstractJoinTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 120
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testFilterWillThrowInvalidArgumentExceptionIfProvidedAnInvalidFieldName() 0 34 1
A testFilterWillApplyExpectedJoinWithoutConditions() 0 19 1
A testInstanceOfFilterInterface() 0 3 1
A testInstanceOfAbstractJoin() 0 3 1
A testFilterWillThrowInvalidArgumentExceptionIfTheRequiredFieldCriteriaIsNotProvided() 0 11 1
A setUp() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DoctrineQueryFilter\Filter;
6
7
use Arp\DoctrineQueryFilter\Enum\JoinConditionType;
8
use Arp\DoctrineQueryFilter\Filter\AbstractJoin;
9
use Arp\DoctrineQueryFilter\Filter\Exception\FilterException;
10
use Arp\DoctrineQueryFilter\Filter\Exception\InvalidArgumentException;
11
use Arp\DoctrineQueryFilter\Filter\FilterInterface;
12
use Arp\DoctrineQueryFilter\QueryBuilderInterface;
13
use Doctrine\ORM\Query\Expr\Base;
14
use Doctrine\ORM\Query\Expr\Composite;
15
use PHPUnit\Framework\MockObject\MockObject;
16
17
/**
18
 * @covers \Arp\DoctrineQueryFilter\Filter\AbstractJoin
19
 */
20
abstract class AbstractJoinTest extends AbstractFilterTest
21
{
22
    protected AbstractJoin $filter;
23
24
    protected string $filterClassName;
25
26
    public function setUp(): void
27
    {
28
        parent::setUp();
29
30
        /** @var AbstractJoin $filter */
31
        $filter = new $this->filterClassName($this->queryFilterManager, $this->typecaster, $this->paramNameGenerator);
32
        $this->filter = $filter;
33
    }
34
35
    public function testInstanceOfFilterInterface(): void
36
    {
37
        $this->assertInstanceOf(FilterInterface::class, $this->filter);
38
    }
39
40
    public function testInstanceOfAbstractJoin(): void
41
    {
42
        $this->assertInstanceOf(AbstractJoin::class, $this->filter);
43
    }
44
45
    /**
46
     * @throws FilterException
47
     */
48
    public function testFilterWillThrowInvalidArgumentExceptionIfTheRequiredFieldCriteriaIsNotProvided(): void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
        $this->expectExceptionMessage(
52
            sprintf(
53
                'The required \'field\' criteria value is missing for filter \'%s\'',
54
                $this->filterClassName
55
            )
56
        );
57
58
        $this->filter->filter($this->queryBuilder, $this->metadata, []);
59
    }
60
61
    /**
62
     * @throws FilterException
63
     */
64
    public function testFilterWillThrowInvalidArgumentExceptionIfProvidedAnInvalidFieldName(): void
65
    {
66
        $entityName = 'Test';
67
        $fieldName = 'foo';
68
        $criteria = [
69
            'field' => $fieldName,
70
        ];
71
72
        $this->metadata->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Arp\DoctrineQueryFilter\Metadata\MetadataInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->metadata->/** @scrutinizer ignore-call */ 
73
                         expects($this->once())

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.

Loading history...
73
            ->method('getName')
74
            ->willReturn($entityName);
75
76
        $this->metadata->expects($this->once())
77
            ->method('hasField')
78
            ->with($fieldName)
79
            ->willReturn(false);
80
81
        $this->metadata->expects($this->once())
82
            ->method('hasAssociation')
83
            ->with($fieldName)
84
            ->willReturn(false);
85
86
        $this->expectException(InvalidArgumentException::class);
87
        $this->expectExceptionMessage(
88
            sprintf(
89
                'Unable to apply query filter \'%s\': '
90
                . 'The entity class \'%s\' has no field or association named \'%s\'',
91
                $this->filterClassName,
92
                $entityName,
93
                $fieldName
94
            )
95
        );
96
97
        $this->filter->filter($this->queryBuilder, $this->metadata, $criteria);
98
    }
99
100
    /**
101
     * @param QueryBuilderInterface&MockObject $queryBuilder
102
     * @param string $fieldName
103
     * @param string $alias
104
     * @param null|string|Composite|Base $joinCondition
105
     * @param JoinConditionType|null $joinConditionType
106
     * @param string|null $indexBy
107
     */
108
    abstract protected function assertFilterJoin(
109
        QueryBuilderInterface $queryBuilder,
110
        string $fieldName,
111
        string $alias,
112
        null|string|Composite|Base $joinCondition = null,
113
        ?JoinConditionType $joinConditionType = null,
114
        ?string $indexBy = null
115
    ): void;
116
117
    /**
118
     * @throws InvalidArgumentException
119
     * @throws FilterException
120
     */
121
    public function testFilterWillApplyExpectedJoinWithoutConditions(): void
122
    {
123
        $fieldName = 'foo';
124
        $fieldAlias = 't';
125
126
        $joinAlias = 'a';
127
        $criteria = [
128
            'field' => $fieldAlias . '.' . $fieldName,
129
            'alias' => $joinAlias,
130
        ];
131
132
        $this->metadata->expects($this->once())
133
            ->method('hasField')
134
            ->with($fieldName)
135
            ->willReturn(true);
136
137
        $this->assertFilterJoin($this->queryBuilder, $fieldAlias . '.' . $fieldName, $joinAlias);
138
139
        $this->filter->filter($this->queryBuilder, $this->metadata, $criteria);
140
    }
141
}
142