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\ExpressionLanguage\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 let() |
28
|
|
|
{ |
29
|
|
|
$this->beConstructedWith(new Container(), new ExpressionLanguage()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function it_implements_parameters_parser_interface() |
33
|
|
|
{ |
34
|
|
|
$this->shouldImplement(ParametersParserInterface::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function it_parses_string_parameters() |
38
|
|
|
{ |
39
|
|
|
$request = new Request(); |
40
|
|
|
$request->request->set('string', 'Lorem ipsum'); |
41
|
|
|
|
42
|
|
|
$this |
43
|
|
|
->parseRequestValues(['nested' => ['string' => '$string']], $request) |
44
|
|
|
->shouldReturn(['nested' => ['string' => 'Lorem ipsum']]) |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function it_parses_boolean_parameters() |
49
|
|
|
{ |
50
|
|
|
$request = new Request(); |
51
|
|
|
$request->request->set('boolean', true); |
52
|
|
|
|
53
|
|
|
$this |
54
|
|
|
->parseRequestValues(['nested' => ['boolean' => '$boolean']], $request) |
55
|
|
|
->shouldReturn(['nested' => ['boolean' => true]]) |
56
|
|
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function it_parses_array_parameters() |
60
|
|
|
{ |
61
|
|
|
$request = new Request(); |
62
|
|
|
$request->request->set('array', ['foo' => 'bar']); |
63
|
|
|
|
64
|
|
|
$this |
65
|
|
|
->parseRequestValues(['nested' => ['array' => '$array']], $request) |
66
|
|
|
->shouldReturn(['nested' => ['array' => ['foo' => 'bar']]]) |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function it_parses_expressions() |
71
|
|
|
{ |
72
|
|
|
$request = new Request(); |
73
|
|
|
|
74
|
|
|
$this |
75
|
|
|
->parseRequestValues(['nested' => ['boolean' => 'expr:"foo" in ["foo", "bar"]']], $request) |
76
|
|
|
->shouldReturn(['nested' => ['boolean' => true]]) |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function it_parses_expressions_with_string_parameters() |
81
|
|
|
{ |
82
|
|
|
$request = new Request(); |
83
|
|
|
$request->request->set('string', 'lorem ipsum'); |
84
|
|
|
|
85
|
|
|
$this |
86
|
|
|
->parseRequestValues(['expression' => 'expr:$string === "lorem ipsum"'], $request) |
87
|
|
|
->shouldReturn(['expression' => true]) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function it_parses_expressions_with_scalar_parameters() |
92
|
|
|
{ |
93
|
|
|
$request = new Request(); |
94
|
|
|
$request->request->set('number', 6); |
95
|
|
|
|
96
|
|
|
$this |
97
|
|
|
->parseRequestValues(['expression' => 'expr:$number === 6'], $request) |
98
|
|
|
->shouldReturn(['expression' => true]) |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function it_throws_an_exception_if_array_parameter_is_injected_into_expression() |
103
|
|
|
{ |
104
|
|
|
$request = new Request(); |
105
|
|
|
$request->request->set('array', ['foo', 'bar']); |
106
|
|
|
|
107
|
|
|
$this |
108
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
109
|
|
|
->during('parseRequestValues', [['expression' => 'expr:"foo" in $array'], $request]) |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function it_throws_an_exception_if_object_parameter_is_injected_into_expression() |
114
|
|
|
{ |
115
|
|
|
$request = new Request(); |
116
|
|
|
$request->request->set('object', new \stdClass()); |
117
|
|
|
|
118
|
|
|
$this |
119
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
120
|
|
|
->during('parseRequestValues', [['expression' => 'expr:$object.callMethod()'], $request]) |
121
|
|
|
; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|