RequestIdMiddlewareTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B itSetsAllHeadersAndRequestIdWhenNotGiven() 0 29 1
A itSetsAllHeadersAndNotRequestIdWhenGiven() 0 19 1
1
<?php
2
3
namespace Bunq\Tests\Middleware;
4
5
use Bunq\Middleware\RequestIdMiddleware;
6
use GuzzleHttp\Psr7\Request;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Ramsey\Uuid\Uuid;
10
use Ramsey\Uuid\UuidFactory;
11
12
final class RequestIdMiddlewareTest extends TestCase
13
{
14
    /**
15
     * @var RequestIdMiddleware
16
     */
17
    private $middleware;
18
19
    public function setUp()
20
    {
21
        $this->middleware = new RequestIdMiddleware();
22
    }
23
24
    /**
25
     * @test
26
     */
27
    public function itSetsAllHeadersAndRequestIdWhenNotGiven()
28
    {
29
        $request = new Request('GET', 'uri', ['other-existing-header' => 'value']);
30
31
        // Create a Uuid object from a known UUID string
32
        $stringUuid = '253e0f90-8842-4731-91dd-0191816e6a28';
33
        $uuid       = Uuid::fromString($stringUuid);
34
35
        /** @var UuidFactory|ObjectProphecy $factoryMock */
36
        $factoryMock = $this->prophesize(UuidFactory::class);
37
        $factoryMock->uuid4()->willReturn($uuid);
0 ignored issues
show
Bug introduced by
The method uuid4 does only exist in Ramsey\Uuid\UuidFactory, but not in Prophecy\Prophecy\ObjectProphecy.

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...
38
39
        // Replace the default factory with our mock
40
        Uuid::setFactory($factoryMock->reveal());
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Ramsey\Uuid\UuidFactory.

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...
41
42
        $result = $this->middleware->__invoke($request);
43
44
        $headers         = [
45
            'X-Bunq-Client-Request-Id' => '253e0f90-8842-4731-91dd-0191816e6a28',
46
            'Cache-Control'            => 'no-cache',
47
            'X-Bunq-Geolocation'       => '52.3 4.89 12 100 NL',
48
            'X-Bunq-Language'          => 'nl_NL',
49
            'X-Bunq-Region'            => 'nl_NL',
50
            'other-existing-header'    => 'value',
51
        ];
52
        $expectedRequest = new Request('GET', 'uri', $headers, $request->getBody());
53
54
        $this->assertEquals($expectedRequest, $result);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function itSetsAllHeadersAndNotRequestIdWhenGiven()
61
    {
62
        $givenRequestId = '253e0f90-8842-4731-91dd-0191816e6a29';
63
        $request        = new Request('GET', 'uri', []);
64
65
        $result = $this->middleware->__invoke($request, ['request-id' => $givenRequestId]);
66
67
        $headers         = [
68
            'X-Bunq-Client-Request-Id' => $givenRequestId,
69
            'Cache-Control'            => 'no-cache',
70
            'X-Bunq-Geolocation'       => '52.3 4.89 12 100 NL',
71
            'X-Bunq-Language'          => 'nl_NL',
72
            'X-Bunq-Region'            => 'nl_NL',
73
        ];
74
        $expectedRequest = new Request('GET', 'uri', $headers, $request->getBody());
75
76
        $this->assertEquals($expectedRequest, $result);
77
78
    }
79
}
80
81