CollectionsVisitorTest::testVisitCompositeAnd()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections\Tests\Functional;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Collections\Expr\Comparison;
9
use Doctrine\Common\Collections\Expr\CompositeExpression;
10
use Psi\Bridge\ObjectAgent\Doctrine\Collections\CollectionsVisitor;
11
use Psi\Component\ObjectAgent\Query\Query;
12
13
class CollectionsVisitorTest extends \PHPUnit_Framework_TestCase
14
{
15
    private $visitor;
16
17
    public function setUp()
18
    {
19
        $this->visitor = new CollectionsVisitor(Criteria::expr());
20
    }
21
22
    /**
23
     * It should visit all comparators.
24
     *
25
     * @dataProvider provideComparator
26
     */
27
    public function testComparator(string $type, string $expectedOperator)
28
    {
29
        $expr = $this->visitor->dispatch(Query::comparison($type, 'title', 42));
30
        $this->assertInstanceOf(Comparison::class, $expr);
31
        $this->assertEquals($expectedOperator, $expr->getOperator());
32
        $this->assertEquals('title', $expr->getField());
33
        $this->assertEquals(42, $expr->getValue()->getValue());
34
    }
35
36 View Code Duplication
    public function provideComparator()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        return [
39
            [
40
                'eq',
41
                '=',
42
            ],
43
            [
44
                'neq',
45
                '<>',
46
            ],
47
            [
48
                'gt',
49
                '>',
50
            ],
51
            [
52
                'gte',
53
                '>=',
54
            ],
55
            [
56
                'lte',
57
                '<=',
58
            ],
59
            [
60
                'lt',
61
                '<',
62
            ],
63
            [
64
                'contains',
65
                'CONTAINS',
66
            ],
67
        ];
68
    }
69
70
    /**
71
     * It should throw an exception on unsupported comparators.
72
     *
73
     * @dataProvider provideUnsupportedComparators
74
     *
75
     * @expectedException Psi\Component\ObjectAgent\Exception\BadMethodCallException
76
     */
77
    public function testUnsupportedComparators($comparator)
78
    {
79
        $this->visitor->dispatch(Query::comparison($comparator, 'title', 42));
80
    }
81
82
    public function provideUnsupportedComparators()
83
    {
84
        return [
85
            [
86
                'not_contains',
87
            ],
88
            [
89
                'not_null',
90
            ],
91
        ];
92
    }
93
94
    /**
95
     * It should visit complex comparators.
96
     *
97
     * @dataProvider provideComplexComparator
98
     */
99
    public function testComplexComparator(string $type, $value, \Closure $assertion)
100
    {
101
        $expr = $this->visitor->dispatch(Query::comparison($type, 'title', $value));
102
        $assertion($expr);
103
    }
104
105
    public function provideComplexComparator()
106
    {
107
        return [
108
            [
109
                'null',
110
                null,
111
                function ($expr) {
112
                    $this->assertEquals('=', $expr->getOperator());
113
                    $this->assertNull($expr->getValue()->getValue());
114
                },
115
            ],
116
            [
117
                'in',
118
                [1, 2, 3],
119 View Code Duplication
                function ($expr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
                    $this->assertEquals('IN', $expr->getOperator());
121
                    $this->assertEquals([1, 2, 3], $expr->getValue()->getValue());
122
                },
123
            ],
124
            [
125
                'nin',
126
                [1, 2, 3],
127 View Code Duplication
                function ($expr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
                    $this->assertEquals('NIN', $expr->getOperator());
129
                    $this->assertEquals([1, 2, 3], $expr->getValue()->getValue());
130
                },
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * It should visit and composites.
137
     */
138 View Code Duplication
    public function testVisitCompositeAnd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $expr = $this->visitor->dispatch(
141
            Query::composite('and', Query::comparison('eq', 'title', 42))
142
        );
143
144
        $this->assertInstanceOf(CompositeExpression::class, $expr);
145
        $this->assertEquals('AND', $expr->getType());
146
        $this->assertInstanceOf(Comparison::class, $expr->getExpressionList()[0]);
147
    }
148
149
    /**
150
     * It should visit or composites.
151
     */
152 View Code Duplication
    public function testVisitCompositeOr()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $expr = $this->visitor->dispatch(
155
            Query::composite('or', Query::comparison('eq', 'title', 42))
156
        );
157
158
        $this->assertInstanceOf(CompositeExpression::class, $expr);
159
        $this->assertEquals('OR', $expr->getType());
160
        $this->assertInstanceOf(Comparison::class, $expr->getExpressionList()[0]);
161
    }
162
}
163