AbstractParserTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testList() 0 9 1
A detailProvider() 0 15 1
A listProvider() 0 31 1
A testDetail() 0 25 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SomeWork\Minjust\Tests\Unit\Parser;
6
7
use Iterator;
8
use PHPUnit\Framework\TestCase;
9
use SomeWork\Minjust\Entity\LawFormation;
10
use SomeWork\Minjust\Entity\Lawyer;
11
use SomeWork\Minjust\FindRequest;
12
use SomeWork\Minjust\Parser\ParserInterface;
13
14
abstract class AbstractParserTest extends TestCase
15
{
16
    /**
17
     * @covers ::list
18
     * @dataProvider listProvider
19
     *
20
     * @param string $resource
21
     * @param int    $page
22
     * @param int    $totalPages
23
     * @param int    $count
24
     */
25
    public function testList(string $resource, int $page, int $totalPages, int $count): void
26
    {
27
        $body = file_get_contents($resource);
28
        $response = $this->getParser()->list($body);
29
30
        $this->assertEquals($page, $response->getPage());
31
        $this->assertEquals($totalPages, $response->getTotalPage());
32
        $this->assertCount($count, $response->getLawyers());
33
        $this->assertContainsOnlyInstancesOf(Lawyer::class, $response->getLawyers());
34
    }
35
36
    abstract public function getParser(): ParserInterface;
37
38
    /**
39
     * @covers ::detail
40
     * @dataProvider detailProvider
41
     *
42
     * @param string            $resource
43
     * @param string            $chamberOfLaw
44
     * @param LawFormation|null $lawFormation
45
     */
46
    public function testDetail(string $resource, string $chamberOfLaw, ?LawFormation $lawFormation): void
47
    {
48
        $body = file_get_contents($resource);
49
        $lawyer = $this->getParser()->detail($body);
50
51
        $this->assertIsString($lawyer->getChamberOfLaw());
52
        $this->assertEquals($chamberOfLaw, $lawyer->getChamberOfLaw());
53
54
        if (null === $lawFormation) {
55
            $this->assertNull($lawyer->getLawFormation());
56
        } else {
57
            $this->assertIsString($lawyer->getLawFormation()->getPhone());
58
            $this->assertIsString($lawyer->getLawFormation()->getEmail());
59
            $this->assertIsString($lawyer->getLawFormation()->getName());
60
            $this->assertIsString($lawyer->getLawFormation()->getOrganizationalForm());
61
            $this->assertIsString($lawyer->getLawFormation()->getAddress());
62
63
            $this->assertEquals($lawFormation->getPhone(), $lawyer->getLawFormation()->getPhone());
64
            $this->assertEquals($lawFormation->getEmail(), $lawyer->getLawFormation()->getEmail());
65
            $this->assertEquals($lawFormation->getName(), $lawyer->getLawFormation()->getName());
66
            $this->assertEquals(
67
                $lawFormation->getOrganizationalForm(),
68
                $lawyer->getLawFormation()->getOrganizationalForm()
69
            );
70
            $this->assertEquals($lawFormation->getAddress(), $lawyer->getLawFormation()->getAddress());
71
        }
72
    }
73
74
    public function listProvider(): Iterator
75
    {
76
        yield [
77
            dirname(__DIR__, 2) . '/data/one-page.html',
78
            'page'       => 1,
79
            'totalPages' => 1,
80
            'count'      => 1,
81
        ];
82
        yield [
83
            dirname(__DIR__, 2) . '/data/rewind-not-first.html',
84
            'page'       => 2,
85
            'totalPages' => 2,
86
            'count'      => 8,
87
        ];
88
        yield [
89
            dirname(__DIR__, 2) . '/data/many-page-not-first.html',
90
            'page'       => 2,
91
            'totalPages' => 6706,
92
            'count'      => 20,
93
        ];
94
        yield [
95
            dirname(__DIR__, 2) . '/data/many-page.html',
96
            'page'       => 1,
97
            'totalPages' => 6706,
98
            'count'      => 20,
99
        ];
100
        yield 'web' => [
101
            'http://lawyers.minjust.ru/Lawyers?' . FindRequest::REGISTER_NUMBER . '=77/2340',
102
            'page'       => 1,
103
            'totalPages' => 1,
104
            'count'      => 1,
105
        ];
106
    }
107
108
    public function detailProvider(): Iterator
109
    {
110
        yield 'web Михайлов' => [
111
            'http://lawyers.minjust.ru/lawyers/show/1610663',
112
            'Адвокатская палата г. Москвы',
113
            null,
114
        ];
115
        yield 'web Трофимова' => [
116
            'http://lawyers.minjust.ru/lawyers/show/1529728',
117
            'Палата адвокатов Республики Алтай',
118
            (new LawFormation())
119
                ->setOrganizationalForm('Коллегии адвокатов')
120
                ->setName('Коллегия адвокатов Республики Алтай')
121
                ->setAddress('г.Бийск, ул.Л.Толстого, 160, кв.12')
122
                ->setPhone('89609626799'),
123
        ];
124
    }
125
}
126