Completed
Push — master ( e9a797...df9aa1 )
by Mitchel
02:23
created

itWontGetExecutedWhenResponseIsNot401()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Bunq\Tests\Middleware;
4
5
use Bunq\Middleware\RefreshSessionMiddleware;
6
use Bunq\Service\InstallationService;
7
use Bunq\Token\DefaultToken;
8
use Bunq\Token\Storage\TokenStorage;
9
use Bunq\Token\TokenType;
10
use GuzzleHttp\Psr7\Response;
11
use PHPUnit\Framework\TestCase;
12
use Prophecy\Argument;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use Psr\Http\Message\ResponseInterface;
15
16
final class RefreshSessionMiddlewareTest extends TestCase
17
{
18
    /**
19
     * @var InstallationService|ObjectProphecy
20
     */
21
    private $installationService;
22
23
    /**
24
     * @var TokenStorage|ObjectProphecy
25
     */
26
    private $tokenStorage;
27
28
    /**
29
     * @var RefreshSessionMiddleware
30
     */
31
    private $middleware;
32
33
    public function setUp()
34
    {
35
        $this->installationService = $this->prophesize(InstallationService::class);
36
        $this->tokenStorage        = $this->prophesize(TokenStorage::class);
37
38
        $this->middleware = new RefreshSessionMiddleware(
39
            $this->installationService->reveal(),
40
            $this->tokenStorage->reveal()
41
        );
42
    }
43
44
    /**
45
     * @test
46
     */
47 View Code Duplication
    public function itWontGetExecutedWhenResponseIsNot401()
0 ignored issues
show
Duplication introduced by
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...
48
    {
49
        $response = new Response(200, [], '{"ok"}');
50
51
        $this->tokenStorage->load(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::any() is of type object<Prophecy\Argument\Token\AnyValueToken>, but the function expects a object<Bunq\Token\TokenType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method load does only exist in Bunq\Token\Storage\TokenStorage, 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...
52
53
        $resultResponse = $this->middleware->__invoke($response);
54
55
        $this->assertSame($response, $resultResponse);
56
    }
57
58
    /**
59
     * @test
60
     */
61 View Code Duplication
    public function itWontGetExecutedWhenResponseIsNotInsufficientAuthorisation()
0 ignored issues
show
Duplication introduced by
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...
62
    {
63
        $body = '{"Error":[{"error_description": "An other error"}]}';
64
        $response = new Response(401, [], $body);
65
66
        $this->tokenStorage->load(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::any() is of type object<Prophecy\Argument\Token\AnyValueToken>, but the function expects a object<Bunq\Token\TokenType>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method load does only exist in Bunq\Token\Storage\TokenStorage, 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...
67
68
        $resultResponse = $this->middleware->__invoke($response);
69
70
        $this->assertEquals($response, $resultResponse);
71
    }
72
73
    /**
74
     * @test
75
     * @expectedException \Bunq\Exception\SessionWasExpiredException
76
     */
77
    public function itWillRefreshSessionAndThrowsAnExceptionIfInsufficientAuthorisation()
78
    {
79
        $body = '{"Error":[{"error_description": "Insufficient authorisation."}]}';
80
        $response = new Response(401, [], $body);
81
82
        $installationToken = new DefaultToken('Installation Token');
83
        $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN())->willReturn($installationToken);
0 ignored issues
show
Bug introduced by
The method load does only exist in Bunq\Token\Storage\TokenStorage, 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...
84
        $this->installationService->createSession($installationToken)->willReturn('someSessionId');
0 ignored issues
show
Bug introduced by
The method createSession does only exist in Bunq\Service\InstallationService, 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...
85
86
        $sessionToken = DefaultToken::fromString('someSessionId');
87
88
        $this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method save does only exist in Bunq\Token\Storage\TokenStorage, 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...
89
90
        $this->middleware->__invoke($response);
91
    }
92
}
93
94