Completed
Push — master ( b91597...be19b0 )
by Mitchel
02:09
created

itCreatesAInstallationClient()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
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\RequestAuthenticationMiddleware;
10
use Bunq\Middleware\RequestIdMiddleware;
11
use Bunq\Middleware\RequestSignatureMiddleware;
12
use Bunq\Middleware\ResponseSignatureMiddleware;
13
use Bunq\Service\TokenService;
14
use Bunq\Token\DefaultToken;
15
use GuzzleHttp\HandlerStack;
16
use GuzzleHttp\Middleware;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Prophecy\ObjectProphecy;
19
20
final class HttpClientFactoryTest extends TestCase
21
{
22
    /**
23
     * @test
24
     */
25
    public function itCreatesAInstallationClient()
26
    {
27
        $baseUrl            = 'uri';
28
        $privateCertificate = DefaultCertificate::fromString('private-certificate');
29
30
        /** @var CertificateStorage|ObjectProphecy $certificateStorage */
31
        $certificateStorage = $this->prophesize(CertificateStorage::class);
32
        $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...
33
34
        $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...
35
36
        $expectedHandlerStack = HandlerStack::create();
37
        $expectedHandlerStack->push(
38
            Middleware::mapRequest(new RequestIdMiddleware())
39
        );
40
        $expectedHandlerStack->push(
41
            Middleware::mapRequest(new RequestSignatureMiddleware($privateCertificate))
42
        );
43
44
        $expectedClient = new \GuzzleHttp\Client(
45
            [
46
                'base_uri' => $baseUrl,
47
                'handler'  => $expectedHandlerStack,
48
                'headers'  => [
49
                    'Content-Type' => 'application/json',
50
                    'User-Agent'   => 'bunq-api-client:user',
51
                ],
52
            ]
53
        );
54
55
        $this->assertEquals($expectedClient, $client);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function itCreatesADefaultClient()
62
    {
63
        $baseUrl = 'uri';
64
65
        $privateCertificate = DefaultCertificate::fromString('private-certificate');
66
        $bunqCertificate    = DefaultCertificate::fromString('bunq-certificate');
67
        /** @var CertificateStorage|ObjectProphecy $certificateStorage */
68
        $certificateStorage = $this->prophesize(CertificateStorage::class);
69
        $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...
70
        $certificateStorage->load(CertificateType::BUNQ_SERVER_KEY())->willReturn($bunqCertificate);
71
72
        $sessionToken = DefaultToken::fromString('session-token');
73
        /** @var TokenService|ObjectProphecy $tokenService */
74
        $tokenService = $this->prophesize(TokenService::class);
75
        $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...
76
77
        $client = HttpClientFactory::create($baseUrl, $tokenService->reveal(), $certificateStorage->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...
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...
78
79
        $expectedHandlerStack = HandlerStack::create();
80
        $expectedHandlerStack->push(
81
            Middleware::mapRequest(new RequestIdMiddleware())
82
        );
83
        $expectedHandlerStack->push(
84
            Middleware::mapRequest(new RequestAuthenticationMiddleware($sessionToken))
85
        );
86
        $expectedHandlerStack->push(
87
            Middleware::mapRequest(new RequestSignatureMiddleware($bunqCertificate))
88
        );
89
        $expectedHandlerStack->push(
90
            Middleware::mapResponse(new ResponseSignatureMiddleware($bunqCertificate))
91
        );
92
93
        $expectedClient = new \GuzzleHttp\Client(
94
            [
95
                'base_uri' => $baseUrl,
96
                'handler'  => $expectedHandlerStack,
97
                'headers'  => [
98
                    'Content-Type' => 'application/json',
99
                    'User-Agent'   => 'bunq-api-client:user',
100
                ],
101
            ]
102
        );
103
104
        $this->assertEquals($expectedClient, $client);
105
    }
106
}
107
108