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
|
|
|
// foreach ($this->getAllDocuments() as $document) { |
35
|
|
|
// $this->deleteDocument($document['Ref']); |
36
|
|
|
// } |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws NovaPoshtaApiException |
41
|
|
|
*/ |
42
|
|
|
public function testCrudDocument(): void |
43
|
|
|
{ |
44
|
|
|
// $counterpartyModel = new \SergeyNezbritskiy\NovaPoshta\Models\Counterparty($this->getConnection()); |
45
|
|
|
// $addressModel = new \SergeyNezbritskiy\NovaPoshta\Models\Address($this->getConnection()); |
46
|
|
|
|
47
|
|
|
// $recipient = $counterpartyModel->savePrivatePerson([ |
48
|
|
|
// 'FirstName' => 'Петро', |
49
|
|
|
// 'MiddleName' => 'Григорович', |
50
|
|
|
// 'LastName' => 'Порошенко', |
51
|
|
|
// 'Phone' => 380501112233, |
52
|
|
|
// 'Email' => '[email protected]', |
53
|
|
|
// 'CounterpartyProperty' => Counterparty::COUNTERPARTY_PROPERTY_RECIPIENT, |
54
|
|
|
// ]); // 4d21c8c7-b88e-11ed-a60f-48df37b921db |
55
|
|
|
$recipientRef = '4d21c8c7-b88e-11ed-a60f-48df37b921db'; |
56
|
|
|
|
57
|
|
|
// $address = $addressModel->save($recipientRef, [ |
58
|
|
|
// 'StreetRef' => self::KHRESHCHATYK_STREET_REF, |
59
|
|
|
// 'BuildingNumber' => '20', |
60
|
|
|
// 'Flat' => '12', |
61
|
|
|
// ]); |
62
|
|
|
$addressRef = 'cecaac32-25bb-11ee-a60f-48df37b921db'; |
63
|
|
|
|
64
|
|
|
// $contactPersons = $counterpartyModel->getCounterpartyContactPersons(self::COUNTERPARTY_REF); |
65
|
|
|
$senderContactRef = '4d06cbac-b88e-11ed-a60f-48df37b921db'; |
66
|
|
|
|
67
|
|
|
// $contactPersons = $counterpartyModel->getCounterpartyContactPersons($recipientRef); |
68
|
|
|
$recipientContactRef = '45b55250-25bb-11ee-a60f-48df37b921db'; |
69
|
|
|
|
70
|
|
|
//create document |
71
|
|
|
$params = [ |
72
|
|
|
'Sender' => self::COUNTERPARTY_REF, |
73
|
|
|
'CitySender' => self::CITY_REF_KHARKIV, |
74
|
|
|
'SenderAddress' => self::ADDRESS_REF_KHARKIV, |
75
|
|
|
'ContactSender' => $senderContactRef, |
76
|
|
|
'SendersPhone' => 380505511696, |
77
|
|
|
|
78
|
|
|
'Recipient' => $recipientRef, |
79
|
|
|
'CityRecipient' => self::CITY_REF_KYIV, |
80
|
|
|
'RecipientAddress' => $addressRef, |
81
|
|
|
'ContactRecipient' => $recipientContactRef, |
82
|
|
|
'RecipientsPhone' => 380505511696, |
83
|
|
|
|
84
|
|
|
'DateTime' => date('d.m.Y'), |
85
|
|
|
'CargoType' => InternetDocument::CARGO_TYPE_CARGO, |
86
|
|
|
'Weight' => '0.5', |
87
|
|
|
'SeatsAmount' => '1', |
88
|
|
|
'ServiceType' => InternetDocument::SERVICE_TYPE_DOORS_DOORS, |
89
|
|
|
'PayerType' => InternetDocument::PAYER_TYPE_RECIPIENT, |
90
|
|
|
'PaymentMethod' => InternetDocument::PAYMENT_TYPE_CASH, |
91
|
|
|
'Description' => 'Це тестове замовлення, не треба його обробляти' |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$actualResult = $this->model->save($params); |
95
|
|
|
$this->assertNotEmpty($actualResult['Ref']); |
96
|
|
|
|
97
|
|
|
$params['PayerType'] = InternetDocument::PAYER_TYPE_SENDER; |
98
|
|
|
$params['Ref'] = $actualResult['Ref']; |
99
|
|
|
|
100
|
|
|
$this->gulp(); |
101
|
|
|
|
102
|
|
|
$actualResult = $this->model->update($params); |
103
|
|
|
|
104
|
|
|
$this->gulp(); |
105
|
|
|
|
106
|
|
|
$document = $this->getDocumentByRef($actualResult['Ref']); |
107
|
|
|
$this->assertSame($params['PayerType'], $document['PayerType']); |
108
|
|
|
|
109
|
|
|
$this->gulp(); |
110
|
|
|
|
111
|
|
|
//delete document |
112
|
|
|
$this->deleteDocument($actualResult['Ref']); |
113
|
|
|
|
114
|
|
|
$this->gulp(); |
115
|
|
|
|
116
|
|
|
$this->assertDocumentDeleted($actualResult['Ref']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $ref |
121
|
|
|
* @param int $attempt |
122
|
|
|
* @return void |
123
|
|
|
* @throws NovaPoshtaApiException |
124
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
125
|
|
|
*/ |
126
|
|
|
private function deleteDocument(string $ref, int $attempt = 1): void |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
$this->model->delete($ref); |
130
|
|
|
} catch (NovaPoshtaApiException $e) { |
131
|
|
|
$docNotCreatedYet = str_contains($e->getMessage(), 'No document changed DeletionMark'); |
132
|
|
|
if (!$docNotCreatedYet) { |
133
|
|
|
throw $e; |
134
|
|
|
} |
135
|
|
|
$attemptsNotExceeded = $attempt <= 3; |
136
|
|
|
if ($attemptsNotExceeded) { |
137
|
|
|
printf(PHP_EOL . 'Attempt %d to delete document failed.' . PHP_EOL, $attempt); |
138
|
|
|
sleep(5 * $attempt); |
139
|
|
|
$this->deleteDocument($ref, ++$attempt); |
140
|
|
|
} else { |
141
|
|
|
throw $e; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $ref |
148
|
|
|
* @return void |
149
|
|
|
* @throws NovaPoshtaApiException |
150
|
|
|
*/ |
151
|
|
|
private function assertDocumentDeleted(string $ref): void |
152
|
|
|
{ |
153
|
|
|
if ($this->getDocumentByRef($ref)) { |
154
|
|
|
$this->fail('Failed to delete document.'); |
155
|
|
|
} |
156
|
|
|
$this->assertTrue(true, 'Just to suppress error that method doesn\'t do any assertions'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $ref |
161
|
|
|
* @return array|null |
162
|
|
|
* @throws NovaPoshtaApiException |
163
|
|
|
*/ |
164
|
|
|
public function getDocumentByRef(string $ref): ?array |
165
|
|
|
{ |
166
|
|
|
$documents = $this->getAllDocuments(); |
167
|
|
|
foreach ($documents as $document) { |
168
|
|
|
if ($document['Ref'] === $ref) { |
169
|
|
|
return $document; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array |
177
|
|
|
* @throws NovaPoshtaApiException |
178
|
|
|
*/ |
179
|
|
|
private function getAllDocuments(): array |
180
|
|
|
{ |
181
|
|
|
return $this->model->getDocumentList([ |
182
|
|
|
'DateTimeFrom' => date('d.m.Y', strtotime('-2 days')), |
183
|
|
|
'DateTimeTo' => date('d.m.Y', strtotime('+2 days')), |
184
|
|
|
'GetFullList' => 1, |
185
|
|
|
'DateTime' => date('d.m.Y'), |
186
|
|
|
], 1); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
private function gulp(): void |
193
|
|
|
{ |
194
|
|
|
$useGulp = getenv('USE_GULP'); |
195
|
|
|
|
196
|
|
|
if ($useGulp) { |
197
|
|
|
sleep(5); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|