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

DomParserTest::invokeMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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