1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\NovaPoshta\Tests\Integration\Models\Counterparty; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\ContactPerson; |
10
|
|
|
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty; |
11
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\ConstantsInterface; |
12
|
|
|
use SergeyNezbritskiy\NovaPoshta\Tests\UsesConnectionTrait; |
13
|
|
|
|
14
|
|
|
class SavePrivatePersonTest extends TestCase implements ConstantsInterface |
15
|
|
|
{ |
16
|
|
|
use UsesConnectionTrait; |
17
|
|
|
|
18
|
|
|
private Counterparty $model; |
19
|
|
|
private ContactPerson $contactPersonModel; |
20
|
|
|
|
21
|
|
|
protected function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$connection = $this->getConnection(); |
24
|
|
|
$this->model = new Counterparty($connection); |
25
|
|
|
$this->contactPersonModel = new ContactPerson($connection); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
* @throws Exception |
31
|
|
|
*/ |
32
|
|
|
public function testCounterpartyPrivatePersonCrud(): void |
33
|
|
|
{ |
34
|
|
|
$sfx = $this->randomString(5); |
35
|
|
|
$counterparty = [ |
36
|
|
|
'FirstName' => 'Іван' . $sfx, |
37
|
|
|
'MiddleName' => 'Іванович' . $sfx, |
38
|
|
|
'LastName' => 'Іванов' . $sfx, |
39
|
|
|
'Phone' => rand(380000000000, 380000999999), |
40
|
|
|
'Email' => '[email protected]', |
41
|
|
|
'CounterpartyProperty' => 'Recipient', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
//create counterparty |
45
|
|
|
$actualResult = $this->model->savePrivatePerson($counterparty); |
46
|
|
|
$this->assertIsCounterparty($actualResult); |
47
|
|
|
|
48
|
|
|
//update counterparty |
49
|
|
|
$counterparty['Ref'] = $actualResult['Ref']; |
50
|
|
|
$counterparty['MiddleName'] = 'Петрович' . $sfx; |
51
|
|
|
$counterparty['CityRef'] = self::CITY_REF_KHARKIV; |
52
|
|
|
|
53
|
|
|
$actualResult = $this->model->updatePrivatePerson($counterparty); |
54
|
|
|
$this->assertSame($counterparty['MiddleName'], $actualResult['MiddleName']); |
55
|
|
|
|
56
|
|
|
//get counterparty options |
57
|
|
|
$counterpartyOptions = $this->model->getCounterpartyOptions($counterparty['Ref']); |
58
|
|
|
$this->assertIsCounterpartyOptions($counterpartyOptions); |
59
|
|
|
|
60
|
|
|
//get counterparty contact persons |
61
|
|
|
$contactPersons = $this->model->getCounterpartyContactPersons($counterparty['Ref']); |
62
|
|
|
$this->assertIsContactPerson($contactPersons[0]); |
63
|
|
|
|
64
|
|
|
//delete counterparty |
65
|
|
|
foreach ($contactPersons as $contactPerson) { |
66
|
|
|
$this->contactPersonModel->delete($contactPerson['Ref']); |
67
|
|
|
} |
68
|
|
|
$this->expectExceptionMessage('Counterparty PrivatePerson can\'t be deleted'); |
69
|
|
|
$this->model->delete($counterparty['Ref']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $counterparty |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
private function assertIsCounterparty(array $counterparty): void |
77
|
|
|
{ |
78
|
|
|
$this->assertArrayHasKey('Ref', $counterparty); |
79
|
|
|
$this->assertArrayHasKey('Description', $counterparty); |
80
|
|
|
$this->assertArrayHasKey('FirstName', $counterparty); |
81
|
|
|
$this->assertArrayHasKey('LastName', $counterparty); |
82
|
|
|
$this->assertArrayHasKey('Counterparty', $counterparty); |
83
|
|
|
$this->assertArrayHasKey('EDRPOU', $counterparty); |
84
|
|
|
$this->assertArrayHasKey('CounterpartyType', $counterparty); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $contactPerson |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
private function assertIsContactPerson(array $contactPerson): void |
92
|
|
|
{ |
93
|
|
|
$this->assertArrayHasKey('Ref', $contactPerson); |
94
|
|
|
$this->assertArrayHasKey('Description', $contactPerson); |
95
|
|
|
$this->assertArrayHasKey('Phones', $contactPerson); |
96
|
|
|
$this->assertArrayHasKey('FirstName', $contactPerson); |
97
|
|
|
$this->assertArrayHasKey('LastName', $contactPerson); |
98
|
|
|
$this->assertArrayHasKey('MiddleName', $contactPerson); |
99
|
|
|
$this->assertArrayHasKey('Email', $contactPerson); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $counterpartyOptions |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
private function assertIsCounterpartyOptions(array $counterpartyOptions): void |
107
|
|
|
{ |
108
|
|
|
$this->assertArrayHasKey('Debtor', $counterpartyOptions); |
109
|
|
|
$this->assertArrayHasKey('DebtorParams', $counterpartyOptions); |
110
|
|
|
$this->assertArrayHasKey('SecurePayment', $counterpartyOptions); |
111
|
|
|
$this->assertArrayHasKey('MainCounterparty', $counterpartyOptions); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @throws Exception |
116
|
|
|
*/ |
117
|
|
|
private function randomString(int $length = 10): string |
118
|
|
|
{ |
119
|
|
|
$characters = 'абвгґдеєжзиіїйклмнопрстуфхцчшщьюяАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ'; |
120
|
|
|
$charactersLength = mb_strlen($characters); |
121
|
|
|
$randomString = ''; |
122
|
|
|
for ($i = 0; $i < $length; $i++) { |
123
|
|
|
$symbolPosition = random_int(0, $charactersLength - 1); |
124
|
|
|
$randomString .= mb_substr($characters, $symbolPosition, 1); |
125
|
|
|
} |
126
|
|
|
return $randomString; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|