Completed
Push — 1.0-phpstan ( 5ae83f )
by Kamil
27:50
created

it_converts_an_array_to_grid_definition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 127
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 127
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 88
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Definition;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Component\Grid\Definition\Action;
19
use Sylius\Component\Grid\Definition\ActionGroup;
20
use Sylius\Component\Grid\Definition\ArrayToDefinitionConverterInterface;
21
use Sylius\Component\Grid\Definition\Field;
22
use Sylius\Component\Grid\Definition\Filter;
23
use Sylius\Component\Grid\Definition\Grid;
24
use Sylius\Component\Grid\Event\GridDefinitionConverterEvent;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
27
/**
28
 * @author Paweł Jędrzejewski <[email protected]>
29
 */
30
final class ArrayToDefinitionConverterSpec extends ObjectBehavior
31
{
32
    function let(EventDispatcherInterface $eventDispatcher): void
33
    {
34
        $this->beConstructedWith($eventDispatcher);
35
    }
36
37
    function it_implements_array_to_definition_converter(): void
38
    {
39
        $this->shouldImplement(ArrayToDefinitionConverterInterface::class);
40
    }
41
42
    function it_converts_an_array_to_grid_definition(EventDispatcherInterface $eventDispatcher): void
43
    {
44
        $grid = Grid::fromCodeAndDriverConfiguration(
45
            'sylius_admin_tax_category',
46
            'doctrine/orm',
47
            ['resource' => 'sylius.tax_category']
48
        );
49
50
        $grid->setSorting(['code' => 'desc']);
51
52
        $grid->setLimits([9, 18]);
53
54
        $codeField = Field::fromNameAndType('code', 'string');
55
        $codeField->setLabel('System Code');
56
        $codeField->setPath('method.code');
57
        $codeField->setOptions(['template' => 'bar.html.twig']);
58
        $codeField->setSortable('code');
59
60
        $grid->addField($codeField);
61
62
        $enabledField = Field::fromNameAndType('enabled', 'boolean');
63
        $enabledField->setLabel('Enabled');
64
        $enabledField->setPath('method.enabled');
65
        $enabledField->setSortable('enabled');
66
67
        $grid->addField($enabledField);
68
69
        $statusField = Field::fromNameAndType('status', 'string');
70
        $statusField->setLabel('Status');
71
        $statusField->setSortable('status');
72
73
        $grid->addField($statusField);
74
75
        $nameField = Field::fromNameAndType('name', 'string');
76
        $nameField->setLabel('Name');
77
        $nameField->setSortable('name');
78
79
        $grid->addField($nameField);
80
81
        $titleField = Field::fromNameAndType('title', 'string');
82
        $titleField->setLabel('Title');
83
84
        $grid->addField($titleField);
85
86
        $viewAction = Action::fromNameAndType('view', 'link');
87
        $viewAction->setLabel('Display Tax Category');
88
        $viewAction->setOptions(['foo' => 'bar']);
89
        $defaultActionGroup = ActionGroup::named('default');
90
        $defaultActionGroup->addAction($viewAction);
91
92
        $grid->addActionGroup($defaultActionGroup);
93
94
        $filter = Filter::fromNameAndType('enabled', 'boolean');
95
        $filter->setOptions(['fields' => ['firstName', 'lastName']]);
96
        $filter->setCriteria('true');
97
        $grid->addFilter($filter);
98
99
        $eventDispatcher
100
            ->dispatch('sylius.grid.admin_tax_category', Argument::type(GridDefinitionConverterEvent::class))
101
            ->shouldBeCalled()
102
        ;
103
104
        $definitionArray = [
105
            'driver' => [
106
                'name' => 'doctrine/orm',
107
                'options' => ['resource' => 'sylius.tax_category'],
108
            ],
109
            'sorting' => [
110
                'code' => 'desc',
111
            ],
112
            'limits' => [9, 18],
113
            'fields' => [
114
                'code' => [
115
                    'type' => 'string',
116
                    'label' => 'System Code',
117
                    'path' => 'method.code',
118
                    'sortable' => 'code',
119
                    'options' => [
120
                        'template' => 'bar.html.twig',
121
                    ],
122
                ],
123
                'enabled' => [
124
                    'type' => 'boolean',
125
                    'label' => 'Enabled',
126
                    'path' => 'method.enabled',
127
                    'sortable' => true,
128
                ],
129
                'status' => [
130
                    'type' => 'string',
131
                    'label' => 'Status',
132
                    'sortable' => true,
133
                ],
134
                'name' => [
135
                    'type' => 'string',
136
                    'label' => 'Name',
137
                    'sortable' => null,
138
                ],
139
                'title' => [
140
                    'type' => 'string',
141
                    'label' => 'Title',
142
                    'sortable' => false,
143
                ],
144
            ],
145
            'filters' => [
146
                'enabled' => [
147
                    'type' => 'boolean',
148
                    'options' => [
149
                        'fields' => ['firstName', 'lastName'],
150
                    ],
151
                    'default_value' => 'true',
152
                ],
153
            ],
154
            'actions' => [
155
                'default' => [
156
                    'view' => [
157
                        'type' => 'link',
158
                        'label' => 'Display Tax Category',
159
                        'options' => [
160
                            'foo' => 'bar',
161
                        ],
162
                    ],
163
                ],
164
            ],
165
        ];
166
167
        $this->convert('sylius_admin_tax_category', $definitionArray)->shouldBeLike($grid);
168
    }
169
}
170