testFilterWillApplyTheProvidedConditions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 88
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 88
rs 9.0472
cc 2
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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');
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Arp\DoctrineQueryFilter\QueryBuilderInterface. ( Ignorable by Annotation )

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

34
        $this->queryBuilder->/** @scrutinizer ignore-call */ 
35
                             expects($this->never())->method('createQueryBuilder');

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...
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())
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

80
        $this->metadata->/** @scrutinizer ignore-call */ 
81
                         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...
81
            ->method('getName')
82
            ->willReturn($className);
83
84
        $this->queryFilterManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Arp\DoctrineQueryFilter\...yFilterManagerInterface. ( Ignorable by Annotation )

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

84
        $this->queryFilterManager->/** @scrutinizer ignore-call */ 
85
                                   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...
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