Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

SlicesTest::testNoSlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
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([
0 ignored issues
show
Bug introduced by
new Laminas\Diactoros\Se...ray('key' => 'value'))) of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ (new ServerRequest())->withParsedBody([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Laminas\Diactoros\Se...ray('key' => 'value'))) of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ (new ServerRequest())->withParsedBody([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Laminas\Diactoros\Se...ray('key' => 'value'))) of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ (new ServerRequest())->withParsedBody([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Laminas\Diactoros\Se...y('name' => 'value')))) of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ (new ServerRequest())->withParsedBody([
Loading history...
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