Completed
Pull Request — master (#347)
by Łukasz
17:04
created

DependentCRUDElementSpec   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 150
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_is_admin_element() 0 6 1
A it_has_default_route() 0 4 1
A it_returns_null_if_parent_element_does_not_have_data_indexer() 0 12 1
A it_returns_null_if_parent_object_id_is_not_available() 0 15 1
A it_returns_parent_object_if_its_available() 0 16 1
A its_route_parameters_contain_parent_object_id_if_its_available() 0 16 1
A it_throws_exception_when_init_datagrid_does_not_return_instance_of_datagrid() 0 8 1
A it_adds_batch_column_to_datagrid_when_element_allow_delete_objects() 0 21 1
A it_throws_exception_when_init_datasource_does_not_return_instance_of_datasource() 0 8 1
A it_throws_exception_when_init_form_does_not_return_instance_of_form() 0 7 1
A it_has_default_options_values() 0 18 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD;
11
12
use FSi\Bundle\AdminBundle\Admin\CRUD\DataIndexerElement;
13
use FSi\Bundle\AdminBundle\Admin\DependentElement;
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
use FSi\Bundle\AdminBundle\Exception\RuntimeException;
16
use FSi\Component\DataGrid\DataGridFactoryInterface;
17
use FSi\Component\DataGrid\DataGridInterface;
18
use FSi\Component\DataIndexer\DataIndexerInterface;
19
use FSi\Component\DataSource\DataSourceFactoryInterface;
20
use PhpSpec\ObjectBehavior;
21
use Prophecy\Argument;
22
use Symfony\Component\Form\FormFactoryInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
use FSi\Bundle\AdminBundle\spec\fixtures\MyDependentCRUD;
26
use FSi\Bundle\AdminBundle\Admin\CRUD\DependentCRUDElement;
27
use FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement;
28
29
class DependentCRUDElementSpec extends ObjectBehavior
30
{
31
    function let()
32
    {
33
        $this->beAnInstanceOf(MyDependentCRUD::class);
34
        $this->beConstructedWith(array());
35
    }
36
37
    function it_is_admin_element()
38
    {
39
        $this->shouldHaveType(DependentCRUDElement::class);
40
        $this->shouldHaveType(CRUDElement::class);
41
        $this->shouldHaveType(DependentElement::class);
42
    }
43
44
    function it_has_default_route()
45
    {
46
        $this->getRoute()->shouldReturn('fsi_admin_list');
47
    }
48
49
    function it_returns_null_if_parent_element_does_not_have_data_indexer(
50
        RequestStack $requestStack,
51
        Request $currentRequest,
52
        Element $parentElement
53
    ) {
54
        $requestStack->getCurrentRequest()->willReturn($currentRequest);
55
56
        $this->setRequestStack($requestStack);
57
        $this->setParentElement($parentElement);
58
59
        $this->getParentObject()->shouldReturn(null);
60
    }
61
62
    function it_returns_null_if_parent_object_id_is_not_available(
63
        RequestStack $requestStack,
64
        Request $currentRequest,
65
        DataIndexerElement $parentElement,
66
        DataIndexerInterface $parentDataIndexer
67
    ) {
68
        $parentElement->getDataIndexer()->willReturn($parentDataIndexer);
69
        $requestStack->getCurrentRequest()->willReturn($currentRequest);
70
        $currentRequest->get(DependentElement::PARENT_REQUEST_PARAMETER)->willReturn(null);
71
72
        $this->setRequestStack($requestStack);
73
        $this->setParentElement($parentElement);
74
75
        $this->getParentObject()->shouldReturn(null);
76
    }
77
78
    function it_returns_parent_object_if_its_available(
79
        RequestStack $requestStack,
80
        Request $currentRequest,
81
        DataIndexerElement $parentElement,
82
        DataIndexerInterface $parentDataIndexer
83
    ) {
84
        $parentElement->getDataIndexer()->willReturn($parentDataIndexer);
85
        $requestStack->getCurrentRequest()->willReturn($currentRequest);
86
        $currentRequest->get(DependentElement::PARENT_REQUEST_PARAMETER)->willReturn('parent_object_id');
87
        $parentDataIndexer->getData('parent_object_id')->willReturn('parent_object');
88
89
        $this->setRequestStack($requestStack);
90
        $this->setParentElement($parentElement);
91
92
        $this->getParentObject()->shouldReturn('parent_object');
93
    }
94
95
    function its_route_parameters_contain_parent_object_id_if_its_available(
96
        RequestStack $requestStack,
97
        Request $currentRequest,
98
        DataIndexerElement $parentElement,
99
        DataIndexerInterface $parentDataIndexer
100
    ) {
101
        $parentElement->getDataIndexer()->willReturn($parentDataIndexer);
102
        $requestStack->getCurrentRequest()->willReturn($currentRequest);
103
        $currentRequest->get(DependentElement::PARENT_REQUEST_PARAMETER)->willReturn('parent_object_id');
104
105
        $this->setRequestStack($requestStack);
106
        $this->setParentElement($parentElement);
107
108
        $this->getRouteParameters()
109
            ->shouldHaveKeyWithValue(DependentElement::PARENT_REQUEST_PARAMETER, 'parent_object_id');
110
    }
111
112
    function it_throws_exception_when_init_datagrid_does_not_return_instance_of_datagrid(
113
        DataGridFactoryInterface $factory
114
    ) {
115
        $this->setDataGridFactory($factory);
116
        $factory->createDataGrid(Argument::cetera())->willReturn(null);
117
118
        $this->shouldThrow(\TypeError::class)->during('createDataGrid');
119
    }
120
121
    function it_adds_batch_column_to_datagrid_when_element_allow_delete_objects(
122
        DataGridFactoryInterface $factory,
123
        DataGridInterface $datagrid
124
    ) {
125
        $factory->createDataGrid('my_datagrid')->shouldBeCalled()->willReturn($datagrid);
126
        $datagrid->hasColumnType('batch')->shouldBeCalled()->willReturn(false);
127
        $datagrid->addColumn('batch', 'batch', array(
128
            'actions' => array(
129
                'delete' => array(
130
                    'route_name' => 'fsi_admin_batch',
131
                    'additional_parameters' => array('element' => $this->getId()),
132
                    'label' => 'crud.list.batch.delete'
133
                )
134
            ),
135
            'display_order' => -1000
136
        ))->shouldBeCalled();
137
138
        $this->setDataGridFactory($factory);
139
140
        $this->createDataGrid()->shouldReturn($datagrid);
141
    }
142
143
    function it_throws_exception_when_init_datasource_does_not_return_instance_of_datasource(
144
        DataSourceFactoryInterface $factory
145
    ) {
146
        $this->setDataSourceFactory($factory);
147
        $factory->createDataSource(Argument::cetera())->willReturn(null);
148
149
        $this->shouldThrow(\TypeError::class)->during('createDataSource');
150
    }
151
152
    function it_throws_exception_when_init_form_does_not_return_instance_of_form(FormFactoryInterface $factory)
153
    {
154
        $this->setFormFactory($factory);
155
        $factory->create(Argument::cetera())->willReturn(null);
156
157
        $this->shouldThrow(\TypeError::class)->during('createForm', [null]);
158
    }
159
160
    function it_has_default_options_values()
161
    {
162
        $options = $this->getOptions();
163
        $options->shouldHaveKey('allow_delete');
164
        $options->shouldHaveKey('allow_add');
165
        $options->shouldHaveKey('template_crud_list');
166
        $options->shouldHaveKey('template_crud_create');
167
        $options->shouldHaveKey('template_crud_edit');
168
        $options->shouldHaveKey('template_list');
169
        $options->shouldHaveKey('template_form');
170
        $options['allow_delete']->shouldBe(true);
171
        $options['allow_add']->shouldBe(true);
172
        $options['template_crud_list']->shouldBe(null);
173
        $options['template_crud_create']->shouldBe(null);
174
        $options['template_crud_edit']->shouldBe(null);
175
        $options['template_list']->shouldBe(null);
176
        $options['template_form']->shouldBe(null);
177
    }
178
}
179