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

DataSourceSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 11
dl 0
loc 109
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_implements_data_source() 0 4 1
A it_should_restrict_with_or_condition() 0 21 1
A it_should_throw_an_exception_if_an_unknown_condition_is_passed() 0 8 1
A it_should_return_the_expression_builder() 0 6 1
A it_should_get_the_data() 0 9 1
A it_should_set_the_order_on_the_query_builder() 0 21 1
A it_should_set_the_order_on_the_query_builder_as_fields_only() 0 21 1
1
<?php
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 spec\Sylius\Bundle\GridBundle\Doctrine\PHPCRODM;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
17
use Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\DataSource;
18
use Sylius\Component\Grid\Data\DataSourceInterface;
19
use Doctrine\Common\Collections\Expr\Comparison;
20
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
21
use Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\ExpressionBuilder;
22
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintAndx;
23
use Doctrine\Common\Collections\Expr\Value;
24
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintComparison;
25
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintOrx;
26
use Sylius\Component\Grid\Parameters;
27
use Pagerfanta\Pagerfanta;
28
use Doctrine\ODM\PHPCR\Query\Builder\OrderBy;
29
use Doctrine\ODM\PHPCR\Query\Builder\Ordering;
30
31
/**
32
 * @mixin Driver
33
 */
34
class DataSourceSpec extends ObjectBehavior
35
{
36
    function let(QueryBuilder $queryBuilder, ExpressionBuilder $expressionBuilder)
37
    {
38
        $this->beConstructedWith($queryBuilder, $expressionBuilder);
39
    }
40
41
    function it_is_initializable()
42
    {
43
        $this->shouldHaveType(DataSource::class);
44
    }
45
46
    function it_implements_data_source()
47
    {
48
        $this->shouldImplement(DataSourceInterface::class);
49
    }
50
51
    function it_should_restrict_with_or_condition(
52
        Comparison $comparison,
53
        Value $value,
54
        QueryBuilder $queryBuilder,
55
        ConstraintOrx $constraint,
56
        ConstraintComparison $comparisonConstraint
57
    )
58
    {
59
        $queryBuilder->orWhere()->willReturn($constraint);
60
        $value->getValue()->willReturn('value');
61
        $comparison->getValue()->willReturn($value);
62
        $comparison->getField()->willReturn('foo');
63
        $comparison->getOperator()->willReturn('=');
64
65
        $constraint->eq()->willReturn($comparisonConstraint);
66
        $comparisonConstraint->field('o.foo')->willReturn($comparisonConstraint);
67
        $comparisonConstraint->literal('value')->shouldBeCalled()->willReturn($comparisonConstraint);
68
        $comparisonConstraint->end()->shouldBeCalled();
69
70
        $this->restrict($comparison, DataSourceInterface::CONDITION_OR);
71
    }
72
73
    function it_should_throw_an_exception_if_an_unknown_condition_is_passed(
74
        Comparison $comparison
75
    )
76
    {
77
        $this->shouldThrow(
78
            new \RuntimeException('Unknown restrict condition "foo"')
79
        )->during('restrict', [ $comparison, 'foo' ]);
80
    }
81
82
    function it_should_return_the_expression_builder(
83
        ExpressionBuilder $expressionBuilder
84
    )
85
    {
86
        $this->getExpressionBuilder()->shouldReturn($expressionBuilder);
87
    }
88
89
    function it_should_get_the_data(
90
        ExpressionBuilder $expressionBuilder,
91
        Parameters $parameters
92
    )
93
    {
94
        $expressionBuilder->getOrderBys()->willReturn([]);
95
        $parameters->get('page', 1)->willReturn(1);
96
        $this->getData($parameters)->shouldHaveType(Pagerfanta::class);
97
    }
98
99
    function it_should_set_the_order_on_the_query_builder(
100
        QueryBuilder $queryBuilder,
101
        ExpressionBuilder $expressionBuilder,
102
        Parameters $parameters,
103
        OrderBy $orderBy,
104
        Ordering $ordering
105
    )
106
    {
107
        $expressionBuilder->getOrderBys()->willReturn([
108
            'foo' => 'asc',
109
            'bar' => 'desc'
110
        ]);
111
        $queryBuilder->orderBy()->willReturn($orderBy);
112
        $orderBy->asc()->willReturn($ordering);
113
        $orderBy->desc()->willReturn($ordering);
114
        $ordering->field('o.foo')->shouldBeCalled();
115
        $ordering->field('o.bar')->shouldBeCalled();
116
117
        $parameters->get('page', 1)->willReturn(1);
118
        $this->getData($parameters)->shouldHaveType(Pagerfanta::class);
119
    }
120
121
    function it_should_set_the_order_on_the_query_builder_as_fields_only(
122
        QueryBuilder $queryBuilder,
123
        ExpressionBuilder $expressionBuilder,
124
        Parameters $parameters,
125
        OrderBy $orderBy,
126
        Ordering $ordering
127
    )
128
    {
129
        $expressionBuilder->getOrderBys()->willReturn([
130
            'foo',
131
            'bar',
132
        ]);
133
        $queryBuilder->orderBy()->willReturn($orderBy);
134
        $orderBy->asc()->willReturn($ordering);
135
        $orderBy->asc()->willReturn($ordering);
136
        $ordering->field('o.foo')->shouldBeCalled();
137
        $ordering->field('o.bar')->shouldBeCalled();
138
139
        $parameters->get('page', 1)->willReturn(1);
140
        $this->getData($parameters)->shouldHaveType(Pagerfanta::class);
141
    }
142
}
143