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

HttpRequestAdapterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 90
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsPsr7CompliantClass() 0 6 1
A testGetImmutableInterfaceAfterChangingQueryParams() 0 11 1
A testPostedDataCanBeReturned() 0 7 1
A testGetHttpRequestFromPsr7Interface() 0 13 1
A getInterface() 0 7 1
A mockRequestData() 0 10 1
1
<?php
2
3
namespace Robbie\Psr7\Tests;
4
5
use Robbie\Psr7\HttpRequestAdapter;
6
use Psr\Http\Message\ServerRequestInterface;
7
use SilverStripe\Control\HTTPRequest;
8
9
/**
10
 * @package psr7-adapters
11
 */
12
class HttpRequestAdapterTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    protected $backupGlobals = false;
18
19
    /**
20
     * Ensure that the Request class returned is a PSR-7 interface
21
     */
22
    public function testReturnsPsr7CompliantClass()
23
    {
24
        $interface = $this->getInterface('GET', '/path/to/request');
25
26
        $this->assertInstanceOf(ServerRequestInterface::class, $interface);
27
    }
28
29
    /**
30
     * Ensure that the returned interface is immutable as per PSR-7 specs
31
     */
32
    public function testGetImmutableInterfaceAfterChangingQueryParams()
33
    {
34
        $original = $this->getInterface('GET', '/path/to/request', ['query' => 'string', 'values' => 'here']);
35
        $new = $original->withQueryParams(['foo' => 'bar']);
36
37
        $this->assertNotSame($original, $new);
38
        $this->assertSame('here', $original->getQueryParams()['values']);
39
        $this->assertArrayNotHasKey('bar', $original->getQueryParams());
40
        $this->assertArrayNotHasKey('here', $new->getQueryParams());
41
        $this->assertSame('bar', $new->getQueryParams()['foo']);
42
    }
43
44
    /**
45
     * Test that POSTed data from the request body can be returned
46
     */
47
    public function testPostedDataCanBeReturned()
48
    {
49
50
        $interface = $this->getInterface('POST', '/path/to/post', [], ['foo' => 'bar'], 'foo=bar');
51
52
        $this->assertSame('foo=bar', (string) $interface->getBody());
53
    }
54
55
    /**
56
     * Test that a PSR-7 interface can be imported into a HTTPRequest interface
57
     */
58
    public function testGetHttpRequestFromPsr7Interface()
59
    {
60
        $interface = $this->getInterface('POST', '/path/to/post', ['abc' => 'def'], ['foo' => 'bar'], 'foo=bar');
61
        $interface = $interface->withHeader('Token', 'foo')->withAddedHeader('Token', 'bar');
62
63
        $result = (new HTTPRequestAdapter)->fromPsr7($interface);
64
65
        $this->assertSame('POST', $result->httpMethod());
0 ignored issues
show
Bug introduced by
The method httpMethod does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

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

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...
67
        $this->assertSame(['abc' => 'def'], $result->getVars());
0 ignored issues
show
Bug introduced by
The method getVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

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...
68
        $this->assertSame(['foo' => 'bar'], $result->postVars());
0 ignored issues
show
Bug introduced by
The method postVars does only exist in SilverStripe\Control\HTTPRequest, but not in SilverStripe\Control\HTTPResponse.

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...
69
        $this->assertSame('foo, bar', $result->getHeader('Token'));
70
    }
71
72
    /**
73
     * @param  string $method
74
     * @param  string $uri
75
     * @param  array  $get
76
     * @param  array  $post
77
     * @param  string $body
78
     * @return ServerRequestInterface
79
     */
80
    protected function getInterface($method, $uri, $get = [], $post = [], $body = null)
81
    {
82
        $httpRequest = new HTTPRequest($method, $uri, $get, $post, $body);
83
        $adapter = new HttpRequestAdapter;
84
        $adapter->setServerVars($this->mockRequestData());
85
        return $adapter->toPsr7($httpRequest);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function mockRequestData()
92
    {
93
        return [
94
            'SERVER_PORT'   => 80,
95
            'PHP_AUTH_USER' => 'foo',
96
            'PHP_AUTH_PW'   => 'bar',
97
            'HTTP_HOST'     => 'example.com',
98
            'REQUEST_URI'   => '/path/to/request?query=string&values=here'
99
        ];
100
    }
101
}
102