Passed
Pull Request — master (#1)
by Igor
02:41
created

DomParserTest::getPageProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 21
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
namespace SomeWork\Minjust\Tests\Unit;
4
5
use Iterator;
6
use PHPHtmlParser\Dom;
7
use ReflectionClass;
8
use SomeWork\Minjust\Entity\DetailLawyer;
9
use SomeWork\Minjust\Entity\LawFormation;
10
use SomeWork\Minjust\Entity\Lawyer;
11
use SomeWork\Minjust\Parser\DomParser;
12
use SomeWork\Minjust\Parser\ParserInterface;
13
14
/**
15
 * @var DomParser $parser
16
 */
17
class DomParserTest extends AbstractParserTest
18
{
19
    public static function getNewParser(): ParserInterface
20
    {
21
        return new DomParser();
22
    }
23
24
    /**
25
     * @dataProvider getTotalPageProvider
26
     *
27
     * @param string $resource
28
     * @param int    $pages
29
     *
30
     * @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
31
     * @throws \PHPHtmlParser\Exceptions\CircularException
32
     * @throws \PHPHtmlParser\Exceptions\CurlException
33
     * @throws \PHPHtmlParser\Exceptions\StrictException
34
     * @throws \ReflectionException
35
     */
36
    public function testGetTotalPage(string $resource, int $pages): void
37
    {
38
        $dom = (new Dom())->load($resource);
39
40
        $this->assertEquals(
41
            $pages,
42
            $this->invokeMethod(static::$parser, 'getTotalPage', [$dom])
43
        );
44
    }
45
46
    /**
47
     * Call protected/private method of a class.
48
     *
49
     * @param object &$object     Instantiated object that we will run method on.
50
     * @param string  $methodName Method name to call
51
     * @param array   $parameters Array of parameters to pass into method.
52
     *
53
     * @return mixed Method return.
54
     * @throws \ReflectionException
55
     */
56
    public function invokeMethod(&$object, $methodName, array $parameters = [])
57
    {
58
        $reflection = new ReflectionClass(get_class($object));
59
        $method = $reflection->getMethod($methodName);
60
        $method->setAccessible(true);
61
62
        return $method->invokeArgs($object, $parameters);
63
    }
64
65
    /**
66
     * @dataProvider getPageProvider
67
     *
68
     * @param string $resource
69
     * @param int    $page
70
     *
71
     * @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
72
     * @throws \PHPHtmlParser\Exceptions\CircularException
73
     * @throws \PHPHtmlParser\Exceptions\CurlException
74
     * @throws \PHPHtmlParser\Exceptions\StrictException
75
     * @throws \ReflectionException
76
     */
77
    public function testGetCurrentPage(string $resource, int $page): void
78
    {
79
        $dom = (new Dom())->load($resource);
80
        $this->assertEquals($page, $this->invokeMethod(static::$parser, 'getCurrentPage', [$dom]),
81
            'Wrong for: ' . $resource);
82
    }
83
84
    /**
85
     * @dataProvider getListLawyersProvider
86
     *
87
     * @param string $resource
88
     *
89
     * @param int    $count
90
     *
91
     * @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
92
     * @throws \PHPHtmlParser\Exceptions\CircularException
93
     * @throws \PHPHtmlParser\Exceptions\CurlException
94
     * @throws \PHPHtmlParser\Exceptions\StrictException
95
     * @throws \ReflectionException
96
     */
97
    public function testGetListLawyers(string $resource, int $count): void
98
    {
99
        $dom = (new Dom())->load($resource);
100
        $lawyers = $this->invokeMethod(static::$parser, 'getListLawyers', [$dom]);
101
        $this->assertIsArray($lawyers);
102
        $this->assertCount($count, $lawyers);
103
104
        foreach ($lawyers as $lawyer) {
105
            $this->assertInstanceOf(Lawyer::class, $lawyer);
106
107
            $this->assertIsString($lawyer->getFullName());
108
            $this->assertGreaterThan(0, strlen($lawyer->getFullName()));
109
110
            $this->assertIsString($lawyer->getRegisterNumber());
111
            $this->assertGreaterThan(0, strlen($lawyer->getRegisterNumber()));
112
113
            $this->assertIsString($lawyer->getCertificateNumber());
114
            $this->assertGreaterThanOrEqual(0, strlen($lawyer->getCertificateNumber()));
115
116
            $this->assertIsString($lawyer->getStatus());
117
            $this->assertGreaterThan(0, strlen($lawyer->getStatus()));
118
119
            $this->assertIsString($lawyer->getTerritorialSubject());
120
            $this->assertGreaterThan(0, strlen($lawyer->getTerritorialSubject()));
121
122
            $this->assertIsString($lawyer->getUrl());
123
            $this->assertGreaterThan(0, strlen($lawyer->getUrl()));
124
        }
125
    }
126
127
    public function getPageProvider(): Iterator
128
    {
129
        yield 'one-page' => [
130
            'resource' => dirname(__DIR__) . '/data/one-page.html',
131
            'page'     => 1,
132
        ];
133
        yield 'many-page' => [
134
            'resource' => dirname(__DIR__) . '/data/many-page.html',
135
            'page'     => 1,
136
        ];
137
        yield 'many-page-not-first' => [
138
            'resource' => dirname(__DIR__) . '/data/many-page-not-first.html',
139
            'page'     => 6,
140
        ];
141
        yield 'rewind-not-first' => [
142
            'resource' => dirname(__DIR__) . '/data/rewind-not-first.html',
143
            'page'     => 2,
144
        ];
145
        yield 'web' => [
146
            'resource' => 'http://lawyers.minjust.ru/Lawyers',
147
            'page'     => 1,
148
        ];
149
    }
150
151
    public function getTotalPageProvider(): Iterator
152
    {
153
        yield 'one-page' => [
154
            'resource' => dirname(__DIR__) . '/data/one-page.html',
155
            'pages'    => 1,
156
        ];
157
        yield 'many-page' => [
158
            'resource' => dirname(__DIR__) . '/data/many-page.html',
159
            'pages'    => 6657,
160
        ];
161
        yield 'many-page-not-first' => [
162
            'resource' => dirname(__DIR__) . '/data/many-page-not-first.html',
163
            'pages'    => 58,
164
        ];
165
        yield 'rewind-not-first' => [
166
            'resource' => dirname(__DIR__) . '/data/rewind-not-first.html',
167
            'pages'    => 2,
168
        ];
169
    }
170
171
    public function getListLawyersProvider(): Iterator
172
    {
173
        yield 'one-page' => [
174
            'resource' => dirname(__DIR__) . '/data/one-page.html',
175
            'count'    => 12,
176
        ];
177
        yield 'many-page' => [
178
            'resource' => dirname(__DIR__) . '/data/many-page.html',
179
            'count'    => 20,
180
        ];
181
        yield 'many-page-not-first' => [
182
            'resource' => dirname(__DIR__) . '/data/many-page-not-first.html',
183
            'count'    => 20,
184
        ];
185
        yield 'rewind-not-first' => [
186
            'resource' => dirname(__DIR__) . '/data/rewind-not-first.html',
187
            'count'    => 8,
188
        ];
189
        yield 'web' => [
190
            'resource' => 'http://lawyers.minjust.ru/Lawyers',
191
            'count'    => 20,
192
        ];
193
    }
194
195
    /**
196
     * @dataProvider getFullLawyerProvider
197
     *
198
     * @param string                                $resource
199
     * @param \SomeWork\Minjust\Entity\DetailLawyer $exampleLawyer
200
     *
201
     * @throws \PHPHtmlParser\Exceptions\ChildNotFoundException
202
     * @throws \PHPHtmlParser\Exceptions\CircularException
203
     * @throws \PHPHtmlParser\Exceptions\CurlException
204
     * @throws \PHPHtmlParser\Exceptions\StrictException
205
     * @throws \ReflectionException
206
     */
207
    public function testGetFullLawyer(string $resource, DetailLawyer $exampleLawyer): void
208
    {
209
        $dom = (new Dom())->load($resource);
210
        /**
211
         * @var DetailLawyer $lawyer
212
         */
213
        $lawyer = $this->invokeMethod(static::$parser, 'getFullLawyer', [$dom]);
214
215
        $this->assertEquals($exampleLawyer->getChamberOfLaw(), $lawyer->getChamberOfLaw());
216
217
        $exampleLawFormation = $exampleLawyer->getLawFormation();
218
        $lawFormation = $lawyer->getLawFormation();
219
        $this->assertInstanceOf(LawFormation::class, $lawFormation);
220
        $this->assertEquals($exampleLawFormation->getOrganizationalForm(), $lawFormation->getOrganizationalForm());
221
        $this->assertEquals($exampleLawFormation->getName(), $lawFormation->getName());
222
        $this->assertEquals($exampleLawFormation->getEmail(), $lawFormation->getEmail());
223
        $this->assertEquals($exampleLawFormation->getPhone(), $lawFormation->getPhone());
224
    }
225
226
    public function getFullLawyerProvider(): Iterator
227
    {
228
        yield 'web' => [
229
            'resource' => 'http://lawyers.minjust.ru/lawyers/show/1532185',
230
            'lawyer'   => (new DetailLawyer())
231
                ->setChamberOfLaw('Адвокатская палата Амурской области')
232
                ->setLawFormation(
233
                    (new LawFormation())
234
                        ->setOrganizationalForm('Адвокатские кабинеты')
235
                        ->setName('Адвокатский кабинет')
236
                        ->setAddress('')
237
                        ->setPhone('. 89098119329')
238
                        ->setEmail('E-mail: [email protected]')
239
                ),
240
        ];
241
    }
242
}
243