Completed
Push — master ( 6ec45a...ca732a )
by Oscar
02:30
created

AppTest::testResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 41
rs 8.8571
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
use Zend\Diactoros\ServerRequest;
4
use Zend\Diactoros\Response;
5
use Zend\Diactoros\Uri;
6
use Zend\Diactoros\UploadedFile;
7
use Psr7Unitesting\Assert;
8
9
class AppTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    public function testServerRequest()
12
    {
13
        $request = (new ServerRequest(['DOCUMENT_ROOT' => '/www']))
14
            ->withMethod('GET')
15
            ->withUri(new Uri('http://user:[email protected]/?ola#id'))
16
            ->withQueryParams(['ola' => 'value'])
17
            ->withAttribute('bar', 'foo')
18
            ->withCookieParams(['cookie-name' => 'cookie-value'])
19
            ->withParsedBody(['name' => 'value'])
20
            ->withUploadedFiles([
21
                'file' => new UploadedFile('file.jpg', 50, 0, 'file.jpg', 'image/jpg'),
22
            ])
23
            ->withHeader('Content-Type', 'text/html');
24
25
        Assert::create($request)
0 ignored issues
show
Bug introduced by
The method protocolVersion does only exist in Psr7Unitesting\Request and Psr7Unitesting\Response, but not in Psr7Unitesting\Stream and Psr7Unitesting\Uri.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
26
            ->protocolVersion('1.1')
27
            ->header('Content-Type', 'text/html')
28
            ->hasHeader('Content-Type')
29
            ->hasNotHeader('X-Content-Type')
30
            ->requestTarget('/?ola')
31
            ->uri('http://user:[email protected]/?ola#id')
32
            ->method('GET')
33
            ->attribute('bar', 'foo')
34
            ->hasAttribute('bar')
35
            ->hasParsedBody('name')
36
            ->parsedBody('name', 'value')
37
            ->hasUploadedFile('file')
38
            ->hasQueryParam('ola')
39
            ->queryParam('ola', 'value')
40
            ->cookieParam('cookie-name', 'cookie-value')
41
            ->hasCookieParam('cookie-name')
42
            ->hasServerParam('DOCUMENT_ROOT')
43
            ->serverParam('DOCUMENT_ROOT', '/www')
44
            ->assertUri()
45
                ->scheme('http')
46
                ->authority('user:[email protected]')
47
                ->host('dag.gal')
48
                ->query('ola')
49
                ->fragment('id')
50
                ->path('/')
51
                ->userInfo('user:pass')
52
                ->port(null)
53
                ->end()
54
55
            ->assertBody()
56
                ->isNotWritable()
57
                ->isReadable()
58
                ->isSeekable()
59
                ->size(0);
60
    }
61
62
    public function testResponse()
63
    {
64
        $response = (new Response())
65
            ->withHeader('Content-Type', 'text/html')
66
            ->withStatus(200, 'Ok!');
67
68
        $response->getBody()->write(<<<HTML
69
<!DOCTYPE html>
70
<html>
71
    <head>
72
        <title>Hello world</title>
73
    </head>
74
    <body>
75
        <h1>Ola mundo</h1>
76
        <div class="first">Ola mundo</div>
77
        <div class="second">Ola mundo</div>
78
    </body>
79
</html>
80
HTML
81
);
82
83
    Assert::create($response)
0 ignored issues
show
Bug introduced by
The method protocolVersion does only exist in Psr7Unitesting\Request and Psr7Unitesting\Response, but not in Psr7Unitesting\Stream and Psr7Unitesting\Uri.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
        ->protocolVersion('1.1')
85
        ->header('Content-Type', 'text/html')
86
        ->hasHeader('Content-Type')
87
        ->hasNotHeader('X-Content-Type')
88
        ->statusCode(200)
89
        ->reasonPhrase('Ok!')
90
        ->assertBody()
91
            ->isWritable()
92
            ->isReadable()
93
            ->isSeekable()
94
            ->size(225)
95
            ->assertHtml()
96
                ->count('div', 2)
97
                ->has('h1')
98
                ->hasNot('section')
99
                ->contains('h1', 'mundo')
100
                ->notContains('h1', 'mundor')
101
                ->isValid();
102
    }
103
}
104