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); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$client = HttpClientFactory::createInstallationClient($baseUrl, $certificateStorage->reveal()); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$client = HttpClientFactory::create($baseUrl, $tokenService->reveal(), $certificateStorage->reveal()); |
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: