1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq\Tests\Service; |
4
|
|
|
|
5
|
|
|
use Bunq\Certificate\CertificateType; |
6
|
|
|
use Bunq\Certificate\DefaultCertificate; |
7
|
|
|
use Bunq\Certificate\Storage\CertificateStorage; |
8
|
|
|
use Bunq\Service\InstallationService; |
9
|
|
|
use Bunq\Service\DefaultTokenService; |
10
|
|
|
use Bunq\Token\DefaultToken; |
11
|
|
|
use Bunq\Token\Storage\TokenNotFoundException; |
12
|
|
|
use Bunq\Token\Storage\TokenStorage; |
13
|
|
|
use Bunq\Token\TokenType; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
17
|
|
|
|
18
|
|
|
final class TokenServiceTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var InstallationService|ObjectProphecy |
22
|
|
|
*/ |
23
|
|
|
private $installationService; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TokenStorage|ObjectProphecy |
27
|
|
|
*/ |
28
|
|
|
private $tokenStorage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CertificateStorage|ObjectProphecy |
32
|
|
|
*/ |
33
|
|
|
private $certificateStorage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DefaultTokenService |
37
|
|
|
*/ |
38
|
|
|
private $service; |
39
|
|
|
|
40
|
|
|
public function setUp() |
41
|
|
|
{ |
42
|
|
|
$this->installationService = $this->prophesize(InstallationService::class); |
43
|
|
|
$this->tokenStorage = $this->prophesize(TokenStorage::class); |
44
|
|
|
$this->certificateStorage = $this->prophesize(CertificateStorage::class); |
45
|
|
|
|
46
|
|
|
$this->service = new DefaultTokenService( |
47
|
|
|
$this->installationService->reveal(), |
48
|
|
|
$this->tokenStorage->reveal(), |
49
|
|
|
$this->certificateStorage->reveal() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function itReturnsASessionToken() |
57
|
|
|
{ |
58
|
|
|
$token = new DefaultToken('someToken'); |
59
|
|
|
$this->tokenStorage->load(TokenType::SESSION_TOKEN())->willReturn($token); |
|
|
|
|
60
|
|
|
$this->installationService->createSession(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$result = $this->service->sessionToken(); |
63
|
|
|
|
64
|
|
|
$this->assertEquals($token, $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function itCreatesANewSessionTokenWhenItDoesNotExist() |
71
|
|
|
{ |
72
|
|
|
$this->tokenStorage->load(TokenType::SESSION_TOKEN())->willThrow( |
|
|
|
|
73
|
|
|
new TokenNotFoundException(TokenType::SESSION_TOKEN(), 'path') |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$installationToken = new DefaultToken('installation_token'); |
77
|
|
|
$this->tokenStorage->load(TokenType::INSTALLATION_TOKEN())->willReturn($installationToken); |
78
|
|
|
|
79
|
|
|
$this->installationService->install()->shouldNotBeCalled(); |
|
|
|
|
80
|
|
|
$this->installationService->createSession($installationToken)->willReturn('a-long-new-session-token'); |
|
|
|
|
81
|
|
|
$this->installationService->registerDevice(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$sessionToken = DefaultToken::fromString('a-long-new-session-token'); |
84
|
|
|
|
85
|
|
|
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN())->shouldBeCalled(); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->assertEquals($sessionToken, $this->service->sessionToken()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function itCreatesANewSessionTokenAndInstallationTokenWithThePublicKey() |
94
|
|
|
{ |
95
|
|
|
$this->tokenStorage->load(TokenType::SESSION_TOKEN())->willThrow( |
|
|
|
|
96
|
|
|
new TokenNotFoundException(TokenType::SESSION_TOKEN(), 'path') |
97
|
|
|
); |
98
|
|
|
$this->tokenStorage->load(TokenType::INSTALLATION_TOKEN())->willThrow( |
99
|
|
|
new TokenNotFoundException(TokenType::INSTALLATION_TOKEN(), 'path') |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->installationService->install()->willReturn( |
|
|
|
|
103
|
|
|
[ |
104
|
|
|
'token' => 'a-long-new-installation-token', |
105
|
|
|
'public_key' => '-----BEGIN PUBLIC KEY-----KEY-----END PUBLIC KEY-----' |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$installationToken = DefaultToken::fromString('a-long-new-installation-token'); |
110
|
|
|
$this->tokenStorage->save($installationToken, TokenType::INSTALLATION_TOKEN())->shouldBeCalled(); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$certificate = DefaultCertificate::fromString('-----BEGIN PUBLIC KEY-----KEY-----END PUBLIC KEY-----'); |
113
|
|
|
$this->certificateStorage->save($certificate, CertificateType::BUNQ_SERVER_KEY())->shouldBeCalled(); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->installationService->registerDevice($installationToken)->shouldBeCalled(); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->installationService->createSession($installationToken)->willReturn('a-long-new-session-token'); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$sessionToken = DefaultToken::fromString('a-long-new-session-token'); |
120
|
|
|
|
121
|
|
|
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN())->shouldBeCalled(); |
122
|
|
|
|
123
|
|
|
$this->assertEquals($sessionToken, $this->service->sessionToken()); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
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: