Test Failed
Pull Request — master (#13)
by Sergey
03:15
created

CrudTest::assertDocumentDeleted()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 3
nc 3
nop 1
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
    protected function setUp(): void
22
    {
23
        date_default_timezone_set('Europe/Kyiv');
24
        $connection = $this->getConnection();
25
        $this->model = new InternetDocument($connection);
26
    }
27
28
    /**
29
     * @throws NovaPoshtaApiException
30
     */
31
    public function testCrudDocument(): void
32
    {
33
34
//        $counterpartyModel = new \SergeyNezbritskiy\NovaPoshta\Models\Counterparty($this->getConnection());
35
//        $addressModel = new \SergeyNezbritskiy\NovaPoshta\Models\Address($this->getConnection());
36
37
//        $recipient = $counterpartyModel->savePrivatePerson([
38
//            'FirstName' => 'Петро',
39
//            'MiddleName' => 'Григорович',
40
//            'LastName' => 'Порошенко',
41
//            'Phone' => 380501112233,
42
//            'Email' => '[email protected]',
43
//            'CounterpartyProperty' => Counterparty::COUNTERPARTY_PROPERTY_RECIPIENT,
44
//        ]); // 4d21c8c7-b88e-11ed-a60f-48df37b921db
45
        $recipientRef = '4d21c8c7-b88e-11ed-a60f-48df37b921db';
46
47
//        $address = $addressModel->save($recipientRef, [
48
//            'StreetRef' => self::KHRESHCHATYK_STREET_REF,
49
//            'BuildingNumber' => '20',
50
//            'Flat' => '12',
51
//        ]);
52
        $addressRef = 'cecaac32-25bb-11ee-a60f-48df37b921db';
53
54
//        $contactPersons = $counterpartyModel->getCounterpartyContactPersons(self::COUNTERPARTY_REF);
55
        $senderContactRef = '4d06cbac-b88e-11ed-a60f-48df37b921db';
56
57
//        $contactPersons = $counterpartyModel->getCounterpartyContactPersons($recipientRef);
58
        $recipientContactRef = '45b55250-25bb-11ee-a60f-48df37b921db';
59
60
        //create document
61
        $params = [
62
            'Sender' => self::COUNTERPARTY_REF,
63
            'CitySender' => self::CITY_REF_KHARKIV,
64
            'SenderAddress' => self::ADDRESS_REF_KHARKIV,
65
            'ContactSender' => $senderContactRef,
66
            'SendersPhone' => 380505511696,
67
68
            'Recipient' => $recipientRef,
69
            'CityRecipient' => self::CITY_REF_KYIV,
70
            'RecipientAddress' => $addressRef,
71
            'ContactRecipient' => $recipientContactRef,
72
            'RecipientsPhone' => 380505511696,
73
74
            'DateTime' => date('d.m.Y'),
75
            'CargoType' => InternetDocument::CARGO_TYPE_CARGO,
76
            'Weight' => '0.5',
77
            'SeatsAmount' => '1',
78
            'ServiceType' => 'DoorsDoors',
79
            'PayerType' => 'Recipient',
80
            'PaymentMethod' => 'Cash',
81
            'Description' => 'TEST, DO NOT PROCEED WITH THIS'
82
        ];
83
84
        $actualResult = $this->model->save($params);
85
        $this->assertNotEmpty($actualResult['Ref']);
86
87
        //delete document
88
        $this->deleteDocument($actualResult['Ref']);
89
90
        $this->assertDocumentDeleted($actualResult['Ref']);
91
    }
92
93
    /**
94
     * @param string $ref
95
     * @param int $attempt
96
     * @return void
97
     * @throws NovaPoshtaApiException
98
     * @SuppressWarninds(PHPMD.ElseExpression)
99
     */
100
    private function deleteDocument(string $ref, int $attempt = 1): void
101
    {
102
        sleep(5 * $attempt);
103
        try {
104
            $this->model->delete($ref);
105
        } catch (NovaPoshtaApiException $e) {
106
            $docNotCreatedYet = $e->getMessage() === 'No document changed DeletionMark';
107
            $attemptsNotExceeded = $attempt <= 3;
108
            if ($attemptsNotExceeded && $docNotCreatedYet) {
109
                $this->deleteDocument($ref, ++$attempt);
110
            } else {
111
                throw $e;
112
            }
113
        }
114
    }
115
116
    /**
117
     * @param string $ref
118
     * @return void
119
     * @throws NovaPoshtaApiException
120
     */
121
    private function assertDocumentDeleted(string $ref): void
122
    {
123
        $documents = $this->model->getDocumentList([
124
            'DateTimeFrom' => date('d.m.Y', strtotime('-2 days')),
125
            'DateTimeTo' => date('d.m.Y', strtotime('+2 days')),
126
            'GetFullList' => 1,
127
            'DateTime' => date('d.m.Y'),
128
        ], 1);
129
        foreach ($documents as $document) {
130
            if ($document['Ref'] === $ref) {
131
                $this->fail('Failed to delete document.');
132
            }
133
        }
134
        $this->assertTrue(true, 'Just to suppress error that method doesn\'t do any assertions');
135
    }
136
}
137