-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBunqSdkTestBase.php
291 lines (255 loc) · 7.77 KB
/
BunqSdkTestBase.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace bunq\test;
use bunq\Context\ApiContext;
use bunq\Context\BunqContext;
use bunq\Exception\BunqException;
use bunq\Http\ApiClient;
use bunq\Model\Generated\Endpoint\AttachmentPublic;
use bunq\Model\Generated\Endpoint\Avatar;
use bunq\Model\Generated\Endpoint\CashRegister;
use bunq\Model\Generated\Endpoint\MonetaryAccountBank;
use bunq\Model\Generated\Endpoint\RequestInquiry;
use bunq\Model\Generated\Object\Amount;
use bunq\Model\Generated\Object\Pointer;
use bunq\Util\BunqEnumApiEnvironmentType;
use bunq\Util\FileUtil;
use bunq\Util\InstallationUtil;
use PHPUnit\Framework\TestCase;
/**
* The base for all the Bunq SDK tests.
*/
class BunqSdkTestBase extends TestCase
{
/**
* Error constants.
*/
const ERROR_COULD_NOT_DETERMINE_IBAN_POINTER = 'Could not determine IBAN pointer';
const ERROR_COULD_NOT_DETERMINE_USER_ALIAS = 'Could not determine user alias.';
const WARMING_TEST_SKIPPED_DUE_TO_INSUFFICIENT_BALANCE = 'Not enough money on primary account.';
/*
* CashRegister constants.
*/
const CASH_REGISTER_NAME = 'PHP test cash register';
const CASH_REGISTER_STATUS = 'PENDING_APPROVAL';
/**
* MonetaryAccount constants.
*/
const MONETARY_ACCOUNT_CURRENCY = 'EUR';
const MONETARY_ACCOUNT_DESCRIPTION = 'test account php';
const MONETARY_ACCOUNT_BALANCE_THRESHOLD = 0.00;
/**
* Pointer constants.
*/
const POINTER_TYPE_IBAN = 'IBAN';
const POINTER_TYPE_EMAIL = 'EMAIL';
const EMAIL_BRAVO = '[email protected]';
/**
* Full name of context config file to use for testing.
*/
const FILE_PATH_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf';
const FILE_PATH_AVATAR = '/resource/vader.png';
/**
* Attachment constants.
*/
const ATTACHMENT_CONTENT_TYPE = 'image/png';
const ATTACHMENT_DESCRIPTION = 'TEST PNG PHP';
const ATTACHMENT_PATH_IN = '/vader.png';
/**
* Default constants.
*/
const PAYMENT_AMOUNT_DEFAULT = '0.01';
/**
* The index of the first item in an array.
*/
const INDEX_FIRST = 0;
/**
* @var MonetaryAccountBank
*/
protected $secondMonetaryAccountBank;
/**
* @var CashRegister
*/
protected $cashRegister;
/**
* Spending money constants.
*/
const SPENDING_MONEY_AMOUNT = '500';
const SPENDING_MONEY_RECIPIENT = '[email protected]';
const SPENDING_MONEY_DESCRIPTION = 'sdk php test, thanks daddy <3';
/**
*/
public static function setUpBeforeClass(): void
{
static::createApiContext();
BunqContext::loadApiContext(
ApiContext::restore(self::FILE_PATH_CONTEXT_CONFIG)
);
}
/**
*/
protected static function createApiContext()
{
InstallationUtil::automaticInstall(
BunqEnumApiEnvironmentType::SANDBOX(),
self::FILE_PATH_CONTEXT_CONFIG
);
}
/**
*/
protected function setUp(): void
{
$this->setSecondMonetaryAccountBank();
$this->requestSpendingMoney();
sleep(0.5); // ensure requests are auto accepted.
BunqContext::getUserContext()->refreshUserContext();
}
/**
*/
private function setCashRegister()
{
$attachmentUuid = AttachmentPublic::create(
FileUtil::getFileContents(__DIR__ . self::FILE_PATH_AVATAR),
[
ApiClient::HEADER_CONTENT_TYPE => $this->getAttachmentContentType(),
ApiClient::HEADER_ATTACHMENT_DESCRIPTION => $this->getAttachmentDescription(),
]
);
$avatarUuid = Avatar::create($attachmentUuid->getValue());
$cashRegisterId = CashRegister::create(
self::CASH_REGISTER_NAME,
self::CASH_REGISTER_STATUS,
$avatarUuid->getValue()
);
$this->cashRegister = CashRegister::get($cashRegisterId->getValue());
}
/**
* @return string
*/
protected function getAttachmentContentType(): string
{
return self::ATTACHMENT_CONTENT_TYPE;
}
/**
* @return string
*/
protected function getAttachmentDescription(): string
{
return self::ATTACHMENT_DESCRIPTION;
}
/**
*/
private function setSecondMonetaryAccountBank()
{
$createdId = MonetaryAccountBank::create(
self::MONETARY_ACCOUNT_CURRENCY,
self::MONETARY_ACCOUNT_DESCRIPTION
);
$this->secondMonetaryAccountBank = MonetaryAccountBank::get($createdId->getValue())->getValue();
}
/**
*/
private function requestSpendingMoney()
{
RequestInquiry::create(
new Amount(self::SPENDING_MONEY_AMOUNT, self::MONETARY_ACCOUNT_CURRENCY),
new Pointer(self::POINTER_TYPE_EMAIL, self::SPENDING_MONEY_RECIPIENT),
self::SPENDING_MONEY_DESCRIPTION,
false
);
RequestInquiry::create(
new Amount(self::SPENDING_MONEY_AMOUNT, self::MONETARY_ACCOUNT_CURRENCY),
new Pointer(self::POINTER_TYPE_EMAIL, self::SPENDING_MONEY_RECIPIENT),
self::SPENDING_MONEY_DESCRIPTION,
false,
$this->getSecondMonetaryAccountId()
);
}
/**
* @return Pointer
*
* @throws BunqException
*/
protected function getSecondMonetaryAccountAlias(): Pointer
{
$allAlias = $this->secondMonetaryAccountBank->getAlias();
foreach ($allAlias as $alias) {
if ($alias->getType() === self::POINTER_TYPE_IBAN) {
return $alias;
}
}
throw new BunqException(self::ERROR_COULD_NOT_DETERMINE_IBAN_POINTER);
}
/**
* @return string
*/
protected function getAttachmentFilePath(): string
{
return self::ATTACHMENT_PATH_IN;
}
/**
* @return Pointer
*
* @throws BunqException
*/
protected function getUserAlias(): Pointer
{
if (BunqContext::getUserContext()->isOnlyUserPersonSet()) {
return BunqContext::getUserContext()->getUserPerson()->getAlias()[self::INDEX_FIRST];
} elseif (BunqContext::getUserContext()->isOnlyUserCompanySet()) {
return BunqContext::getUserContext()->getUserCompany()->getAlias()[self::INDEX_FIRST];
} elseif (BunqContext::getUserContext()->isOnlyUserApiKeySet()) {
return BunqContext::getUserContext()
->getUserApiKey()
->getRequestedByUser()
->getReferencedObject()
->getAlias()[self::INDEX_FIRST];
} else {
throw new BunqException(self::ERROR_COULD_NOT_DETERMINE_USER_ALIAS);
}
}
/**
* @return Pointer
*/
protected function getPointerUserBravo(): Pointer
{
return new Pointer(
self::POINTER_TYPE_EMAIL,
self::EMAIL_BRAVO
);
}
/**
* @return int
*/
protected function getSecondMonetaryAccountId(): int
{
return $this->secondMonetaryAccountBank->getId();
}
/**
* @return int
*/
protected function getCashRegisterId(): int
{
if (is_null($this->cashRegister)) {
$this->setCashRegister();
}
return $this->cashRegister->getId();
}
/**
* @return bool
*/
protected function isMonetaryAccountBalanceSufficient(): bool
{
$balance = floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue());
return $balance > self::MONETARY_ACCOUNT_BALANCE_THRESHOLD;
}
/**
* @return bool
*/
protected function skipTestIfNeededDueToInsufficientBalance(): bool
{
if (!$this->isMonetaryAccountBalanceSufficient()) {
static::markTestSkipped(self::WARMING_TEST_SKIPPED_DUE_TO_INSUFFICIENT_BALANCE);
}
return true;
}
}