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

ServerRequestTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 34.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 36
loc 105
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testUploadedFiles() 0 14 1
A testServerParams() 0 7 1
A testCookieParams() 12 12 1
A testQueryParams() 12 12 1
A testParsedBody() 12 12 1
A testAttributes() 0 27 1
A testNullAttribute() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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