Passed
Push — master ( 727475...c2e18b )
by Igor
04:07
created

AbstractParserTest   A

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