forked from Adyen/adyen-php-api-library
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
329 lines (291 loc) · 11.4 KB
/
TestCase.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen API Library for PHP
*
* Copyright (c) 2020 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
*/
namespace Adyen\Tests;
use Adyen\AdyenException;
use Adyen\Client;
use Adyen\Environment;
class TestCase extends \PHPUnit\Framework\TestCase
{
protected $merchantAccount;
protected $donationAccount;
protected $skinCode;
protected $hmacSignature;
/**
* Settings parsed from the config/test.ini file
*
* @var array
*/
protected $settings;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->settings = $this->loadConfig();
$this->merchantAccount = $this->getMerchantAccount();
$this->donationAccount = $this->getDonationAccount();
$this->skinCode = $this->getSkinCode();
$this->hmacSignature = $this->getHmacSignature();
$this->setDefaultsDuringDevelopment();
}
/**
* During development of the standalone php api library this function sets the necessary defaults which would
* otherwise set by the merchant
*/
private function setDefaultsDuringDevelopment()
{
// Check default timezone if not set use a default value for that
if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'Europe/Amsterdam');
}
}
/**
* Mock client object without configuring the config/test.ini
*
* @return Client
*/
protected function createClientWithoutTestIni()
{
try {
$client = new Client();
$client->setApplicationName("My Test Application");
$client->setEnvironment(Environment::TEST);
} catch (AdyenException $exception) {
$this->skipTest($exception->getMessage());
}
return $client;
}
/**
* Mock client for payout
*
* @return Client
*/
protected function createPayoutClient()
{
// validate username, password and MERCHANTAccount
if (isset($this->settings['storePayoutUsername']) && isset($this->settings['storePayoutPassword'])) {
if ($this->settings['storePayoutUsername'] == "YOUR STORE PAYOUT USERNAME"
|| $this->settings['storePayoutUsername'] == ""
|| $this->settings['storePayoutPassword'] == "YOUR STORE PAYOUT PASSWORD"
|| $this->settings['storePayoutPassword'] == "") {
$client = new Client();
$client->setApplicationName("My Test Application");
$client->setEnvironment(Environment::TEST);
$this->skipTest(
"Skipped the test. Configure your WebService Payout Username and Password in the config"
);
return $client;
} else {
$client = new Client();
$client->setApplicationName("My Test Application");
$client->setUsername($this->settings['storePayoutUsername']);
$client->setPassword($this->settings['storePayoutPassword']);
$client->setEnvironment(Environment::TEST);
return $client;
}
} else {
$this->skipTest("Skipped the test. Configure your WebService Payout Username and Password in the config");
}
}
/**
* Mock client for reviewing payout
*
* @return Client
*/
protected function createReviewPayoutClient()
{
// validate username, password and MERCHANTAccount
if (isset($this->settings['reviewPayoutUsername']) && isset($this->settings['reviewPayoutPassword'])) {
if ($this->settings['reviewPayoutUsername'] == "YOUR REVIEW PAYOUT USERNAME"
|| $this->settings['reviewPayoutUsername'] == ""
|| $this->settings['reviewPayoutPassword'] == "YOUR REVIEW PAYOUT PASSWORD"
|| $this->settings['reviewPayoutPassword'] == "") {
$client = new Client();
$client->setApplicationName("My Test Application");
$client->setEnvironment(Environment::TEST);
$this->skipTest(
"Skipped the test. Configure your WebService ReviewPayout Username and Password in the config"
);
return $client;
} else {
$client = new Client();
$client->setApplicationName("My Test Application");
$client->setUsername($this->settings['reviewPayoutUsername']);
$client->setPassword($this->settings['reviewPayoutPassword']);
$client->setEnvironment(Environment::TEST);
return $client;
}
} else {
$this->skipTest(
"Skipped the test. Configure your WebService ReviewPayout Username and Password in the config"
);
}
}
protected function createTerminalCloudAPIClient()
{
if (empty($this->settings['x-api-key']) || $this->settings['x-api-key'] == 'YOUR X-API KEY') {
$this->skipTest("Skipped the test. Configure your x-api-key in the config");
} else {
$client = new Client();
$client->setApplicationName("My Test Terminal API App");
$client->setEnvironment(Environment::TEST);
$client->setXApiKey($this->settings['x-api-key']);
return $client;
}
}
protected function createClientWithMerchantAccount()
{
$client = $this->createClient();
if (empty($this->settings['merchantAccount']) || $this->settings['merchantAccount'] == 'YOUR MERCHANTACCOUNT') {
$this->skipTest("Skipped the test. Configure your MerchantAccount in the config");
return null;
}
$client->setMerchantAccount($this->settings['merchantAccount']);
return $client;
}
protected function createCheckoutAPIClient()
{
$client = $this->createClientWithMerchantAccount();
if (empty($this->settings['x-api-key']) || $this->settings['x-api-key'] == 'YOUR X-API KEY') {
$this->skipTest("Skipped the test. Configure your x-api-key");
} else {
$client->setXApiKey($this->settings['x-api-key']);
return $client;
}
}
protected function getMerchantAccount()
{
if (empty($this->settings['merchantAccount']) || $this->settings['merchantAccount'] == 'YOUR MERCHANTACCOUNT') {
return null;
}
return $this->settings['merchantAccount'];
}
protected function getDonationAccount()
{
if (empty($this->settings['donationAccount']) ||
$this->settings['donationAccount'] == 'YOUR DONATION MERCHANT ACCOUNT') {
return null;
}
return $this->settings['donationAccount'];
}
protected function getSkinCode()
{
if (empty($this->settings['skinCode']) || $this->settings['skinCode'] == 'YOUR SKIN CODE') {
return null;
}
return $this->settings['skinCode'];
}
protected function getHmacSignature()
{
if (empty($this->settings['hmacSignature'])|| $this->settings['hmacSignature'] == 'YOUR HMAC SIGNATURE') {
return null;
}
return $this->settings['hmacSignature'];
}
protected function getPOIID()
{
if (empty($this->settings['POIID']) || $this->settings['POIID'] == 'MODEL-SERIALNUMBER') {
return null;
}
return $this->settings['POIID'];
}
protected function loadConfig()
{
if (file_exists($this->getTestFilePath())) {
return $this->loadConfigIni();
}
return array(
'username' => getenv('INTEGRATION_USERNAME'),
'password' => getenv('INTEGRATION_PASSWORD'),
'x-api-key' => getenv('INTEGRATION_X_API_KEY'),
'merchantAccount' => getenv('INTEGRATION_MERCHANT_ACCOUNT'),
'donationAccount' => getenv('INTEGRATION_DONATION_ACCOUNT'),
'skinCode' => getenv('INTEGRATION_SKIN_CODE'),
'hmacSignature' => getenv('INTEGRATION_HMAC_SIGNATURE'),
'storePayoutUsername' => getenv('INTEGRATION_STORE_PAYOUT_USERNAME'),
'storePayoutPassword' => getenv('INTEGRATION_STORE_PAYOUT_PASSWORD'),
'reviewPayoutUsername' => getenv('INTEGRATION_REVIEW_PAYOUT_USERNAME'),
'reviewPayoutPassword' => getenv('INTEGRATION_REVIEW_PAYOUT_PASSWORD'),
);
}
/**
* Loads the settings into and array from the config/test.ini file
*
* @return array|bool
*/
protected function loadConfigIni()
{
return parse_ini_file($this->getTestFilePath(), true);
}
protected function skipTest($msg)
{
$this->markTestSkipped($msg);
}
protected function needSkinCode()
{
if (!$this->skinCode) {
$this->skipTest("Skipped the test. Configure your SkinCode in the config");
}
}
public function validateApiPermission($e)
{
// it is possible you do not have permission to use full API then switch over to CSE
if ($e->getMessage() == "Not allowed") {
$this->assertEquals(AdyenException::class, get_class($e));
$this->assertEquals('Not allowed', $e->getMessage());
$this->assertEquals('10', $e->getCode());
$this->markTestSkipped(
'Skipped the test. ' .
'You do not have the permission to do a full api call. Try to use Client Side Encryption (CSE)'
);
} elseif ($e->getMessage() == "Recurring is not enabled") {
$this->assertEquals(AdyenException::class, get_class($e));
$this->assertEquals('Recurring is not enabled', $e->getMessage());
$this->assertEquals('107', $e->getCode());
$this->markTestSkipped("Skipped the test. You do not have the permission to do a recurring transaction.");
}
}
/**
* Get reflection class method set to public to make it testable
*
* @param string $class full path
* @param string $name
* @return mixed
*/
protected function getMethod($class, $name)
{
try {
$class = new \ReflectionClass($class);
} catch (\ReflectionException $exception) {
$this->skipTest($exception->getMessage());
}
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
/**
* @return string
*/
protected function getTestFilePath()
{
return __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'test.ini';
}
}