Completed
Push — parameters-resolver ( 40c34c )
by Kamil
20:09
created

ParametersParserSpec::it_should_parse_array_parameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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 Sylius\Bundle\ResourceBundle\Controller\ParametersParserInterface;
16
use Symfony\Component\DependencyInjection\Container;
17
use Symfony\Component\DependencyInjection\ExpressionLanguage;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author Arnaud Langade <[email protected]>
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 * @author Dosena Ishmael <[email protected]>
24
 */
25
final class ParametersParserSpec extends ObjectBehavior
26
{
27
    function it_implements_parameters_parser_interface()
28
    {
29
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
30
        $this->shouldImplement(ParametersParserInterface::class);
31
    }
32
33
    function it_should_parse_string_parameters()
34
    {
35
        $request = new Request();
36
        $request->request->set('string', 'Lorem ipsum');
37
38
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
39
40
        $this
41
            ->parseRequestValues(['nested' => ['string' => '$string']], $request)
42
            ->shouldReturn(['nested' => ['string' => 'Lorem ipsum']])
43
        ;
44
    }
45
46
    function it_should_parse_boolean_parameters()
47
    {
48
        $request = new Request();
49
        $request->request->set('boolean', true);
50
51
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
52
53
        $this
54
            ->parseRequestValues(['nested' => ['boolean' => '$boolean']], $request)
55
            ->shouldReturn(['nested' => ['boolean' => true]])
56
        ;
57
    }
58
59
    function it_should_parse_array_parameters()
60
    {
61
        $request = new Request();
62
        $request->request->set('array', ['foo' => 'bar']);
63
64
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
65
66
        $this
67
            ->parseRequestValues(['nested' => ['array' => '$array']], $request)
68
            ->shouldReturn(['nested' => ['array' => ['foo' => 'bar']]])
69
        ;
70
    }
71
72
    function it_should_parse_expressions()
73
    {
74
        $service = new \stdClass();
75
76
        $container = new Container();
77
        $container->set('service', $service);
78
79
        $expression = new ExpressionLanguage();
80
81
        $request = new Request();
82
83
        $this->beConstructedWith($container, $expression);
84
85
        $this
86
            ->parseRequestValues(['nested' => ['service' => 'expr:service("service")']], $request)
87
            ->shouldReturn(['nested' => ['service' => $service]])
88
        ;
89
    }
90
91
    function it_should_parse_expressions_with_parameters()
92
    {
93
        $service = new \stdClass();
94
95
        $container = new Container();
96
        $container->set('service', $service);
97
98
        $expression = new ExpressionLanguage();
99
100
        $request = new Request();
101
        $request->request->set('serviceName', 'service');
102
103
        $this->beConstructedWith($container, $expression);
104
105
        $this
106
            ->parseRequestValues(['nested' => ['service' => 'expr:service($serviceName)']], $request)
107
            ->shouldReturn(['nested' => ['service' => $service]])
108
        ;
109
    }
110
111
    function it_should_throw_an_exception_if_array_parameter_is_injected_into_expression()
112
    {
113
        $request = new Request();
114
        $request->request->set('array', ['foo', 'bar']);
115
116
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
117
118
        $this
119
            ->shouldThrow(\InvalidArgumentException::class)
120
            ->during('parseRequestValues', [['contains' => 'expr:"foo" in $array'], $request])
121
        ;
122
    }
123
124
    function it_should_throw_an_exception_if_object_parameter_is_injected_into_expression()
125
    {
126
        $request = new Request();
127
        $request->request->set('object', new \stdClass());
128
129
        $this->beConstructedWith(new Container(), new ExpressionLanguage());
130
131
        $this
132
            ->shouldThrow(\InvalidArgumentException::class)
133
            ->during('parseRequestValues', [['object' => 'expr:$object.callMethod()'], $request])
134
        ;
135
    }
136
}
137