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