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