1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\Tests\Integration\Models\Address; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Address; |
9
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty; |
10
|
|
|
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException; |
11
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\UsesConnectionTrait; |
12
|
|
|
|
13
|
|
|
class AddressCrudTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
use UsesConnectionTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* City Kharkov |
19
|
|
|
*/ |
20
|
|
|
private const COUNTERPARTY_REF = '98d078a0-e096-11e6-8ba8-005056881c6b'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Вулиця Тимурiвцiв |
24
|
|
|
*/ |
25
|
|
|
private const STREET_REF = 'a7503d2c-8c06-11de-9467-001ec9d9f7b7'; |
26
|
|
|
|
27
|
|
|
private Address $model; |
28
|
|
|
private Counterparty $counterpartyModel; |
29
|
|
|
|
30
|
|
|
protected function setUp(): void |
31
|
|
|
{ |
32
|
|
|
$connection = $this->getConnection(); |
33
|
|
|
$this->model = new Address($connection); |
34
|
|
|
$this->counterpartyModel = new Counterparty($connection); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return void |
39
|
|
|
* @throws NovaPoshtaApiException |
40
|
|
|
*/ |
41
|
|
|
public function testAddressCrud(): void |
42
|
|
|
{ |
43
|
|
|
$addressRef = $this->performAddressCreate(); |
44
|
|
|
$this->performAddressUpdate($addressRef); |
45
|
|
|
$this->performAddressDelete($addressRef); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
* @throws NovaPoshtaApiException |
51
|
|
|
*/ |
52
|
|
|
private function performAddressCreate(): string |
53
|
|
|
{ |
54
|
|
|
$note = 'This address is for testing (created)'; |
55
|
|
|
$flat = '111'; |
56
|
|
|
$actualResult = $this->model->save(self::COUNTERPARTY_REF, self::STREET_REF, '68', $flat, $note); |
57
|
|
|
$this->assertAddress($actualResult, $flat, $note); |
58
|
|
|
return $actualResult['Ref']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $addressRef |
63
|
|
|
* @return void |
64
|
|
|
* @throws NovaPoshtaApiException |
65
|
|
|
*/ |
66
|
|
|
private function performAddressUpdate(string $addressRef): void |
67
|
|
|
{ |
68
|
|
|
$note = 'This address is for testing (updated)'; |
69
|
|
|
$flat = '333'; |
70
|
|
|
$address = [ |
71
|
|
|
'CounterpartyRef' => self::COUNTERPARTY_REF, |
72
|
|
|
'StreetRef' => self::STREET_REF, |
73
|
|
|
'BuildingNumber' => '68', |
74
|
|
|
'Flat' => $flat, |
75
|
|
|
]; |
76
|
|
|
$actualResult = $this->model->update($addressRef, $address, $note); |
77
|
|
|
|
78
|
|
|
$this->assertAddress($actualResult, $flat, $note); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $addressRef |
83
|
|
|
* @return void |
84
|
|
|
* @throws NovaPoshtaApiException |
85
|
|
|
*/ |
86
|
|
|
private function performAddressDelete(string $addressRef): void |
87
|
|
|
{ |
88
|
|
|
$this->model->delete($addressRef); |
89
|
|
|
$addresses = $this->counterpartyModel->getCounterpartyAddresses(self::COUNTERPARTY_REF, 'Sender'); |
90
|
|
|
$this->assertAddressMissing($addressRef, $addresses); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $actualResult |
95
|
|
|
* @param string $flat |
96
|
|
|
* @param string $note |
97
|
|
|
* @return void |
98
|
|
|
* @throws NovaPoshtaApiException |
99
|
|
|
*/ |
100
|
|
|
private function assertAddress(array $actualResult, string $flat, string $note): void |
101
|
|
|
{ |
102
|
|
|
$this->assertArrayHasKey('Ref', $actualResult); |
103
|
|
|
$this->assertArrayHasKey('Description', $actualResult); |
104
|
|
|
$this->assertStringContainsString($flat, $actualResult['Description']); |
105
|
|
|
$this->assertStringContainsString($note, $actualResult['Description']); |
106
|
|
|
|
107
|
|
|
//ensure that address has been created |
108
|
|
|
$addresses = $this->counterpartyModel->getCounterpartyAddresses(self::COUNTERPARTY_REF, 'Sender'); |
109
|
|
|
$this->assertAddressExists($actualResult['Ref'], $addresses); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $addressRef |
114
|
|
|
* @param array $addresses |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
private function assertAddressExists(string $addressRef, array $addresses): void |
118
|
|
|
{ |
119
|
|
|
foreach ($addresses as $address) { |
120
|
|
|
if ($address['Ref'] === $addressRef) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
$this->fail('Seems like address has not been created properly'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $addressRef |
129
|
|
|
* @param array $addresses |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
private function assertAddressMissing(string $addressRef, array $addresses): void |
133
|
|
|
{ |
134
|
|
|
foreach ($addresses as $address) { |
135
|
|
|
if ($address['Ref'] === $addressRef) { |
136
|
|
|
$this->fail('Address has not been deleted.'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|