-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetAuthTokenTest.php
177 lines (143 loc) · 6.13 KB
/
GetAuthTokenTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
namespace BrokeYourBike\ZenithBank\Tests;
use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use Carbon\Carbon;
use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface;
use BrokeYourBike\ZenithBank\Client;
/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class GetAuthTokenTest extends TestCase
{
private string $tokenValue = 'super-secure-token';
/** @test */
public function it_can_cache_and_return_auth_token()
{
$currentTestDate = Carbon::create(2020, 1, 1, 23, 30, 59);
Carbon::setTestNow($currentTestDate);
$expirationDate = Carbon::now()->addHour();
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"responseCode": "00",
"responseDescription": "SUCCESS",
"description": null,
"tokenDetail": {
"token": "' . $this->tokenValue . '",
"expiration": "' . (string) $expirationDate . '"
}
}');
/** @var \GuzzleHttp\Client $mockedClient */
$mockedClient = $this->createPartialMock(\GuzzleHttp\Client::class, ['request']);
$mockedClient->method('request')->willReturn($mockedResponse);
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::spy(CacheInterface::class);
/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
$this->assertSame($this->tokenValue, $api->getAuthToken());
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache->shouldHaveReceived('set')
->once()
->with(
$api->authTokenCacheKey(),
$this->tokenValue,
3600 - 60
);
}
/** @test */
public function it_will_throw_if_expiration_date_invalid()
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"responseCode": "00",
"responseDescription": "SUCCESS",
"description": null,
"tokenDetail": {
"token": "' . $this->tokenValue . '",
"expiration": "NOT-A-VALID-DATE"
}
}');
/** @var \GuzzleHttp\Client $mockedClient */
$mockedClient = $this->createPartialMock(\GuzzleHttp\Client::class, ['request']);
$mockedClient->method('request')->willReturn($mockedResponse);
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::spy(CacheInterface::class);
/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
try {
$api->getAuthToken();
} catch (\Throwable $th) {
$this->assertInstanceOf(\Carbon\Exceptions\InvalidFormatException::class, $th);
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache->shouldNotHaveReceived('set');
return;
}
$this->fail('Exception was not thrown');
}
/** @test */
public function it_can_return_cached_value()
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldNotReceive('request');
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::mock(CacheInterface::class);
$mockedCache->shouldReceive('has')
->once()
->andReturn(true);
$mockedCache->shouldReceive('get')
->once()
->andReturn($this->tokenValue);
/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
$requestResult = $api->getAuthToken();
$this->assertSame($this->tokenValue, $requestResult);
}
/** @test */
public function it_will_return_null_if_response_invalid()
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedResponse = $this->getMockBuilder(\Psr\Http\Message\ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')->willReturn('{}');
/** @var \GuzzleHttp\Client $mockedClient */
$mockedClient = $this->createPartialMock(\GuzzleHttp\Client::class, ['request']);
$mockedClient->method('request')->willReturn($mockedResponse);
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache = \Mockery::spy(CacheInterface::class);
/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);
$requestResult = $api->getAuthToken();
$this->assertNull($requestResult);
/** @var \Mockery\MockInterface $mockedCache */
$mockedCache->shouldNotReceive('set');
}
}