|
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
|
|
|
|