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