Completed
Push — master ( 91e054...12ace7 )
by Kamil
31:08 queued 26:39
created

RequestConfigurationSpec   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 433
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 7
Bugs 3 Features 2
Metric Value
wmc 38
c 7
b 3
f 2
lcom 0
cbo 7
dl 0
loc 433
rs 8.3999

38 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 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_takes_the_custom_template_if_specified() 0 7 1
A it_generates_form_type() 0 11 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
A it_returns_array_as_redirect_parameters() 0 15 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 8 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 13 1
A it_generates_permission_name() 0 9 1
A it_takes_permission_name_from_parameters_if_provided() 0 7 1
A it_throws_an_exception_when_permission_is_set_as_false_in_parameters_but_still_trying_to_get_it() 0 10 1
A it_has_event_name() 0 5 1
A it_has_section() 0 8 1
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\ResourceBundle\Controller;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use Sylius\Bundle\ResourceBundle\Controller\Parameters;
17
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
18
use Sylius\Component\Resource\Metadata\MetadataInterface;
19
use Symfony\Component\HttpFoundation\ParameterBag;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * @mixin RequestConfiguration
24
 *
25
 * @author Paweł Jędrzejewski <[email protected]>
26
 * @author Arnaud Langade <[email protected]>
27
 */
28
class RequestConfigurationSpec extends ObjectBehavior
29
{
30
    function let(MetadataInterface $metadata, Request $request, Parameters $parameters)
31
    {
32
        $this->beConstructedWith($metadata, $request, $parameters);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType('Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration');
38
    }
39
40
    function it_has_request(Request $request)
41
    {
42
        $this->getRequest()->shouldReturn($request);
43
    }
44
45
    function it_has_metadata(MetadataInterface $metadata)
46
    {
47
        $this->getMetadata()->shouldReturn($metadata);
48
    }
49
    
50
    function it_has_parameters(Parameters $parameters)
51
    {
52
        $this->getParameters()->shouldReturn($parameters);
53
    }
54
55
    function it_checks_if_its_a_html_request(Request $request)
56
    {
57
        $request->getRequestFormat()->willReturn('html');
58
        $this->isHtmlRequest()->shouldReturn(true);
59
60
        $request->getRequestFormat()->willReturn('json');
61
        $this->isHtmlRequest()->shouldReturn(false);
62
    }
63
64
    function it_returns_default_template_names(MetadataInterface $metadata)
65
    {
66
        $metadata->getTemplatesNamespace()->willReturn('SyliusAdminBundle:Product');
67
68
        $this->getDefaultTemplate('index.html')->shouldReturn('SyliusAdminBundle:Product:index.html.twig');
69
        $this->getDefaultTemplate('show.html')->shouldReturn('SyliusAdminBundle:Product:show.html.twig');
70
        $this->getDefaultTemplate('create.html')->shouldReturn('SyliusAdminBundle:Product:create.html.twig');
71
        $this->getDefaultTemplate('update.html')->shouldReturn('SyliusAdminBundle:Product:update.html.twig');
72
        $this->getDefaultTemplate('custom.html')->shouldReturn('SyliusAdminBundle:Product:custom.html.twig');
73
    }
74
75
    function it_takes_the_custom_template_if_specified(MetadataInterface $metadata, Parameters $parameters)
76
    {
77
        $metadata->getTemplatesNamespace()->willReturn('SyliusAdminBundle:Product');
78
        $parameters->get('template', 'SyliusAdminBundle:Product:foo.html.twig')->willReturn('AppBundle:Product:show.html.twig');
79
80
        $this->getTemplate('foo.html')->shouldReturn('AppBundle:Product:show.html.twig');
81
    }
82
83
    function it_generates_form_type(MetadataInterface $metadata, Parameters $parameters)
84
    {
85
        $metadata->getApplicationName()->willReturn('sylius');
86
        $metadata->getName()->willReturn('product');
87
88
        $parameters->get('form', 'sylius_product')->willReturn('sylius_product');
89
        $this->getFormType()->shouldReturn('sylius_product');
90
91
        $parameters->get('form', 'sylius_product')->willReturn('sylius_product_pricing');
92
        $this->getFormType()->shouldReturn('sylius_product_pricing');
93
    }
94
95
    function it_generates_route_names(MetadataInterface $metadata, Parameters $parameters)
96
    {
97
        $metadata->getApplicationName()->willReturn('sylius');
98
        $metadata->getName()->willReturn('product');
99
        $parameters->get('section')->willReturn(null);
100
101
        $this->getRouteName('index')->shouldReturn('sylius_product_index');
102
        $this->getRouteName('show')->shouldReturn('sylius_product_show');
103
        $this->getRouteName('custom')->shouldReturn('sylius_product_custom');
104
105
        $parameters->get('section')->willReturn('admin');
106
        $this->getRouteName('index')->shouldReturn('sylius_admin_product_index');
107
        $this->getRouteName('show')->shouldReturn('sylius_admin_product_show');
108
        $this->getRouteName('custom')->shouldReturn('sylius_admin_product_custom');
109
    }
110
111
    function it_generates_redirect_referer(Parameters $parameters, Request $request, ParameterBag $bag)
112
    {
113
        $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...
114
        $bag->get('referer')->willReturn('http://myurl.com');
115
116
        $parameters->get('redirect')->willReturn(array('referer' => 'http://myurl.com'));
117
118
        $this->getRedirectReferer()->shouldReturn('http://myurl.com');
119
    }
120
121
    function it_generates_redirect_route(MetadataInterface $metadata, Parameters $parameters)
122
    {
123
        $metadata->getApplicationName()->willReturn('sylius');
124
        $metadata->getName()->willReturn('product');
125
        $parameters->get('section')->willReturn(null);
126
127
        $parameters->get('redirect')->willReturn(null);
128
        $this->getRedirectRoute('index')->shouldReturn('sylius_product_index');
129
130
        $parameters->get('redirect')->willReturn(array('route' => 'myRoute'));
131
        $this->getRedirectRoute('show')->shouldReturn('myRoute');
132
133
        $parameters->get('redirect')->willReturn('myRoute');
134
        $this->getRedirectRoute('custom')->shouldReturn('myRoute');
135
    }
136
137
    function it_takes_section_into_account_when_generating_redirect_route(MetadataInterface $metadata, Parameters $parameters)
138
    {
139
        $metadata->getApplicationName()->willReturn('sylius');
140
        $metadata->getName()->willReturn('product');
141
        $parameters->get('section')->willReturn('admin');
142
143
        $parameters->get('redirect')->willReturn(null);
144
        $this->getRedirectRoute('index')->shouldReturn('sylius_admin_product_index');
145
146
        $parameters->get('redirect')->willReturn(array('route' => 'myRoute'));
147
        $this->getRedirectRoute('show')->shouldReturn('myRoute');
148
149
        $parameters->get('redirect')->willReturn('myRoute');
150
        $this->getRedirectRoute('custom')->shouldReturn('myRoute');
151
    }
152
153
    function it_returns_array_as_redirect_parameters(Parameters $parameters)
154
    {
155
        $parameters->get('redirect')->willReturn(null);
156
        $this->getRedirectParameters()->shouldReturn(array());
157
158
        $parameters->get('redirect')->willReturn('string');
159
        $this->getRedirectParameters()->shouldReturn(array());
160
161
        $parameters->get('redirect')->willReturn(array('parameters' => array('myParameter')));
162
        $this->getRedirectParameters()->shouldReturn(array('myParameter'));
163
164
        $parameters->get('redirect')->willReturn(array('parameters' => array('myParameter')));
165
166
        $this->getRedirectParameters('resource')->shouldReturn(array('myParameter'));
167
    }
168
169
    function it_checks_if_limit_is_enabled(Parameters $parameters)
170
    {
171
        $parameters->get('limit', Argument::any())->willReturn(10);
172
        $this->isLimited()->shouldReturn(true);
173
174
        $parameters->get('limit', Argument::any())->willReturn(null);
175
        $this->isLimited()->shouldReturn(false);
176
    }
177
178
    function it_gets_limit(Parameters $parameters)
179
    {
180
        $parameters->get('limit', false)->willReturn(true);
181
        $parameters->get('limit', 10)->willReturn(10);
182
        $this->getLimit()->shouldReturn(10);
183
184
        $parameters->get('limit', false)->willReturn(false);
185
        $parameters->get('limit', 10)->willReturn(null);
186
        $this->getLimit()->shouldReturn(null);
187
    }
188
189
    function it_checks_if_pagination_is_enabled(Parameters $parameters)
190
    {
191
        $parameters->get('paginate', Argument::any())->willReturn(10);
192
        $this->isPaginated()->shouldReturn(true);
193
194
        $parameters->get('paginate', Argument::any())->willReturn(null);
195
        $this->isPaginated()->shouldReturn(false);
196
    }
197
198
    function it_gets_pagination_max_per_page(Parameters $parameters)
199
    {
200
        $parameters->get('paginate', 10)->willReturn(20);
201
        $this->getPaginationMaxPerPage()->shouldReturn(20);
202
203
        $parameters->get('paginate', 10)->willReturn(10);
204
        $this->getPaginationMaxPerPage()->shouldReturn(10);
205
    }
206
207
    function it_checks_if_the_resource_is_filterable(Parameters $parameters)
208
    {
209
        $parameters->get('filterable', Argument::any())->willReturn(true);
210
        $this->isFilterable()->shouldReturn(true);
211
212
        $parameters->get('filterable', Argument::any())->willReturn(null);
213
        $this->isFilterable()->shouldReturn(false);
214
    }
215
216
    function it_has_no_filterable_parameter(Parameters $parameters)
217
    {
218
        $defaultCriteria = array('property' => 'myValue');
219
220
        $parameters->get('criteria', Argument::any())->willReturn(array());
221
        $parameters->get('filterable', false)->willReturn(false);
222
223
        $this->getCriteria($defaultCriteria)->shouldBeArray();
224
        $this->getCriteria($defaultCriteria)->shouldHaveCount(1);
225
    }
226
227
    function it_has_criteria_parameter(Parameters $parameters, Request $request)
228
    {
229
        $criteria = array('property' => 'myNewValue');
230
231
        $parameters->get('filterable', false)->willReturn(true);
232
        $parameters->get('criteria', Argument::any())->willReturn(array());
233
        $request->get('criteria', array())->willReturn($criteria);
234
        $this->getCriteria()->shouldReturn($criteria);
235
    }
236
237
    function it_allows_to_override_criteria_parameter_in_route(Parameters $parameters, Request $request)
238
    {
239
        $criteria = array('property' => 'myValue');
240
        $overriddenCriteria = array('other_property' => 'myNewValue');
241
        $combinedCriteria = array('property' => 'myValue', 'other_property' => 'myNewValue');
242
243
        $parameters->get('filterable', false)->willReturn(true);
244
        $parameters->get('criteria', array())->willReturn($criteria);
245
        $request->get('criteria', array())->willReturn($overriddenCriteria);
246
247
        $this->getCriteria()->shouldReturn($combinedCriteria);
248
249
        $defaultCriteria = array('slug' => 'foo');
250
        $combinedDefaultCriteria = array('property' => 'myValue', 'slug' => 'foo', 'other_property' => 'myNewValue');
251
252
        $parameters->get('filterable', false)->willReturn(true);
253
        $parameters->get('criteria', Argument::any())->willReturn($criteria);
254
        $request->get('criteria', array())->willReturn($overriddenCriteria);
255
256
        $this->getCriteria($defaultCriteria)->shouldReturn($combinedDefaultCriteria);
257
258
        $parameters->get('filterable', false)->willReturn(true);
259
        $parameters->get('criteria', array())->willReturn(array('filter' => 'route'));
260
        $request->get('criteria', array())->willReturn(array('filter' => 'request'));
261
262
        $this->getCriteria(array('filter' => 'default'))->shouldReturn(array('filter' => 'request'));
263
    }
264
265
    function it_checks_if_the_resource_is_sortable(Parameters $parameters)
266
    {
267
        $parameters->get('sortable', Argument::any())->willReturn(true);
268
        $this->isSortable()->shouldReturn(true);
269
270
        $parameters->get('sortable', Argument::any())->willReturn(null);
271
        $this->isSortable()->shouldReturn(false);
272
    }
273
274
    function it_has_sorting_parameter(Parameters $parameters, Request $request)
275
    {
276
        $sorting = array('property' => 'asc');
277
278
        $parameters->get('sortable', false)->willReturn(true);
279
        $parameters->get('sorting', Argument::any())->willReturn($sorting);
280
        $request->get('sorting', array())->willReturn($sorting);
281
282
        $this->getSorting()->shouldReturn($sorting);
283
    }
284
285
    function it_has_no_sortable_parameter(Parameters $parameters)
286
    {
287
        $defaultSorting = array('property' => 'desc');
288
289
        $parameters->get('sorting', Argument::any())->willReturn(array());
290
        $parameters->get('sortable', false)->willReturn(false);
291
292
        $this->getSorting($defaultSorting)->shouldBeArray();
293
        $this->getSorting($defaultSorting)->shouldHaveCount(1);
294
    }
295
296
    function it_allows_to_override_sorting_parameter_in_route(Parameters $parameters, Request $request)
297
    {
298
        $sorting = array('property' => 'desc');
299
        $overriddenSorting = array('other_property' => 'asc');
300
        $combinedSorting = array('other_property' => 'asc', 'property' => 'desc');
301
302
        $parameters->get('sortable', false)->willReturn(true);
303
        $parameters->get('sorting', array())->willReturn($sorting);
304
        $request->get('sorting', array())->willReturn($overriddenSorting);
305
306
        $this->getSorting()->shouldReturn($combinedSorting);
307
308
        $defaultSorting = array('foo' => 'bar');
309
        $combinedDefaultSorting = array('other_property' => 'asc', 'property' => 'desc', 'foo' => 'bar');
310
311
        $parameters->get('sortable', false)->willReturn(true);
312
        $parameters->get('sorting', Argument::any())->willReturn($sorting);
313
        $request->get('sorting', array())->willReturn($overriddenSorting);
314
315
        $this->getSorting($defaultSorting)->shouldReturn($combinedDefaultSorting);
316
317
        $parameters->get('sortable', false)->willReturn(true);
318
        $parameters->get('sorting', array())->willReturn(array('sort' => 'route'));
319
        $request->get('sorting', array())->willReturn(array('sort' => 'request'));
320
321
        $this->getSorting(array('sort' => 'default'))->shouldReturn(array('sort' => 'request'));
322
    }
323
324
    function it_has_repository_method_parameter(Parameters $parameters)
325
    {
326
        $parameters->has('repository')->willReturn(false);
327
        $this->getRepositoryMethod()->shouldReturn(null);
328
329
        $parameters->has('repository')->willReturn(true);
330
        $parameters->get('repository')->willReturn(array('method' => 'findAllEnabled'));
331
332
        $this->getRepositoryMethod()->shouldReturn('findAllEnabled');
333
    }
334
335
    function it_has_repository_arguments_parameter(Parameters $parameters)
336
    {
337
        $parameters->has('repository')->willReturn(false);
338
        $this->getRepositoryArguments()->shouldReturn(array());
339
340
        $repositoryConfiguration = array('arguments' => 'value');
341
        $parameters->has('repository')->willReturn(true);
342
        $parameters->get('repository')->willReturn($repositoryConfiguration);
343
344
        $this->getRepositoryArguments()->shouldReturn(array('value'));
345
346
        $repositoryConfiguration = array('arguments' => array('foo, bar'));
347
        $parameters->has('repository')->willReturn(true);
348
        $parameters->get('repository')->willReturn($repositoryConfiguration);
349
350
        $this->getRepositoryArguments()->shouldReturn(array('foo, bar'));
351
    }
352
353
    function it_has_factory_method_parameter(Parameters $parameters)
354
    {
355
        $parameters->has('factory')->willReturn(false);
356
        $this->getFactoryMethod()->shouldReturn(null);
357
358
        $parameters->has('factory')->willReturn(true);
359
        $parameters->get('factory')->willReturn(array('method' => 'createForPromotion'));
360
361
        $this->getFactoryMethod()->shouldReturn('createForPromotion');
362
    }
363
364
    function it_has_factory_arguments_parameter(Parameters $parameters)
365
    {
366
        $parameters->has('factory')->willReturn(false);
367
        $this->getFactoryArguments()->shouldReturn(array());
368
369
        $factoryConfiguration = array('arguments' => 'value');
370
        $parameters->has('factory')->willReturn(true);
371
        $parameters->get('factory')->willReturn($factoryConfiguration);
372
373
        $this->getFactoryArguments()->shouldReturn(array('value'));
374
375
        $factoryConfiguration = array('arguments' => array('foo, bar'));
376
        $parameters->has('factory')->willReturn(true);
377
        $parameters->get('factory')->willReturn($factoryConfiguration);
378
379
        $this->getFactoryArguments()->shouldReturn(array('foo, bar'));
380
    }
381
382
    function it_has_flash_message_parameter(MetadataInterface $metadata, Parameters $parameters)
383
    {
384
        $metadata->getApplicationName()->willReturn('sylius');
385
        $metadata->getName()->willReturn('product');
386
387
        $parameters->get('flash', 'sylius.product.message')->willReturn('sylius.product.message');
388
        $this->getFlashMessage('message')->shouldReturn('sylius.product.message');
389
390
        $parameters->get('flash', 'sylius.product.flash')->willReturn('sylius.product.myMessage');
391
        $this->getFlashMessage('flash')->shouldReturn('sylius.product.myMessage');
392
    }
393
394
    function it_has_sortable_position_parameter(Parameters $parameters)
395
    {
396
        $parameters->get('sortable_position', 'position')->willReturn('position');
397
        $this->getSortablePosition()->shouldReturn('position');
398
399
        $parameters->get('sortable_position', 'position')->willReturn('myPosition');
400
        $this->getSortablePosition()->shouldReturn('myPosition');
401
    }
402
    
403
    function it_has_permission_unless_defined_as_false_in_parameters(Parameters $parameters)
404
    {
405
        $parameters->has('permission')->willReturn(false);
406
        $this->shouldHavePermission();
407
408
        $parameters->has('permission')->willReturn(true);
409
        $parameters->get('permission')->willReturn('custom_permission');
410
        $this->shouldHavePermission();
411
412
        $parameters->has('permission')->willReturn(true);
413
        $parameters->get('permission')->willReturn(false);
414
        $this->shouldNotHavePermission();
415
    }
416
    
417
    function it_generates_permission_name(MetadataInterface $metadata, Parameters $parameters)
418
    {
419
        $metadata->getApplicationName()->willReturn('sylius');
420
        $metadata->getName()->willReturn('product');
421
        
422
        $parameters->has('permission')->willReturn(false);
423
424
        $this->getPermission('index')->shouldReturn('sylius.product.index');
425
    }
426
427
    function it_takes_permission_name_from_parameters_if_provided(Parameters $parameters)
428
    {
429
        $parameters->has('permission')->willReturn(true);
430
        $parameters->get('permission')->willReturn('app.sales_order.view_pricing');
431
432
        $this->getPermission('index')->shouldReturn('app.sales_order.view_pricing');
433
    }
434
435
    function it_throws_an_exception_when_permission_is_set_as_false_in_parameters_but_still_trying_to_get_it(Parameters $parameters)
436
    {
437
        $parameters->has('permission')->willReturn(true);
438
        $parameters->get('permission')->willReturn(false);
439
440
        $this
441
            ->shouldThrow(\LogicException::class)
442
            ->during('getPermission', array('index'))
443
        ;
444
    }
445
    
446
    function it_has_event_name(Parameters $parameters)
447
    {
448
        $parameters->get('event')->willReturn('foo');
449
        $this->getEvent()->shouldReturn('foo');
450
    }
451
452
    function it_has_section(Parameters $parameters)
453
    {
454
        $parameters->get('section')->willReturn(null);
455
        $this->getSection()->shouldReturn(null);
456
457
        $parameters->get('section')->willReturn('admin');
458
        $this->getSection()->shouldReturn('admin');
459
    }
460
}
461