Passed
Push — master ( 84fad3...cf1b89 )
by Sergey
01:02 queued 12s
created

SaveTest::randomString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SergeyNezbritskiy\Tests\Integration\Models\Counterparty;
6
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use SergeyNezbritskiy\NovaPoshta\Models\Counterparty;
10
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException;
11
use SergeyNezbritskiy\NovaPoshta\Tests\UsesConnectionTrait;
12
13
class SaveTest extends TestCase
14
{
15
    use UsesConnectionTrait;
16
17
    private Counterparty $model;
18
19
    protected function setUp(): void
20
    {
21
        $connection = $this->getConnection();
22
        $this->model = new Counterparty($connection);
23
    }
24
25
26
    /**
27
     * @return void
28
     * @throws Exception
29
     */
30
    public function testCounterpartyCrud(): void
31
    {
32
        $sfx = $this->randomString(5);
33
        $counterparty = [
34
            'FirstName' => 'Іван' . $sfx,
35
            'MiddleName' => 'Іванович' . $sfx,
36
            'LastName' => 'Іванов' . $sfx,
37
            'Phone' => rand(380_000_000_000, 380_000_999_999),
0 ignored issues
show
Bug introduced by
The constant SergeyNezbritskiy\Tests\...erparty\380_000_999_999 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant SergeyNezbritskiy\Tests\...erparty\380_000_000_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
            'Email' => '[email protected]',
39
            'CounterpartyType' => 'PrivatePerson',
40
            'CounterpartyProperty' => 'Recipient',
41
        ];
42
        $actualResult = $this->model->save($counterparty);
43
        $this->assertIsCounterparty($actualResult);
44
    }
45
46
    /**
47
     * @param array $counterparty
48
     * @return void
49
     */
50
    private function assertIsCounterparty(array $counterparty): void
51
    {
52
        $this->assertArrayHasKey('Ref', $counterparty);
53
        $this->assertArrayHasKey('Description', $counterparty);
54
        $this->assertArrayHasKey('FirstName', $counterparty);
55
        $this->assertArrayHasKey('LastName', $counterparty);
56
        $this->assertArrayHasKey('Counterparty', $counterparty);
57
        $this->assertArrayHasKey('EDRPOU', $counterparty);
58
        $this->assertArrayHasKey('CounterpartyType', $counterparty);
59
    }
60
61
    /**
62
     * @throws Exception
63
     */
64
    private function randomString($length = 10): string
65
    {
66
        $characters = 'абвгґдеєжзиіїйклмнопрстуфхцчшщьюяАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ';
67
        $charactersLength = mb_strlen($characters);
68
        $randomString = '';
69
        for ($i = 0; $i < $length; $i++) {
70
            $symbolPosition = random_int(0, $charactersLength - 1);
71
            $randomString .= mb_substr($characters, $symbolPosition, 1);
72
        }
73
        return $randomString;
74
    }
75
}
76