1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Http; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Spiral\Core\Container; |
17
|
|
|
use Spiral\Http\Request\InputManager; |
18
|
|
|
use Laminas\Diactoros\ServerRequest; |
19
|
|
|
|
20
|
|
|
class SlicesTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Container |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var InputManager |
29
|
|
|
*/ |
30
|
|
|
private $input; |
31
|
|
|
|
32
|
|
|
public function setUp(): void |
33
|
|
|
{ |
34
|
|
|
$this->container = new Container(); |
35
|
|
|
$this->input = new InputManager($this->container); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testNoSlice(): void |
39
|
|
|
{ |
40
|
|
|
$this->container->bind(ServerRequestInterface::class, (new ServerRequest())->withParsedBody([ |
|
|
|
|
41
|
|
|
'array' => [ |
42
|
|
|
'key' => 'value' |
43
|
|
|
] |
44
|
|
|
])); |
45
|
|
|
|
46
|
|
|
$this->assertSame([ |
47
|
|
|
'array' => [ |
48
|
|
|
'key' => 'value' |
49
|
|
|
] |
50
|
|
|
], $this->input->data->all()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testSlice(): void |
54
|
|
|
{ |
55
|
|
|
$this->container->bind(ServerRequestInterface::class, (new ServerRequest())->withParsedBody([ |
|
|
|
|
56
|
|
|
'array' => [ |
57
|
|
|
'key' => 'value' |
58
|
|
|
] |
59
|
|
|
])); |
60
|
|
|
|
61
|
|
|
$this->assertSame([ |
62
|
|
|
'key' => 'value' |
63
|
|
|
], $this->input->withPrefix('array')->data->all()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDeadEnd(): void |
67
|
|
|
{ |
68
|
|
|
$this->container->bind(ServerRequestInterface::class, (new ServerRequest())->withParsedBody([ |
|
|
|
|
69
|
|
|
'array' => [ |
70
|
|
|
'key' => 'value' |
71
|
|
|
] |
72
|
|
|
])); |
73
|
|
|
|
74
|
|
|
$this->assertSame([], $this->input->withPrefix('other')->data->all()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testMultiple(): void |
78
|
|
|
{ |
79
|
|
|
$this->container->bind(ServerRequestInterface::class, (new ServerRequest())->withParsedBody([ |
|
|
|
|
80
|
|
|
'array' => [ |
81
|
|
|
'key' => [ |
82
|
|
|
'name' => 'value' |
83
|
|
|
] |
84
|
|
|
] |
85
|
|
|
])); |
86
|
|
|
|
87
|
|
|
$this->assertSame([ |
88
|
|
|
'name' => 'value' |
89
|
|
|
], $this->input->withPrefix('array.key')->data->all()); |
90
|
|
|
|
91
|
|
|
$input = $this->input->withPrefix('array'); |
92
|
|
|
|
93
|
|
|
$this->assertSame([ |
94
|
|
|
'key' => [ |
95
|
|
|
'name' => 'value' |
96
|
|
|
] |
97
|
|
|
], $input->data->all()); |
98
|
|
|
|
99
|
|
|
$input = $input->withPrefix('key'); |
100
|
|
|
|
101
|
|
|
$this->assertSame([ |
102
|
|
|
'name' => 'value' |
103
|
|
|
], $input->data->all()); |
104
|
|
|
|
105
|
|
|
$input = $input->withPrefix('', false); |
106
|
|
|
|
107
|
|
|
$this->assertSame([ |
108
|
|
|
'array' => [ |
109
|
|
|
'key' => [ |
110
|
|
|
'name' => 'value' |
111
|
|
|
] |
112
|
|
|
] |
113
|
|
|
], $input->data->all()); |
114
|
|
|
|
115
|
|
|
$this->assertSame('value', $input->data('array.key.name')); |
116
|
|
|
$this->assertSame('value', $input->post('array.key.name')); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|