|
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_string_parameters() |
|
92
|
|
|
{ |
|
93
|
|
|
$request = new Request(); |
|
94
|
|
|
$request->request->set('string', 'lorem ipsum'); |
|
95
|
|
|
|
|
96
|
|
|
$this->beConstructedWith(new Container(), new ExpressionLanguage()); |
|
97
|
|
|
|
|
98
|
|
|
$this |
|
99
|
|
|
->parseRequestValues(['expression' => 'expr:$string === "lorem ipsum"'], $request) |
|
100
|
|
|
->shouldReturn(['expression' => true]) |
|
101
|
|
|
; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
function it_should_parse_expressions_with_scalar_parameters() |
|
105
|
|
|
{ |
|
106
|
|
|
$request = new Request(); |
|
107
|
|
|
$request->request->set('number', 6); |
|
108
|
|
|
|
|
109
|
|
|
$this->beConstructedWith(new Container(), new ExpressionLanguage()); |
|
110
|
|
|
|
|
111
|
|
|
$this |
|
112
|
|
|
->parseRequestValues(['expression' => 'expr:$number === 6'], $request) |
|
113
|
|
|
->shouldReturn(['expression' => true]) |
|
114
|
|
|
; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
function it_should_throw_an_exception_if_array_parameter_is_injected_into_expression() |
|
118
|
|
|
{ |
|
119
|
|
|
$request = new Request(); |
|
120
|
|
|
$request->request->set('array', ['foo', 'bar']); |
|
121
|
|
|
|
|
122
|
|
|
$this->beConstructedWith(new Container(), new ExpressionLanguage()); |
|
123
|
|
|
|
|
124
|
|
|
$this |
|
125
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
|
126
|
|
|
->during('parseRequestValues', [['expression' => 'expr:"foo" in $array'], $request]) |
|
127
|
|
|
; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
function it_should_throw_an_exception_if_object_parameter_is_injected_into_expression() |
|
131
|
|
|
{ |
|
132
|
|
|
$request = new Request(); |
|
133
|
|
|
$request->request->set('object', new \stdClass()); |
|
134
|
|
|
|
|
135
|
|
|
$this->beConstructedWith(new Container(), new ExpressionLanguage()); |
|
136
|
|
|
|
|
137
|
|
|
$this |
|
138
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
|
139
|
|
|
->during('parseRequestValues', [['expression' => 'expr:$object.callMethod()'], $request]) |
|
140
|
|
|
; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|