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

RequestConfigurationSpec   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 527
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 43
c 0
b 0
f 0
lcom 0
cbo 7
dl 0
loc 527
rs 8.3157

43 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_has_request() 0 4 1
A it_has_metadata() 0 4 1
A it_has_parameters() 0 4 1
A it_checks_if_its_a_html_request() 0 8 1
A it_returns_default_template_names() 0 10 1
A it_returns_default_template_names_for_a_directory_based_templates() 0 10 1
A it_takes_the_custom_template_if_specified() 0 7 1
B it_gets_form_type_and_its_options() 0 24 1
A it_generates_form_type_with_array_configuration() 0 9 1
A it_generates_route_names() 0 15 1
A it_generates_redirect_referer() 0 9 1
A it_generates_redirect_route() 0 15 1
A it_takes_section_into_account_when_generating_redirect_route() 0 15 1
B it_returns_array_as_redirect_parameters() 0 32 1
A it_checks_if_limit_is_enabled() 0 8 1
A it_gets_limit() 0 10 1
A it_checks_if_pagination_is_enabled() 0 14 1
A it_gets_pagination_max_per_page() 0 8 1
A it_checks_if_the_resource_is_filterable() 0 8 1
A it_has_no_filterable_parameter() 0 10 1
A it_has_criteria_parameter() 0 9 1
B it_allows_to_override_criteria_parameter_in_route() 0 27 1
A it_checks_if_the_resource_is_sortable() 0 8 1
A it_has_sorting_parameter() 0 10 1
A it_has_no_sortable_parameter() 0 10 1
B it_allows_to_override_sorting_parameter_in_route() 0 27 1
A it_has_repository_method_parameter() 0 10 1
A it_has_repository_arguments_parameter() 0 17 1
A it_has_factory_method_parameter() 0 10 1
A it_has_factory_arguments_parameter() 0 17 1
A it_has_flash_message_parameter() 0 11 1
A it_has_sortable_position_parameter() 0 8 1
A it_has_permission_unless_defined_as_false_in_parameters() 0 11 1
A it_generates_permission_name() 0 9 1
A it_takes_permission_name_from_parameters_if_provided() 0 6 1
A it_throws_an_exception_when_permission_is_set_as_false_in_parameters_but_still_trying_to_get_it() 0 9 1
A it_has_event_name() 0 5 1
A it_has_section() 0 8 1
A it_has_vars() 0 5 1
A it_does_not_have_grid_unless_defined_as_in_parameters() 0 13 1
A it_throws_an_exception_when_trying_to_retrieve_undefined_grid() 0 9 1
A it_can_have_state_machine_transition() 0 15 1

How to fix   Complexity   

Complex Class

Complex classes like RequestConfigurationSpec often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RequestConfigurationSpec, and based on these observations, apply Extract Interface, too.

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\ResourceBundle\Controller;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Bundle\ResourceBundle\Controller\Parameters;
19
use Sylius\Component\Resource\Metadata\MetadataInterface;
20
use Symfony\Component\HttpFoundation\ParameterBag;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * @author Paweł Jędrzejewski <[email protected]>
25
 * @author Arnaud Langade <[email protected]>
26
 */
27
final class RequestConfigurationSpec extends ObjectBehavior
28
{
29
    function let(MetadataInterface $metadata, Request $request, Parameters $parameters): void
30
    {
31
        $this->beConstructedWith($metadata, $request, $parameters);
32
    }
33
34
    function it_has_request(Request $request): void
35
    {
36
        $this->getRequest()->shouldReturn($request);
37
    }
38
39
    function it_has_metadata(MetadataInterface $metadata): void
40
    {
41
        $this->getMetadata()->shouldReturn($metadata);
42
    }
43
44
    function it_has_parameters(Parameters $parameters): void
45
    {
46
        $this->getParameters()->shouldReturn($parameters);
47
    }
48
49
    function it_checks_if_its_a_html_request(Request $request): void
50
    {
51
        $request->getRequestFormat()->willReturn('html');
52
        $this->isHtmlRequest()->shouldReturn(true);
53
54
        $request->getRequestFormat()->willReturn('json');
55
        $this->isHtmlRequest()->shouldReturn(false);
56
    }
57
58
    function it_returns_default_template_names(MetadataInterface $metadata): void
59
    {
60
        $metadata->getTemplatesNamespace()->willReturn('SyliusAdminBundle:Product');
61
62
        $this->getDefaultTemplate('index.html')->shouldReturn('SyliusAdminBundle:Product:index.html.twig');
63
        $this->getDefaultTemplate('show.html')->shouldReturn('SyliusAdminBundle:Product:show.html.twig');
64
        $this->getDefaultTemplate('create.html')->shouldReturn('SyliusAdminBundle:Product:create.html.twig');
65
        $this->getDefaultTemplate('update.html')->shouldReturn('SyliusAdminBundle:Product:update.html.twig');
66
        $this->getDefaultTemplate('custom.html')->shouldReturn('SyliusAdminBundle:Product:custom.html.twig');
67
    }
68
69
    function it_returns_default_template_names_for_a_directory_based_templates(MetadataInterface $metadata): void
70
    {
71
        $metadata->getTemplatesNamespace()->willReturn('book/Backend');
72
73
        $this->getDefaultTemplate('index.html')->shouldReturn('book/Backend/index.html.twig');
74
        $this->getDefaultTemplate('show.html')->shouldReturn('book/Backend/show.html.twig');
75
        $this->getDefaultTemplate('create.html')->shouldReturn('book/Backend/create.html.twig');
76
        $this->getDefaultTemplate('update.html')->shouldReturn('book/Backend/update.html.twig');
77
        $this->getDefaultTemplate('custom.html')->shouldReturn('book/Backend/custom.html.twig');
78
    }
79
80
    function it_takes_the_custom_template_if_specified(MetadataInterface $metadata, Parameters $parameters): void
81
    {
82
        $metadata->getTemplatesNamespace()->willReturn('SyliusAdminBundle:Product');
83
        $parameters->get('template', 'SyliusAdminBundle:Product:foo.html.twig')->willReturn('AppBundle:Product:show.html.twig');
84
85
        $this->getTemplate('foo.html')->shouldReturn('AppBundle:Product:show.html.twig');
86
    }
87
88
    function it_gets_form_type_and_its_options(MetadataInterface $metadata, Parameters $parameters): void
89
    {
90
        $parameters->get('form')->willReturn(['type' => 'sylius_custom_resource']);
91
        $this->getFormType()->shouldReturn('sylius_custom_resource');
92
        $this->getFormOptions()->shouldReturn([]);
93
94
        $parameters->get('form')->willReturn('sylius_custom_resource');
95
        $this->getFormType()->shouldReturn('sylius_custom_resource');
96
        $this->getFormOptions()->shouldReturn([]);
97
98
        $parameters->get('form')->willReturn(['type' => 'sylius_custom_resource', 'options' => ['key' => 'value']]);
99
        $this->getFormType()->shouldReturn('sylius_custom_resource');
100
        $this->getFormOptions()->shouldReturn(['key' => 'value']);
101
102
        $metadata->getClass('form')->willReturn('\Fully\Qualified\ClassName');
103
        $parameters->get('form')->willReturn([]);
104
        $this->getFormType()->shouldReturn('\Fully\Qualified\ClassName');
105
        $this->getFormOptions()->shouldReturn([]);
106
107
        $metadata->getClass('form')->willReturn('\Fully\Qualified\ClassName');
108
        $parameters->get('form')->willReturn(['options' => ['key' => 'value']]);
109
        $this->getFormType()->shouldReturn('\Fully\Qualified\ClassName');
110
        $this->getFormOptions()->shouldReturn(['key' => 'value']);
111
    }
112
113
    function it_generates_form_type_with_array_configuration(MetadataInterface $metadata, Parameters $parameters): void
114
    {
115
        $metadata->getApplicationName()->willReturn('sylius');
116
        $metadata->getName()->willReturn('product');
117
118
        $parameters->get('form')->willReturn(['type' => 'sylius_product', 'options' => ['validation_groups' => ['sylius']]]);
119
        $this->getFormType()->shouldReturn('sylius_product');
120
        $this->getFormOptions()->shouldReturn(['validation_groups' => ['sylius']]);
121
    }
122
123
    function it_generates_route_names(MetadataInterface $metadata, Parameters $parameters): void
124
    {
125
        $metadata->getApplicationName()->willReturn('sylius');
126
        $metadata->getName()->willReturn('product');
127
        $parameters->get('section')->willReturn(null);
128
129
        $this->getRouteName('index')->shouldReturn('sylius_product_index');
130
        $this->getRouteName('show')->shouldReturn('sylius_product_show');
131
        $this->getRouteName('custom')->shouldReturn('sylius_product_custom');
132
133
        $parameters->get('section')->willReturn('admin');
134
        $this->getRouteName('index')->shouldReturn('sylius_admin_product_index');
135
        $this->getRouteName('show')->shouldReturn('sylius_admin_product_show');
136
        $this->getRouteName('custom')->shouldReturn('sylius_admin_product_custom');
137
    }
138
139
    function it_generates_redirect_referer(Parameters $parameters, Request $request, ParameterBag $bag): void
140
    {
141
        $request->headers = $bag;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bag of type object<Symfony\Component...oundation\ParameterBag> is incompatible with the declared type object<Symfony\Component...tpFoundation\HeaderBag> of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
142
        $bag->get('referer')->willReturn('http://myurl.com');
143
144
        $parameters->get('redirect')->willReturn(['referer' => 'http://myurl.com']);
145
146
        $this->getRedirectReferer()->shouldReturn('http://myurl.com');
147
    }
148
149
    function it_generates_redirect_route(MetadataInterface $metadata, Parameters $parameters): void
150
    {
151
        $metadata->getApplicationName()->willReturn('sylius');
152
        $metadata->getName()->willReturn('product');
153
        $parameters->get('section')->willReturn(null);
154
155
        $parameters->get('redirect')->willReturn(null);
156
        $this->getRedirectRoute('index')->shouldReturn('sylius_product_index');
157
158
        $parameters->get('redirect')->willReturn(['route' => 'myRoute']);
159
        $this->getRedirectRoute('show')->shouldReturn('myRoute');
160
161
        $parameters->get('redirect')->willReturn('myRoute');
162
        $this->getRedirectRoute('custom')->shouldReturn('myRoute');
163
    }
164
165
    function it_takes_section_into_account_when_generating_redirect_route(MetadataInterface $metadata, Parameters $parameters): void
166
    {
167
        $metadata->getApplicationName()->willReturn('sylius');
168
        $metadata->getName()->willReturn('product');
169
        $parameters->get('section')->willReturn('admin');
170
171
        $parameters->get('redirect')->willReturn(null);
172
        $this->getRedirectRoute('index')->shouldReturn('sylius_admin_product_index');
173
174
        $parameters->get('redirect')->willReturn(['route' => 'myRoute']);
175
        $this->getRedirectRoute('show')->shouldReturn('myRoute');
176
177
        $parameters->get('redirect')->willReturn('myRoute');
178
        $this->getRedirectRoute('custom')->shouldReturn('myRoute');
179
    }
180
181
    function it_returns_array_as_redirect_parameters(Parameters $parameters): void
182
    {
183
        $parameters->get('vars', [])->willReturn([]);
184
        $this->getVars()->shouldReturn([]);
185
186
        $parameters->get('redirect')->willReturn(null);
187
        $this->getRedirectParameters()->shouldReturn([]);
188
189
        $parameters->get('redirect')->willReturn('string');
190
        $this->getRedirectParameters()->shouldReturn([]);
191
192
        $parameters->get('redirect')->willReturn(['parameters' => []]);
193
        $this->getRedirectParameters()->shouldReturn([]);
194
195
        $parameters->get('redirect')->willReturn(['parameters' => ['myParameter']]);
196
        $this->getRedirectParameters()->shouldReturn(['myParameter']);
197
198
        $parameters->get('redirect')->willReturn(['parameters' => ['myParameter']]);
199
        $this->getRedirectParameters('resource')->shouldReturn(['myParameter']);
200
201
        $invalidExtraParameters = ['redirect' => ['parameters' => 'myValue']];
202
        $parameters->get('vars', [])->willReturn($invalidExtraParameters);
203
        $this->getVars()->shouldReturn($invalidExtraParameters);
204
        $parameters->get('redirect')->willReturn(['parameters' => ['myParameter']]);
205
        $this->getRedirectParameters('resource')->shouldReturn(['myParameter']);
206
207
        $validExtraParameters = ['redirect' => ['parameters' => ['myExtraParameter']]];
208
        $parameters->get('vars', [])->willReturn($validExtraParameters);
209
        $this->getVars()->shouldReturn($validExtraParameters);
210
        $parameters->get('redirect')->willReturn(['parameters' => ['myParameter']]);
211
        $this->getRedirectParameters('resource')->shouldReturn(['myParameter', 'myExtraParameter']);
212
    }
213
214
    function it_checks_if_limit_is_enabled(Parameters $parameters): void
215
    {
216
        $parameters->get('limit', Argument::any())->willReturn(10);
217
        $this->isLimited()->shouldReturn(true);
218
219
        $parameters->get('limit', Argument::any())->willReturn(null);
220
        $this->isLimited()->shouldReturn(false);
221
    }
222
223
    function it_gets_limit(Parameters $parameters): void
224
    {
225
        $parameters->get('limit', false)->willReturn(true);
226
        $parameters->get('limit', 10)->willReturn(10);
227
        $this->getLimit()->shouldReturn(10);
228
229
        $parameters->get('limit', false)->willReturn(false);
230
        $parameters->get('limit', 10)->willReturn(null);
231
        $this->getLimit()->shouldReturn(null);
232
    }
233
234
    function it_checks_if_pagination_is_enabled(Parameters $parameters): void
235
    {
236
        $parameters->get('paginate', Argument::any())->willReturn(10);
237
        $this->isPaginated()->shouldReturn(true);
238
239
        $parameters->get('paginate', Argument::any())->willReturn(0);
240
        $this->isPaginated()->shouldReturn(true);
241
242
        $parameters->get('paginate', Argument::any())->willReturn(null);
243
        $this->isPaginated()->shouldReturn(false);
244
245
        $parameters->get('paginate', Argument::any())->willReturn(false);
246
        $this->isPaginated()->shouldReturn(false);
247
    }
248
249
    function it_gets_pagination_max_per_page(Parameters $parameters): void
250
    {
251
        $parameters->get('paginate', 10)->willReturn(20);
252
        $this->getPaginationMaxPerPage()->shouldReturn(20);
253
254
        $parameters->get('paginate', 10)->willReturn(10);
255
        $this->getPaginationMaxPerPage()->shouldReturn(10);
256
    }
257
258
    function it_checks_if_the_resource_is_filterable(Parameters $parameters): void
259
    {
260
        $parameters->get('filterable', Argument::any())->willReturn(true);
261
        $this->isFilterable()->shouldReturn(true);
262
263
        $parameters->get('filterable', Argument::any())->willReturn(null);
264
        $this->isFilterable()->shouldReturn(false);
265
    }
266
267
    function it_has_no_filterable_parameter(Parameters $parameters): void
268
    {
269
        $defaultCriteria = ['property' => 'myValue'];
270
271
        $parameters->get('criteria', Argument::any())->willReturn([]);
272
        $parameters->get('filterable', false)->willReturn(false);
273
274
        $this->getCriteria($defaultCriteria)->shouldBeArray();
275
        $this->getCriteria($defaultCriteria)->shouldHaveCount(1);
276
    }
277
278
    function it_has_criteria_parameter(Parameters $parameters, Request $request): void
279
    {
280
        $criteria = ['property' => 'myNewValue'];
281
282
        $parameters->get('filterable', false)->willReturn(true);
283
        $parameters->get('criteria', Argument::any())->willReturn([]);
284
        $request->get('criteria', [])->willReturn($criteria);
285
        $this->getCriteria()->shouldReturn($criteria);
286
    }
287
288
    function it_allows_to_override_criteria_parameter_in_route(Parameters $parameters, Request $request): void
289
    {
290
        $criteria = ['property' => 'myValue'];
291
        $overriddenCriteria = ['other_property' => 'myNewValue'];
292
        $combinedCriteria = ['property' => 'myValue', 'other_property' => 'myNewValue'];
293
294
        $parameters->get('filterable', false)->willReturn(true);
295
        $parameters->get('criteria', [])->willReturn($criteria);
296
        $request->get('criteria', [])->willReturn($overriddenCriteria);
297
298
        $this->getCriteria()->shouldReturn($combinedCriteria);
299
300
        $defaultCriteria = ['slug' => 'foo'];
301
        $combinedDefaultCriteria = ['property' => 'myValue', 'slug' => 'foo', 'other_property' => 'myNewValue'];
302
303
        $parameters->get('filterable', false)->willReturn(true);
304
        $parameters->get('criteria', Argument::any())->willReturn($criteria);
305
        $request->get('criteria', [])->willReturn($overriddenCriteria);
306
307
        $this->getCriteria($defaultCriteria)->shouldReturn($combinedDefaultCriteria);
308
309
        $parameters->get('filterable', false)->willReturn(true);
310
        $parameters->get('criteria', [])->willReturn(['filter' => 'route']);
311
        $request->get('criteria', [])->willReturn(['filter' => 'request']);
312
313
        $this->getCriteria(['filter' => 'default'])->shouldReturn(['filter' => 'request']);
314
    }
315
316
    function it_checks_if_the_resource_is_sortable(Parameters $parameters): void
317
    {
318
        $parameters->get('sortable', Argument::any())->willReturn(true);
319
        $this->isSortable()->shouldReturn(true);
320
321
        $parameters->get('sortable', Argument::any())->willReturn(null);
322
        $this->isSortable()->shouldReturn(false);
323
    }
324
325
    function it_has_sorting_parameter(Parameters $parameters, Request $request): void
326
    {
327
        $sorting = ['property' => 'asc'];
328
329
        $parameters->get('sortable', false)->willReturn(true);
330
        $parameters->get('sorting', Argument::any())->willReturn($sorting);
331
        $request->get('sorting', [])->willReturn($sorting);
332
333
        $this->getSorting()->shouldReturn($sorting);
334
    }
335
336
    function it_has_no_sortable_parameter(Parameters $parameters): void
337
    {
338
        $defaultSorting = ['property' => 'desc'];
339
340
        $parameters->get('sorting', Argument::any())->willReturn([]);
341
        $parameters->get('sortable', false)->willReturn(false);
342
343
        $this->getSorting($defaultSorting)->shouldBeArray();
344
        $this->getSorting($defaultSorting)->shouldHaveCount(1);
345
    }
346
347
    function it_allows_to_override_sorting_parameter_in_route(Parameters $parameters, Request $request): void
348
    {
349
        $sorting = ['property' => 'desc'];
350
        $overriddenSorting = ['other_property' => 'asc'];
351
        $combinedSorting = ['other_property' => 'asc', 'property' => 'desc'];
352
353
        $parameters->get('sortable', false)->willReturn(true);
354
        $parameters->get('sorting', [])->willReturn($sorting);
355
        $request->get('sorting', [])->willReturn($overriddenSorting);
356
357
        $this->getSorting()->shouldReturn($combinedSorting);
358
359
        $defaultSorting = ['foo' => 'bar'];
360
        $combinedDefaultSorting = ['other_property' => 'asc', 'property' => 'desc', 'foo' => 'bar'];
361
362
        $parameters->get('sortable', false)->willReturn(true);
363
        $parameters->get('sorting', Argument::any())->willReturn($sorting);
364
        $request->get('sorting', [])->willReturn($overriddenSorting);
365
366
        $this->getSorting($defaultSorting)->shouldReturn($combinedDefaultSorting);
367
368
        $parameters->get('sortable', false)->willReturn(true);
369
        $parameters->get('sorting', [])->willReturn(['sort' => 'route']);
370
        $request->get('sorting', [])->willReturn(['sort' => 'request']);
371
372
        $this->getSorting(['sort' => 'default'])->shouldReturn(['sort' => 'request']);
373
    }
374
375
    function it_has_repository_method_parameter(Parameters $parameters): void
376
    {
377
        $parameters->has('repository')->willReturn(false);
378
        $this->getRepositoryMethod()->shouldReturn(null);
379
380
        $parameters->has('repository')->willReturn(true);
381
        $parameters->get('repository')->willReturn(['method' => 'findAllEnabled']);
382
383
        $this->getRepositoryMethod()->shouldReturn('findAllEnabled');
384
    }
385
386
    function it_has_repository_arguments_parameter(Parameters $parameters): void
387
    {
388
        $parameters->has('repository')->willReturn(false);
389
        $this->getRepositoryArguments()->shouldReturn([]);
390
391
        $repositoryConfiguration = ['arguments' => 'value'];
392
        $parameters->has('repository')->willReturn(true);
393
        $parameters->get('repository')->willReturn($repositoryConfiguration);
394
395
        $this->getRepositoryArguments()->shouldReturn(['value']);
396
397
        $repositoryConfiguration = ['arguments' => ['foo, bar']];
398
        $parameters->has('repository')->willReturn(true);
399
        $parameters->get('repository')->willReturn($repositoryConfiguration);
400
401
        $this->getRepositoryArguments()->shouldReturn(['foo, bar']);
402
    }
403
404
    function it_has_factory_method_parameter(Parameters $parameters): void
405
    {
406
        $parameters->has('factory')->willReturn(false);
407
        $this->getFactoryMethod()->shouldReturn(null);
408
409
        $parameters->has('factory')->willReturn(true);
410
        $parameters->get('factory')->willReturn(['method' => 'createForPromotion']);
411
412
        $this->getFactoryMethod()->shouldReturn('createForPromotion');
413
    }
414
415
    function it_has_factory_arguments_parameter(Parameters $parameters): void
416
    {
417
        $parameters->has('factory')->willReturn(false);
418
        $this->getFactoryArguments()->shouldReturn([]);
419
420
        $factoryConfiguration = ['arguments' => 'value'];
421
        $parameters->has('factory')->willReturn(true);
422
        $parameters->get('factory')->willReturn($factoryConfiguration);
423
424
        $this->getFactoryArguments()->shouldReturn(['value']);
425
426
        $factoryConfiguration = ['arguments' => ['foo, bar']];
427
        $parameters->has('factory')->willReturn(true);
428
        $parameters->get('factory')->willReturn($factoryConfiguration);
429
430
        $this->getFactoryArguments()->shouldReturn(['foo, bar']);
431
    }
432
433
    function it_has_flash_message_parameter(MetadataInterface $metadata, Parameters $parameters): void
434
    {
435
        $metadata->getApplicationName()->willReturn('sylius');
436
        $metadata->getName()->willReturn('product');
437
438
        $parameters->get('flash', 'sylius.product.message')->willReturn('sylius.product.message');
439
        $this->getFlashMessage('message')->shouldReturn('sylius.product.message');
440
441
        $parameters->get('flash', 'sylius.product.flash')->willReturn('sylius.product.myMessage');
442
        $this->getFlashMessage('flash')->shouldReturn('sylius.product.myMessage');
443
    }
444
445
    function it_has_sortable_position_parameter(Parameters $parameters): void
446
    {
447
        $parameters->get('sortable_position', 'position')->willReturn('position');
448
        $this->getSortablePosition()->shouldReturn('position');
449
450
        $parameters->get('sortable_position', 'position')->willReturn('myPosition');
451
        $this->getSortablePosition()->shouldReturn('myPosition');
452
    }
453
454
    function it_has_permission_unless_defined_as_false_in_parameters(Parameters $parameters): void
455
    {
456
        $parameters->get('permission', false)->willReturn(false);
457
        $this->shouldNotHavePermission();
458
459
        $parameters->get('permission', false)->willReturn('custom_permission');
460
        $this->shouldHavePermission();
461
462
        $parameters->get('permission', false)->willReturn(false);
463
        $this->shouldNotHavePermission();
464
    }
465
466
    function it_generates_permission_name(MetadataInterface $metadata, Parameters $parameters): void
467
    {
468
        $metadata->getApplicationName()->willReturn('sylius');
469
        $metadata->getName()->willReturn('product');
470
471
        $parameters->get('permission')->willReturn(true);
472
473
        $this->getPermission('index')->shouldReturn('sylius.product.index');
474
    }
475
476
    function it_takes_permission_name_from_parameters_if_provided(Parameters $parameters): void
477
    {
478
        $parameters->get('permission')->willReturn('app.sales_order.view_pricing');
479
480
        $this->getPermission('index')->shouldReturn('app.sales_order.view_pricing');
481
    }
482
483
    function it_throws_an_exception_when_permission_is_set_as_false_in_parameters_but_still_trying_to_get_it(Parameters $parameters): void
484
    {
485
        $parameters->get('permission')->willReturn(null);
486
487
        $this
488
            ->shouldThrow(\LogicException::class)
489
            ->during('getPermission', ['index'])
490
        ;
491
    }
492
493
    function it_has_event_name(Parameters $parameters): void
494
    {
495
        $parameters->get('event')->willReturn('foo');
496
        $this->getEvent()->shouldReturn('foo');
497
    }
498
499
    function it_has_section(Parameters $parameters): void
500
    {
501
        $parameters->get('section')->willReturn(null);
502
        $this->getSection()->shouldReturn(null);
503
504
        $parameters->get('section')->willReturn('admin');
505
        $this->getSection()->shouldReturn('admin');
506
    }
507
508
    function it_has_vars(Parameters $parameters): void
509
    {
510
        $parameters->get('vars', [])->willReturn(['foo' => 'bar']);
511
        $this->getVars()->shouldReturn(['foo' => 'bar']);
512
    }
513
514
    function it_does_not_have_grid_unless_defined_as_in_parameters(Parameters $parameters): void
515
    {
516
        $parameters->has('grid')->willReturn(false);
517
        $this->shouldNotHaveGrid();
518
519
        $parameters->has('grid')->willReturn(true);
520
        $this->shouldHaveGrid();
521
522
        $parameters->has('grid')->willReturn(true);
523
        $parameters->get('grid')->willReturn('sylius_admin_tax_category');
524
525
        $this->getGrid()->shouldReturn('sylius_admin_tax_category');
526
    }
527
528
    function it_throws_an_exception_when_trying_to_retrieve_undefined_grid(Parameters $parameters): void
529
    {
530
        $parameters->has('grid')->willReturn(false);
531
532
        $this
533
            ->shouldThrow(\LogicException::class)
534
            ->during('getGrid')
535
        ;
536
    }
537
538
    function it_can_have_state_machine_transition(Parameters $parameters): void
539
    {
540
        $parameters->has('state_machine')->willReturn(false);
541
        $this->hasStateMachine()->shouldReturn(false);
542
543
        $parameters->has('state_machine')->willReturn(true);
544
        $parameters->get('state_machine')->willReturn([
545
            'graph' => 'sylius_product_review_state',
546
            'transition' => 'approve',
547
        ]);
548
549
        $this->hasStateMachine()->shouldReturn(true);
550
        $this->getStateMachineGraph()->shouldReturn('sylius_product_review_state');
551
        $this->getStateMachineTransition()->shouldReturn('approve');
552
    }
553
}
554