Completed
Push — master ( 704539...2a5253 )
by Robbie
02:16
created

testCanImportPsr7InterfaceToHttpResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
nc 1
cc 1
eloc 9
nop 0
1
<?php
2
3
namespace Robbie\Psr7\Tests;
4
5
use Robbie\Psr7\HttpResponseAdapter;
6
use Psr\Http\Message\ResponseInterface;
7
use SilverStripe\Control\HTTPResponse;
8
9
/**
10
 * @package psr7-adapters
11
 */
12
class HttpResponseAdapterTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    protected $backupGlobals = false;
18
19
    /**
20
     * Ensure that the Response class returned is a PSR-7 interface
21
     */
22
    public function testReturnsPsr7CompliantClass()
23
    {
24
        $interface = $this->getInterface();
25
26
        $this->assertInstanceOf(ResponseInterface::class, $interface);
27
    }
28
29
    /**
30
     * Assert that the input can be retrieved again
31
     */
32
    public function testBodyMatchesInputBody()
33
    {
34
        $body = json_encode(['success' => true, 'message' => 'The operation was completed.']);
35
        $code = 202;
36
        $message = 'Yeah, accepted...';
37
38
        $interface = $this->getInterface($body, $code, $message);
39
40
        $this->assertSame($body, (string) $interface->getBody());
41
        $this->assertSame($code, $interface->getStatusCode());
42
        $this->assertSame($message, $interface->getReasonPhrase());
43
    }
44
45
    /**
46
     * Test that without providing a status code, the default is 200
47
     */
48
    public function testSuccessfulByDefault()
49
    {
50
        $interface = $this->getInterface();
51
52
        $this->assertSame(200, $interface->getStatusCode());
53
    }
54
55
    /**
56
     * Ensure that as per PSR-7 specifications the interfaces are immutable, and different instances returned
57
     * when we add headers to it (for example)
58
     */
59
    public function testImmutabilyAddHeaders()
60
    {
61
        $interface = $this->getInterface();
62
63
        $new = $interface->withHeader('Content-Type', 'application/json');
64
        $this->assertNotSame($new, $interface);
65
        $this->assertSame(['application/json'], $new->getHeader('Content-Type'));
66
67
        $new = $interface->withHeader('Custom-Header', 'value-here');
68
        $this->assertTrue($new->hasHeader('Custom-Header'));
69
        $this->assertFalse($interface->hasHeader('Custom-Header'));
70
    }
71
72
    /**
73
     * Test that a PSR-7 interface can be imported (with multiple headers converted to a single line) to a
74
     * HTTPResponse class
75
     */
76
    public function testCanImportPsr7InterfaceToHttpResponse()
77
    {
78
        $body = json_encode(['success' => true]);
79
        $interface = $this->getInterface($body, 201, 'Testing');
80
        $interface = $interface->withHeader('Foo', 'bar')->withAddedHeader('Foo', 'baz');
81
82
        $result = (new HttpResponseAdapter)->fromPsr7($interface);
83
84
        $this->assertSame($body, $result->getBody());
85
        $this->assertSame(201, $result->getStatusCode());
0 ignored issues
show
Bug introduced by
The method getStatusCode does only exist in SilverStripe\Control\HTTPResponse, but not in SilverStripe\Control\HTTPRequest.

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...
86
        $this->assertSame('Testing', $result->getStatusDescription());
0 ignored issues
show
Bug introduced by
The method getStatusDescription does only exist in SilverStripe\Control\HTTPResponse, but not in SilverStripe\Control\HTTPRequest.

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...
87
        $this->assertSame('bar, baz', $result->getHeader('Foo'));
88
    }
89
90
    /**
91
     * Create a mocked HTTP response for "adapting"
92
     *
93
     * @param  string $body              The body of the response
94
     * @param  int    $statusCode        The numeric status code - 200, 404, etc
95
     * @param  string $statusDescription The text to be given alongside the status code
96
     * @return \Psr\Http\Message\ResponseInterface
97
     */
98
    protected function getInterface($body = null, $statusCode = null, $statusDescription = null)
99
    {
100
        $httpRequest = new HTTPResponse($body, $statusCode, $statusDescription);
101
        $adapter = new HttpResponseAdapter;
102
        return $adapter->toPsr7($httpRequest);
103
    }
104
}
105