HttpResponseAdapterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 22.02 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 24
loc 109
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsPsr7CompliantClass() 0 6 1
A testBodyMatchesInputBody() 0 12 1
A testSuccessfulByDefault() 0 6 1
A testImmutablyAddHeaders() 12 12 1
A testCanImportPsr7InterfaceToHttpResponse() 0 13 1
A getInterface() 0 6 1
A testImmutablyRemoveHeaders() 12 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 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 View Code Duplication
    public function testImmutablyAddHeaders()
0 ignored issues
show
Duplication introduced by Robbie Averill
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...
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 Robbie Averill
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 Robbie Averill
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
     * Ensure that headers can be removed from the response, and that the return is immutable
92
     */
93 View Code Duplication
    public function testImmutablyRemoveHeaders()
0 ignored issues
show
Duplication introduced by Robbie Averill
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...
94
    {
95
        $interface = $this->getInterface();
96
97
        $new = $interface->withHeader('Content-Type', 'application/json');
98
        $this->assertNotSame($new, $interface);
99
        $this->assertSame(['application/json'], $new->getHeader('Content-Type'));
100
101
        $new = $interface->withoutHeader('Content-Type');
102
        $this->assertFalse($new->hasHeader('Content-Type'));
103
        $this->assertTrue($interface->hasHeader('Content-Type'));
104
    }
105
106
    /**
107
     * Create a mocked HTTP response for "adapting"
108
     *
109
     * @param  string $body              The body of the response
110
     * @param  int    $statusCode        The numeric status code - 200, 404, etc
111
     * @param  string $statusDescription The text to be given alongside the status code
112
     * @return \Psr\Http\Message\ResponseInterface
113
     */
114
    protected function getInterface($body = null, $statusCode = null, $statusDescription = null)
115
    {
116
        $httpRequest = new HTTPResponse($body, $statusCode, $statusDescription);
117
        $adapter = new HttpResponseAdapter;
118
        return $adapter->toPsr7($httpRequest);
119
    }
120
}
121