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
|
|
|
$address = [ |
57
|
|
|
'StreetRef' => self::STREET_REF, |
58
|
|
|
'BuildingNumber' => '68', |
59
|
|
|
'Flat' => $flat |
60
|
|
|
]; |
61
|
|
|
$actualResult = $this->model->save(self::COUNTERPARTY_REF, $address, $note); |
62
|
|
|
$this->assertAddress($actualResult, $flat, $note); |
63
|
|
|
return $actualResult['Ref']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $addressRef |
68
|
|
|
* @return void |
69
|
|
|
* @throws NovaPoshtaApiException |
70
|
|
|
*/ |
71
|
|
|
private function performAddressUpdate(string $addressRef): void |
72
|
|
|
{ |
73
|
|
|
$note = 'This address is for testing (updated)'; |
74
|
|
|
$flat = '333'; |
75
|
|
|
$address = [ |
76
|
|
|
'Ref' => $addressRef, |
77
|
|
|
'CounterpartyRef' => self::COUNTERPARTY_REF, |
78
|
|
|
'StreetRef' => self::STREET_REF, |
79
|
|
|
'BuildingNumber' => '68', |
80
|
|
|
'Flat' => $flat, |
81
|
|
|
]; |
82
|
|
|
$actualResult = $this->model->update($address, $note); |
83
|
|
|
|
84
|
|
|
$this->assertAddress($actualResult, $flat, $note); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $addressRef |
89
|
|
|
* @return void |
90
|
|
|
* @throws NovaPoshtaApiException |
91
|
|
|
*/ |
92
|
|
|
private function performAddressDelete(string $addressRef): void |
93
|
|
|
{ |
94
|
|
|
$this->model->delete($addressRef); |
95
|
|
|
$addresses = $this->counterpartyModel->getCounterpartyAddresses(self::COUNTERPARTY_REF, 'Sender'); |
96
|
|
|
$this->assertAddressMissing($addressRef, $addresses); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $actualResult |
101
|
|
|
* @param string $flat |
102
|
|
|
* @param string $note |
103
|
|
|
* @return void |
104
|
|
|
* @throws NovaPoshtaApiException |
105
|
|
|
*/ |
106
|
|
|
private function assertAddress(array $actualResult, string $flat, string $note): void |
107
|
|
|
{ |
108
|
|
|
$this->assertArrayHasKey('Ref', $actualResult); |
109
|
|
|
$this->assertArrayHasKey('Description', $actualResult); |
110
|
|
|
$this->assertStringContainsString($flat, $actualResult['Description']); |
111
|
|
|
$this->assertStringContainsString($note, $actualResult['Description']); |
112
|
|
|
|
113
|
|
|
//ensure that address has been created |
114
|
|
|
$addresses = $this->counterpartyModel->getCounterpartyAddresses(self::COUNTERPARTY_REF, 'Sender'); |
115
|
|
|
$this->assertAddressExists($actualResult['Ref'], $addresses); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $addressRef |
120
|
|
|
* @param array $addresses |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
private function assertAddressExists(string $addressRef, array $addresses): void |
124
|
|
|
{ |
125
|
|
|
foreach ($addresses as $address) { |
126
|
|
|
if ($address['Ref'] === $addressRef) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$this->fail('Seems like address has not been created properly'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $addressRef |
135
|
|
|
* @param array $addresses |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
private function assertAddressMissing(string $addressRef, array $addresses): void |
139
|
|
|
{ |
140
|
|
|
foreach ($addresses as $address) { |
141
|
|
|
if ($address['Ref'] === $addressRef) { |
142
|
|
|
$this->fail('Address has not been deleted.'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|