Completed
Push — theme-collector ( e0987d )
by Kamil
23:25
created

DataSourceSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\GridBundle\Doctrine\PHPCRODM;
15
16
use Doctrine\Common\Collections\Expr\Comparison;
17
use Doctrine\Common\Collections\Expr\Value;
18
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintComparison;
19
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintOrx;
20
use Doctrine\ODM\PHPCR\Query\Builder\OrderBy;
21
use Doctrine\ODM\PHPCR\Query\Builder\Ordering;
22
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
23
use Doctrine\ODM\PHPCR\Query\Query;
24
use Pagerfanta\Pagerfanta;
25
use PhpSpec\ObjectBehavior;
26
use Prophecy\Argument;
27
use Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\ExpressionBuilderInterface;
28
use Sylius\Component\Grid\Data\DataSourceInterface;
29
use Sylius\Component\Grid\Parameters;
30
31
/**
32
 * @require Doctrine\ODM\PHPCR\DocumentManagerInterface
33
 */
34
final class DataSourceSpec extends ObjectBehavior
35
{
36
    function let(QueryBuilder $queryBuilder, ExpressionBuilderInterface $expressionBuilder): void
37
    {
38
        $this->beConstructedWith($queryBuilder, $expressionBuilder);
39
    }
40
41
    function it_implements_data_source(): void
42
    {
43
        $this->shouldImplement(DataSourceInterface::class);
44
    }
45
46
    function it_should_restrict_with_or_condition(
47
        Comparison $comparison,
48
        Value $value,
49
        QueryBuilder $queryBuilder,
50
        ConstraintOrx $constraint,
51
        ConstraintComparison $comparisonConstraint
52
    ): void {
53
        $queryBuilder->orWhere()->willReturn($constraint);
54
        $value->getValue()->willReturn('value');
55
        $comparison->getValue()->willReturn($value);
56
        $comparison->getField()->willReturn('foo');
57
        $comparison->getOperator()->willReturn('=');
58
59
        $constraint->eq()->willReturn($comparisonConstraint);
60
        $comparisonConstraint->field('o.foo')->willReturn($comparisonConstraint);
61
        $comparisonConstraint->literal('value')->shouldBeCalled()->willReturn($comparisonConstraint);
62
        $comparisonConstraint->end()->shouldBeCalled();
63
64
        $this->restrict($comparison, DataSourceInterface::CONDITION_OR);
65
    }
66
67
    function it_should_throw_an_exception_if_an_unknown_condition_is_passed(
68
        Comparison $comparison
69
    ): void {
70
        $this->shouldThrow(
71
            new \RuntimeException('Unknown restrict condition "foo"')
72
        )->during('restrict', [ $comparison, 'foo' ]);
73
    }
74
75
    function it_should_return_the_expression_builder(
76
        ExpressionBuilderInterface $expressionBuilder
77
    ): void {
78
        $this->getExpressionBuilder()->shouldReturn($expressionBuilder);
79
    }
80
81
    function it_should_get_the_data(
82
        QueryBuilder $queryBuilder,
83
        ExpressionBuilderInterface $expressionBuilder,
84
        Query $query
85
    ): void {
86
        $expressionBuilder->getOrderBys()->willReturn([]);
87
88
        $queryBuilder->orderBy()->willReturn(null);
89
        $queryBuilder->getQuery()->willReturn($query);
90
        $query->setMaxResults(Argument::any())->willReturn($query);
91
        $query->setFirstResult(Argument::any())->willReturn($query);
92
        $query->execute()->willReturn([]);
93
94
        $this->getData(new Parameters(['page' => 1]))->shouldHaveType(Pagerfanta::class);
95
    }
96
97
    function it_should_set_the_order_on_the_query_builder(
98
        QueryBuilder $queryBuilder,
99
        ExpressionBuilderInterface $expressionBuilder,
100
        Query $query,
101
        OrderBy $orderBy,
102
        Ordering $ordering
103
    ): void {
104
        $expressionBuilder->getOrderBys()->willReturn([
105
            'foo' => 'asc',
106
            'bar' => 'desc'
107
        ]);
108
        $queryBuilder->orderBy()->willReturn($orderBy);
109
        $orderBy->asc()->willReturn($ordering);
110
        $orderBy->desc()->willReturn($ordering);
111
        $ordering->field('o.foo')->shouldBeCalled();
112
        $ordering->field('o.bar')->shouldBeCalled();
113
114
        $queryBuilder->getQuery()->willReturn($query);
115
        $query->setMaxResults(Argument::any())->willReturn($query);
116
        $query->setFirstResult(Argument::any())->willReturn($query);
117
        $query->execute()->willReturn([]);
118
119
        $this->getData(new Parameters(['page' => 1]))->shouldHaveType(Pagerfanta::class);
120
    }
121
122
    function it_should_set_the_order_on_the_query_builder_as_fields_only(
123
        QueryBuilder $queryBuilder,
124
        ExpressionBuilderInterface $expressionBuilder,
125
        Query $query,
126
        OrderBy $orderBy,
127
        Ordering $ordering
128
    ): void {
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
        $queryBuilder->getQuery()->willReturn($query);
140
        $query->setMaxResults(Argument::any())->willReturn($query);
141
        $query->setFirstResult(Argument::any())->willReturn($query);
142
        $query->execute()->willReturn([]);
143
144
        $this->getData(new Parameters(['page' => 1]))->shouldHaveType(Pagerfanta::class);
145
    }
146
}
147