|
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\Component\Grid\Sorting; |
|
15
|
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
|
17
|
|
|
use Sylius\Component\Grid\Data\DataSourceInterface; |
|
18
|
|
|
use Sylius\Component\Grid\Data\ExpressionBuilderInterface; |
|
19
|
|
|
use Sylius\Component\Grid\Definition\Field; |
|
20
|
|
|
use Sylius\Component\Grid\Definition\Grid; |
|
21
|
|
|
use Sylius\Component\Grid\Parameters; |
|
22
|
|
|
use Sylius\Component\Grid\Sorting\SorterInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class SorterSpec extends ObjectBehavior |
|
28
|
|
|
{ |
|
29
|
|
|
function it_implements_grid_data_source_sorter_interface(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->shouldImplement(SorterInterface::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
function it_sorts_the_data_source_via_expression_builder_based_on_the_grid_definition( |
|
35
|
|
|
Grid $grid, |
|
36
|
|
|
Field $nameField, |
|
37
|
|
|
Field $nonSortableField, |
|
38
|
|
|
DataSourceInterface $dataSource, |
|
39
|
|
|
ExpressionBuilderInterface $expressionBuilder |
|
40
|
|
|
): void { |
|
41
|
|
|
$parameters = new Parameters(); |
|
42
|
|
|
|
|
43
|
|
|
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder); |
|
44
|
|
|
|
|
45
|
|
|
$grid->getSorting()->willReturn(['name' => 'desc', 'non_sortable_field' => 'asc']); |
|
46
|
|
|
|
|
47
|
|
|
$grid->hasField('name')->willReturn(true); |
|
48
|
|
|
$grid->getField('name')->willReturn($nameField); |
|
49
|
|
|
$nameField->isSortable()->willReturn(true); |
|
50
|
|
|
$nameField->getSortable()->willReturn('translation.name'); |
|
51
|
|
|
|
|
52
|
|
|
$grid->hasField('non_sortable_field')->willReturn(true); |
|
53
|
|
|
$grid->getField('non_sortable_field')->willReturn($nonSortableField); |
|
54
|
|
|
$nonSortableField->isSortable()->willReturn(false); |
|
55
|
|
|
$nonSortableField->getSortable()->willReturn(null); |
|
56
|
|
|
|
|
57
|
|
|
$expressionBuilder->addOrderBy('translation.name', 'desc')->shouldBeCalled(); |
|
58
|
|
|
$expressionBuilder->addOrderBy(null, 'asc')->shouldNotBeCalled(); |
|
59
|
|
|
|
|
60
|
|
|
$this->sort($dataSource, $grid, $parameters); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function it_sorts_the_data_source_via_expression_builder_based_on_sorting_parameter( |
|
64
|
|
|
Grid $grid, |
|
65
|
|
|
Field $nameField, |
|
66
|
|
|
Field $nonSortableField, |
|
67
|
|
|
DataSourceInterface $dataSource, |
|
68
|
|
|
ExpressionBuilderInterface $expressionBuilder |
|
69
|
|
|
): void { |
|
70
|
|
|
$parameters = new Parameters(['sorting' => ['name' => 'asc', 'non_sortable_field' => 'asc']]); |
|
71
|
|
|
|
|
72
|
|
|
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder); |
|
73
|
|
|
|
|
74
|
|
|
$grid->getSorting()->willReturn(['code' => 'desc']); |
|
75
|
|
|
|
|
76
|
|
|
$grid->hasField('name')->willReturn(true); |
|
77
|
|
|
$grid->getField('name')->willReturn($nameField); |
|
78
|
|
|
$nameField->isSortable()->willReturn(true); |
|
79
|
|
|
$nameField->getSortable()->willReturn('translation.name'); |
|
80
|
|
|
|
|
81
|
|
|
$grid->hasField('non_sortable_field')->willReturn(true); |
|
82
|
|
|
$grid->getField('non_sortable_field')->willReturn($nonSortableField); |
|
83
|
|
|
$nonSortableField->isSortable()->willReturn(false); |
|
84
|
|
|
$nonSortableField->getSortable()->willReturn(null); |
|
85
|
|
|
|
|
86
|
|
|
$expressionBuilder->addOrderBy('translation.name', 'asc')->shouldBeCalled(); |
|
87
|
|
|
$expressionBuilder->addOrderBy(null, 'asc')->shouldNotBeCalled(); |
|
88
|
|
|
|
|
89
|
|
|
$this->sort($dataSource, $grid, $parameters); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|