Completed
Push — master ( f9a038...cc9983 )
by Kamil
22:34
created

ExpressionVisitorTest::assertQueryBuilderState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\GridBundle\Tests\DependencyInjection;
13
14
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
15
use Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\ExpressionVisitor;
16
use Doctrine\Common\Collections\Expr\Comparison;
17
use Doctrine\ODM\PHPCR\Query\Builder\AbstractNode;
18
use Doctrine\ODM\PHPCR\Query\Builder\AbstractLeafNode;
19
use Doctrine\ODM\PHPCR\Query\Builder\OperandDynamicField;
20
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintComparison;
21
use Doctrine\ODM\PHPCR\Query\Builder\OperandStaticLiteral;
22
use Doctrine\Common\Collections\Expr\CompositeExpression;
23
use Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\ExtraComparison;
24
25
require(__DIR__ . '/QueryBuilderWalker.php');
26
27
class ExpressionVisitorTest extends \PHPUnit_Framework_TestCase
28
{
29
    private $queryBuilder;
30
    private $visitor;
31
32
    public function setUp()
33
    {
34
        $this->queryBuilder = new QueryBuilder();
35
        $this->visitor = new ExpressionVisitor($this->queryBuilder);
36
    }
37
38
    /**
39
     * @dataProvider provideComparisons
40
     */
41
    public function test_it_should_handle_comparisons($comparator, $expected)
42
    {
43
        $expr = new Comparison('hello', $comparator, 'world');
44
        $this->visitor->dispatch($expr);
45
46
        $this->assertQueryBuilderState($expected);
47
    }
48
49
    public function provideComparisons()
50
    {
51
        return [
52
            [
53
                Comparison::EQ,
54
                'jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
55
            ],
56
            [
57
                Comparison::NEQ,
58
                'jcr.operator.not.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
59
            ],
60
            [
61
                Comparison::LT,
62
                'jcr.operator.less.than ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
63
            ],
64
            [
65
                Comparison::LTE,
66
                'jcr.operator.less.than.or.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
67
            ],
68
            [
69
                Comparison::GT,
70
                'jcr.operator.greater.than ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
71
            ],
72
            [
73
                Comparison::GTE,
74
                'jcr.operator.greater.than.or.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
75
            ],
76
            [
77
                Comparison::CONTAINS,
78
                'jcr.operator.like ( OperandDynamicField(o.hello) OperandStaticLiteral("world") )',
79
            ],
80
            [
81
                ExtraComparison::NOT_CONTAINS,
82
                'ConstraintNot ( jcr.operator.like ( OperandDynamicField(o.hello) OperandStaticLiteral("world") ) )',
83
            ],
84
            [
85
                ExtraComparison::IS_NULL,
86
                'ConstraintNot ( ConstraintFieldIsset("hello") )'
87
            ],
88
            [
89
                ExtraComparison::IS_NOT_NULL,
90
                'ConstraintFieldIsset("hello")'
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * @dataProvider provideEmulateIn
97
     */
98
    public function test_it_should_emulate_in($comparator, array $values, $expected)
99
    {
100
        $expr = new Comparison('hello', $comparator, $values);
101
        $this->visitor->dispatch($expr);
102
103
        $this->assertQueryBuilderState($expected);
104
    }
105
106
    public function provideEmulateIn()
107
    {
108
        return [
109
            [
110
                Comparison::IN,
111
                [ 'one' ],
112
                'ConstraintOrx ( jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("one") ) )'
113
            ],
114
            [
115
                Comparison::IN,
116
                [ 'one', 'two', 'three' ],
117
                'ConstraintOrx ( jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("one") ) jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("two") ) jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("three") ) )',
118
            ],
119
            [
120
                Comparison::NIN,
121
                [ 'one' ],
122
                'ConstraintNot ( ConstraintOrx ( jcr.operator.equal.to ( OperandDynamicField(o.hello) OperandStaticLiteral("one") ) ) )'
123
            ],
124
        ];
125
    }
126
127
    /**
128
     * @dataProvider provideComposite
129
     */
130
    public function test_it_should_handle_a_composite_with_an_arity_of_2($type, $expectedType)
131
    {
132
        $expr1 = new Comparison('hello', Comparison::EQ, 'world');
133
        $expr2 = new Comparison('number', Comparison::GT, 8);
134
        $expr = new CompositeExpression(
135
            $type,
136
            [ $expr1, $expr2 ]
137
        );
138
139
        $this->visitor->dispatch($expr);
140
        $this->assertQueryBuilderState(<<<EOT
141
$expectedType ( 
142
    jcr.operator.equal.to (
143
        OperandDynamicField(o.hello) OperandStaticLiteral("world") 
144
    ) 
145
    jcr.operator.greater.than ( 
146
        OperandDynamicField(o.number) OperandStaticLiteral("8") 
147
    ) 
148
)
149
EOT
150
        );
151
    }
152
153
    public function provideComposite()
154
    {
155
        return [
156
            [ CompositeExpression::TYPE_AND, 'ConstraintAndx' ],
157
            [ CompositeExpression::TYPE_OR, 'ConstraintOrx' ]
158
        ];
159
    }
160
161
    public function test_it_should_handle_a_composite_with_an_arity_of_3()
162
    {
163
        $type = CompositeExpression::TYPE_AND;
164
        $expr1 = new Comparison('hello', Comparison::EQ, 'world');
165
        $expr2 = new Comparison('number', Comparison::GT, 8);
166
        $expr3 = new Comparison('date', Comparison::GT, '2015-12-10');
167
        $expr = new CompositeExpression(
168
            $type,
169
            [ $expr1, $expr2, $expr3 ]
170
        );
171
172
        $this->visitor->dispatch($expr);
173
        $this->assertQueryBuilderState(<<<'EOT'
174
ConstraintAndx ( 
175
    jcr.operator.equal.to ( 
176
        OperandDynamicField(o.hello) OperandStaticLiteral("world") 
177
    ) 
178
    ConstraintAndx ( 
179
        jcr.operator.greater.than ( 
180
            OperandDynamicField(o.number) OperandStaticLiteral("8") 
181
        ) 
182
        jcr.operator.greater.than ( 
183
            OperandDynamicField(o.date) OperandStaticLiteral("2015-12-10")
184
        )
185
    ) 
186
)
187
EOT
188
        );
189
    }
190
191
    public function test_it_should_handle_a_nested_composites()
192
    {
193
        $type = CompositeExpression::TYPE_AND;
194
        $expr1 = new Comparison('hello', Comparison::EQ, 'world');
195
        $expr2 = new Comparison('number', Comparison::GT, 8);
196
        $expr3 = new Comparison('date', Comparison::GT, '2015-12-10');
197
        $expr4 = new Comparison('date', Comparison::LT, '2016-12-10');
198
        $expr5 = new Comparison('date', Comparison::NEQ, '2016-12-10');
199
200
        $comp2 = new CompositeExpression(CompositeExpression::TYPE_AND, [ $expr2, $expr5 ]);
201
        $comp1 = new CompositeExpression(CompositeExpression::TYPE_OR, [ $expr1, $comp2 ]);
202
203
        $expr = new CompositeExpression(
204
            $type,
205
            [ $comp1, $expr3, $expr4 ]
206
        );
207
208
        $this->visitor->dispatch($expr);
209
        $this->assertQueryBuilderState(<<<EOT
210
ConstraintAndx ( 
211
    ConstraintOrx ( 
212
        jcr.operator.equal.to ( 
213
            OperandDynamicField(o.hello) OperandStaticLiteral("world") 
214
        ) 
215
        ConstraintAndx ( 
216
            jcr.operator.greater.than ( 
217
                OperandDynamicField(o.number) OperandStaticLiteral("8") 
218
            ) 
219
            jcr.operator.not.equal.to ( 
220
                OperandDynamicField(o.date) OperandStaticLiteral("2016-12-10")
221
            )
222
        ) 
223
    )
224
    ConstraintAndx ( 
225
        jcr.operator.greater.than ( 
226
            OperandDynamicField(o.date) OperandStaticLiteral("2015-12-10")
227
        )
228
        jcr.operator.less.than ( 
229
            OperandDynamicField(o.date) OperandStaticLiteral("2016-12-10")
230
        )
231
    ) 
232
)
233
EOT
234
        );
235
    }
236
237
    private function assertQueryBuilderState($expected)
238
    {
239
        $converter = new QueryBuilderWalker();
240
        $result = $converter->toString($this->queryBuilder->getChild('where')->getChild());
241
        $expected = preg_replace('{\s{2,}}', ' ', $expected);
242
        $expected = str_replace("\n", ' ', $expected);
243
244
        $this->assertEquals($expected, $result);
245
    }
246
}
247