Completed
Push — master ( 93a9b6...fec7c1 )
by Tobias
02:09
created

ServerRequestTest::testAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Nyholm\Psr7;
4
5
use Nyholm\Psr7\ServerRequest;
6
use Nyholm\Psr7\UploadedFile;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @covers \Nyholm\Psr7\ServerRequest
11
 */
12
class ServerRequestTest extends TestCase
13
{
14
    public function testUploadedFiles()
15
    {
16
        $request1 = new ServerRequest('GET', '/');
17
18
        $files = [
19
            'file' => new UploadedFile('test', 123, UPLOAD_ERR_OK),
20
        ];
21
22
        $request2 = $request1->withUploadedFiles($files);
23
24
        $this->assertNotSame($request2, $request1);
25
        $this->assertSame([], $request1->getUploadedFiles());
26
        $this->assertSame($files, $request2->getUploadedFiles());
27
    }
28
29
    public function testServerParams()
30
    {
31
        $params = ['name' => 'value'];
32
33
        $request = new ServerRequest('GET', '/', [], null, '1.1', $params);
34
        $this->assertSame($params, $request->getServerParams());
35
    }
36
37 View Code Duplication
    public function testCookieParams()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $request1 = new ServerRequest('GET', '/');
40
41
        $params = ['name' => 'value'];
42
43
        $request2 = $request1->withCookieParams($params);
44
45
        $this->assertNotSame($request2, $request1);
46
        $this->assertEmpty($request1->getCookieParams());
47
        $this->assertSame($params, $request2->getCookieParams());
48
    }
49
50 View Code Duplication
    public function testQueryParams()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $request1 = new ServerRequest('GET', '/');
53
54
        $params = ['name' => 'value'];
55
56
        $request2 = $request1->withQueryParams($params);
57
58
        $this->assertNotSame($request2, $request1);
59
        $this->assertEmpty($request1->getQueryParams());
60
        $this->assertSame($params, $request2->getQueryParams());
61
    }
62
63 View Code Duplication
    public function testParsedBody()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $request1 = new ServerRequest('GET', '/');
66
67
        $params = ['name' => 'value'];
68
69
        $request2 = $request1->withParsedBody($params);
70
71
        $this->assertNotSame($request2, $request1);
72
        $this->assertEmpty($request1->getParsedBody());
73
        $this->assertSame($params, $request2->getParsedBody());
74
    }
75
76
    public function testAttributes()
77
    {
78
        $request1 = new ServerRequest('GET', '/');
79
80
        $request2 = $request1->withAttribute('name', 'value');
81
        $request3 = $request2->withAttribute('other', 'otherValue');
82
        $request4 = $request3->withoutAttribute('other');
83
        $request5 = $request3->withoutAttribute('unknown');
84
85
        $this->assertNotSame($request2, $request1);
86
        $this->assertNotSame($request3, $request2);
87
        $this->assertNotSame($request4, $request3);
88
        $this->assertNotSame($request5, $request4);
89
90
        $this->assertEmpty($request1->getAttributes());
91
        $this->assertEmpty($request1->getAttribute('name'));
92
        $this->assertEquals(
93
            'something',
94
            $request1->getAttribute('name', 'something'),
95
            'Should return the default value'
96
        );
97
98
        $this->assertEquals('value', $request2->getAttribute('name'));
99
        $this->assertEquals(['name' => 'value'], $request2->getAttributes());
100
        $this->assertEquals(['name' => 'value', 'other' => 'otherValue'], $request3->getAttributes());
101
        $this->assertEquals(['name' => 'value'], $request4->getAttributes());
102
    }
103
104
    public function testNullAttribute()
105
    {
106
        $request = (new ServerRequest('GET', '/'))->withAttribute('name', null);
107
108
        $this->assertSame(['name' => null], $request->getAttributes());
109
        $this->assertNull($request->getAttribute('name', 'different-default'));
110
111
        $requestWithoutAttribute = $request->withoutAttribute('name');
112
113
        $this->assertSame([], $requestWithoutAttribute->getAttributes());
114
        $this->assertSame('different-default', $requestWithoutAttribute->getAttribute('name', 'different-default'));
115
    }
116
}
117