|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\NovaPoshta\Tests\Integration\Models\InternetDocument; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\InternetDocument; |
|
9
|
|
|
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException; |
|
10
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\AssertEntityByPropertiesTrait; |
|
11
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\ConstantsInterface; |
|
12
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\UsesConnectionTrait; |
|
13
|
|
|
|
|
14
|
|
|
class CrudTest extends TestCase implements ConstantsInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use AssertEntityByPropertiesTrait; |
|
17
|
|
|
use UsesConnectionTrait; |
|
18
|
|
|
|
|
19
|
|
|
private InternetDocument $model; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
date_default_timezone_set('Europe/Kyiv'); |
|
24
|
|
|
$connection = $this->getConnection(); |
|
25
|
|
|
$this->model = new InternetDocument($connection); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return void |
|
30
|
|
|
* @throws NovaPoshtaApiException |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function tearDown(): void |
|
33
|
|
|
{ |
|
34
|
|
|
if (filter_var(getenv('CLEAR_DOCUMENTS'), FILTER_VALIDATE_BOOL)) { |
|
35
|
|
|
foreach ($this->getAllDocuments() as $document) { |
|
36
|
|
|
$this->deleteDocument($document['Ref']); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws NovaPoshtaApiException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testCrudDocument(): void |
|
45
|
|
|
{ |
|
46
|
|
|
// In order to get sender and recipient refs create a document in UI and fetch this document |
|
47
|
|
|
// $documents = $this->getAllDocuments(); |
|
48
|
|
|
// 'Sender' => '85d0d7a2-b8fa-11ed-a60f-48df37b921db', |
|
49
|
|
|
// 'ContactSender' => 'e8253e73-cc91-11ed-a60f-48df37b921db', |
|
50
|
|
|
// 'SenderAddress' => 'dbbc2098-25b3-11ee-a60f-48df37b921db', |
|
51
|
|
|
// 'Recipient' => '85d18dbb-b8fa-11ed-a60f-48df37b921db', |
|
52
|
|
|
// 'RecipientAddress' => '1ec09d88-e1c2-11e3-8c4a-0050568002cf', |
|
53
|
|
|
// 'ContactRecipient' => '4184cb87-59ec-11f0-a1d5-48df37b921da', |
|
54
|
|
|
|
|
55
|
|
|
$senderRef = '85d0d7a2-b8fa-11ed-a60f-48df37b921db'; |
|
56
|
|
|
$senderContactRef = 'e8253e73-cc91-11ed-a60f-48df37b921db'; |
|
57
|
|
|
$senderAddressRef = 'dbbc2098-25b3-11ee-a60f-48df37b921db'; |
|
58
|
|
|
|
|
59
|
|
|
$recipientRef = '85d18dbb-b8fa-11ed-a60f-48df37b921db'; |
|
60
|
|
|
$recipientContactRef = '4184cb87-59ec-11f0-a1d5-48df37b921da'; |
|
61
|
|
|
$recipientAddressRef = '1ec09d88-e1c2-11e3-8c4a-0050568002cf'; |
|
62
|
|
|
|
|
63
|
|
|
//create document |
|
64
|
|
|
$params = [ |
|
65
|
|
|
'Sender' => $senderRef, |
|
66
|
|
|
'CitySender' => self::CITY_REF_KHARKIV, |
|
67
|
|
|
'SenderAddress' => $senderAddressRef, |
|
68
|
|
|
'ContactSender' => $senderContactRef, |
|
69
|
|
|
'SendersPhone' => 380507778797, |
|
70
|
|
|
|
|
71
|
|
|
'Recipient' => $recipientRef, |
|
72
|
|
|
'CityRecipient' => self::CITY_REF_KYIV, |
|
73
|
|
|
'RecipientAddress' => $recipientAddressRef, |
|
74
|
|
|
'ContactRecipient' => $recipientContactRef, |
|
75
|
|
|
'RecipientsPhone' => 380507778797, |
|
76
|
|
|
|
|
77
|
|
|
'DateTime' => date('d.m.Y'), |
|
78
|
|
|
'CargoType' => InternetDocument::CARGO_TYPE_CARGO, |
|
79
|
|
|
'Weight' => '0.5', |
|
80
|
|
|
'SeatsAmount' => '1', |
|
81
|
|
|
'ServiceType' => InternetDocument::SERVICE_TYPE_DOORS_DOORS, |
|
82
|
|
|
'PayerType' => InternetDocument::PAYER_TYPE_RECIPIENT, |
|
83
|
|
|
'PaymentMethod' => InternetDocument::PAYMENT_TYPE_CASH, |
|
84
|
|
|
'Description' => 'ЦЕ ТЕСТОВЕ ЗАМОВЛЕННЯ, В ЖОДНОМУ РАЗІ НЕ ОБРОБЛЯТИ !!!' |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
$actualResult = $this->model->save($params); |
|
88
|
|
|
$this->assertNotEmpty($actualResult['Ref']); |
|
89
|
|
|
|
|
90
|
|
|
$params['PayerType'] = InternetDocument::PAYER_TYPE_SENDER; |
|
91
|
|
|
$params['Ref'] = $actualResult['Ref']; |
|
92
|
|
|
|
|
93
|
|
|
$this->gulp(); |
|
94
|
|
|
|
|
95
|
|
|
$actualResult = $this->model->update($params); |
|
96
|
|
|
|
|
97
|
|
|
$this->gulp(); |
|
98
|
|
|
|
|
99
|
|
|
$document = $this->getDocumentByRef($actualResult['Ref']); |
|
100
|
|
|
$this->assertSame($params['PayerType'], $document['PayerType']); |
|
101
|
|
|
|
|
102
|
|
|
$this->gulp(); |
|
103
|
|
|
|
|
104
|
|
|
//delete document |
|
105
|
|
|
$this->deleteDocument($actualResult['Ref']); |
|
106
|
|
|
|
|
107
|
|
|
$this->gulp(); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertDocumentDeleted($actualResult['Ref']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $ref |
|
114
|
|
|
* @param int $attempt |
|
115
|
|
|
* @return void |
|
116
|
|
|
* @throws NovaPoshtaApiException |
|
117
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
118
|
|
|
*/ |
|
119
|
|
|
private function deleteDocument(string $ref, int $attempt = 1): void |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
|
|
$this->model->delete($ref); |
|
123
|
|
|
} catch (NovaPoshtaApiException $e) { |
|
124
|
|
|
$docNotCreatedYet = str_contains($e->getMessage(), 'No document changed DeletionMark'); |
|
125
|
|
|
if (!$docNotCreatedYet) { |
|
126
|
|
|
throw $e; |
|
127
|
|
|
} |
|
128
|
|
|
$attemptsNotExceeded = $attempt <= 3; |
|
129
|
|
|
if ($attemptsNotExceeded) { |
|
130
|
|
|
printf(PHP_EOL . 'Attempt %d to delete document failed.' . PHP_EOL, $attempt); |
|
131
|
|
|
sleep(5 * $attempt); |
|
132
|
|
|
$this->deleteDocument($ref, ++$attempt); |
|
133
|
|
|
} else { |
|
134
|
|
|
throw $e; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $ref |
|
141
|
|
|
* @return void |
|
142
|
|
|
* @throws NovaPoshtaApiException |
|
143
|
|
|
*/ |
|
144
|
|
|
private function assertDocumentDeleted(string $ref): void |
|
145
|
|
|
{ |
|
146
|
|
|
if ($this->getDocumentByRef($ref)) { |
|
147
|
|
|
$this->fail('Failed to delete document.'); |
|
148
|
|
|
} |
|
149
|
|
|
$this->assertTrue(true, 'Just to suppress error that method doesn\'t do any assertions'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $ref |
|
154
|
|
|
* @return array|null |
|
155
|
|
|
* @throws NovaPoshtaApiException |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getDocumentByRef(string $ref): ?array |
|
158
|
|
|
{ |
|
159
|
|
|
$documents = $this->getAllDocuments(); |
|
160
|
|
|
foreach ($documents as $document) { |
|
161
|
|
|
if ($document['Ref'] === $ref) { |
|
162
|
|
|
return $document; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return array |
|
170
|
|
|
* @throws NovaPoshtaApiException |
|
171
|
|
|
*/ |
|
172
|
|
|
private function getAllDocuments(): array |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->model->getDocumentList([ |
|
175
|
|
|
'DateTimeFrom' => date('d.m.Y', strtotime('-2 days')), |
|
176
|
|
|
'DateTimeTo' => date('d.m.Y', strtotime('+2 days')), |
|
177
|
|
|
'GetFullList' => 1, |
|
178
|
|
|
'DateTime' => date('d.m.Y'), |
|
179
|
|
|
], 1); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
|
|
private function gulp(): void |
|
186
|
|
|
{ |
|
187
|
|
|
$gulp = (int)getenv('GULP'); |
|
188
|
|
|
|
|
189
|
|
|
if ($gulp) { |
|
190
|
|
|
sleep($gulp); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|