AddressCrudTest::performAddressUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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