HttpClientFactoryTest::itCreatesADefaultClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
rs 9.5555
c 1
b 0
f 0
cc 1
eloc 36
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bunq\Tests;
4
5
use Bunq\Certificate\CertificateType;
6
use Bunq\Certificate\DefaultCertificate;
7
use Bunq\Certificate\Storage\CertificateStorage;
8
use Bunq\HttpClientFactory;
9
use Bunq\Middleware\RefreshSessionMiddleware;
10
use Bunq\Middleware\RequestAuthenticationMiddleware;
11
use Bunq\Middleware\RequestIdMiddleware;
12
use Bunq\Middleware\RequestSignatureMiddleware;
13
use Bunq\Middleware\ResponseSignatureMiddleware;
14
use Bunq\Service\InstallationService;
15
use Bunq\Service\TokenService;
16
use Bunq\Token\DefaultToken;
17
use Bunq\Token\Storage\TokenStorage;
18
use GuzzleHttp\HandlerStack;
19
use GuzzleHttp\Middleware;
20
use PHPUnit\Framework\TestCase;
21
use Prophecy\Prophecy\ObjectProphecy;
22
23
final class HttpClientFactoryTest extends TestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function itCreatesAInstallationClient()
29
    {
30
        $baseUrl            = 'uri';
31
        $privateCertificate = DefaultCertificate::fromString('private-certificate');
32
33
        /** @var CertificateStorage|ObjectProphecy $certificateStorage */
34
        $certificateStorage = $this->prophesize(CertificateStorage::class);
35
        $certificateStorage->load(CertificateType::PRIVATE_KEY())->willReturn($privateCertificate);
0 ignored issues
show
Bug introduced by
The method load does only exist in Bunq\Certificate\Storage\CertificateStorage, 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...
36
37
        $client = HttpClientFactory::createInstallationClient($baseUrl, $certificateStorage->reveal());
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Bunq\Certificate\Storage\CertificateStorage.

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
        $expectedHandlerStack = HandlerStack::create();
40
        $expectedHandlerStack->push(
41
            Middleware::mapRequest(new RequestIdMiddleware())
42
        );
43
        $expectedHandlerStack->push(
44
            Middleware::mapRequest(new RequestSignatureMiddleware($privateCertificate))
45
        );
46
47
        $expectedClient = new \GuzzleHttp\Client(
48
            [
49
                'base_uri' => $baseUrl,
50
                'handler'  => $expectedHandlerStack,
51
                'headers'  => [
52
                    'Content-Type' => 'application/json',
53
                    'User-Agent'   => 'bunq-api-client:user',
54
                ],
55
            ]
56
        );
57
58
        $this->assertEquals($expectedClient, $client);
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function itCreatesADefaultClient()
65
    {
66
        $baseUrl = 'uri';
67
68
        $privateCertificate = DefaultCertificate::fromString('private-certificate');
69
        $bunqCertificate    = DefaultCertificate::fromString('bunq-certificate');
70
        /** @var CertificateStorage|ObjectProphecy $certificateStorage */
71
        $certificateStorage = $this->prophesize(CertificateStorage::class);
72
        $certificateStorage->load(CertificateType::PRIVATE_KEY())->willReturn($privateCertificate);
0 ignored issues
show
Bug introduced by
The method load does only exist in Bunq\Certificate\Storage\CertificateStorage, 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...
73
        $certificateStorage->load(CertificateType::BUNQ_SERVER_KEY())->willReturn($bunqCertificate);
74
75
        $sessionToken = DefaultToken::fromString('session-token');
76
        /** @var TokenService|ObjectProphecy $tokenService */
77
        $tokenService = $this->prophesize(TokenService::class);
78
        $tokenService->sessionToken()->willReturn($sessionToken);
0 ignored issues
show
Bug introduced by
The method sessionToken does only exist in Bunq\Service\TokenService, 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...
79
80
        /** @var InstallationService|ObjectProphecy $installationService */
81
        $installationService = $this->prophesize(InstallationService::class);
82
83
        /** @var TokenStorage|ObjectProphecy $tokenStorage */
84
        $tokenStorage = $this->prophesize(TokenStorage::class);
85
86
        $client = HttpClientFactory::create(
87
            $baseUrl,
88
            $tokenService->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Bunq\Service\TokenService.

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
            $certificateStorage->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Bunq\Certificate\Storage\CertificateStorage.

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...
90
            $installationService->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Bunq\Service\InstallationService.

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...
91
            $tokenStorage->reveal()
0 ignored issues
show
Bug introduced by
The method reveal does only exist in Prophecy\Prophecy\ObjectProphecy, but not in Bunq\Token\Storage\TokenStorage.

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...
92
        );
93
94
        $expectedHandlerStack = HandlerStack::create();
95
        $expectedHandlerStack->push(
96
            Middleware::mapRequest(new RequestIdMiddleware())
97
        );
98
        $expectedHandlerStack->push(
99
            Middleware::mapRequest(new RequestAuthenticationMiddleware($sessionToken))
100
        );
101
        $expectedHandlerStack->push(
102
            Middleware::mapRequest(new RequestSignatureMiddleware($bunqCertificate))
103
        );
104
        $expectedHandlerStack->push(
105
            Middleware::mapResponse(new ResponseSignatureMiddleware($bunqCertificate))
106
        );
107
        $expectedHandlerStack->push(
108
            Middleware::mapResponse(new RefreshSessionMiddleware($installationService->reveal(), $tokenStorage->reveal()))
109
        );
110
111
        $expectedClient = new \GuzzleHttp\Client(
112
            [
113
                'base_uri' => $baseUrl,
114
                'handler'  => $expectedHandlerStack,
115
                'headers'  => [
116
                    'Content-Type' => 'application/json',
117
                    'User-Agent'   => 'bunq-api-client:user',
118
                ],
119
            ]
120
        );
121
122
        $this->assertEquals($expectedClient, $client);
123
    }
124
}
125
126