1 | <?php |
||
15 | class SpiderTest extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var Spider |
||
19 | */ |
||
20 | protected $spider; |
||
21 | |||
22 | /** |
||
23 | * @var PHPUnit_Framework_MockObject_MockObject |
||
24 | */ |
||
25 | protected $requestHandler; |
||
26 | |||
27 | /** @var DiscoveredUri */ |
||
28 | protected $linkA; |
||
29 | /** @var DiscoveredUri */ |
||
30 | protected $linkB; |
||
31 | /** @var DiscoveredUri */ |
||
32 | protected $linkC; |
||
33 | /** @var DiscoveredUri */ |
||
34 | protected $linkD; |
||
35 | /** @var DiscoveredUri */ |
||
36 | protected $linkE; |
||
37 | /** @var DiscoveredUri */ |
||
38 | protected $linkF; |
||
39 | /** @var DiscoveredUri */ |
||
40 | protected $linkG; |
||
41 | |||
42 | /** @var Response */ |
||
43 | protected $responseA; |
||
44 | /** @var Response */ |
||
45 | protected $responseB; |
||
46 | /** @var Response */ |
||
47 | protected $responseC; |
||
48 | /** @var Response */ |
||
49 | protected $responseD; |
||
50 | /** @var Response */ |
||
51 | protected $responseE; |
||
52 | /** @var Response */ |
||
53 | protected $responseF; |
||
54 | /** @var Response */ |
||
55 | protected $responseG; |
||
56 | |||
57 | /** @var string */ |
||
58 | protected $hrefA; |
||
59 | protected $hrefB; |
||
60 | protected $hrefC; |
||
61 | protected $hrefD; |
||
62 | protected $hrefE; |
||
63 | protected $hrefF; |
||
64 | protected $hrefG; |
||
65 | |||
66 | /** |
||
67 | * @var array An associative array, containing a map of $this->linkX to $this->responseX. |
||
68 | */ |
||
69 | protected $linkToResponseMap = []; |
||
70 | |||
71 | /** |
||
72 | * Sets up the fixture, for example, opens a network connection. |
||
73 | * This method is called before a test is executed. |
||
74 | * |
||
75 | * Setting up the following structure: |
||
76 | * |
||
77 | * 0: A |
||
78 | * /|\ |
||
79 | * 1: B C E |
||
80 | * /| | | |
||
81 | * 2: D F G | |
||
82 | * | _ | |
||
83 | * |
||
84 | * Note: E links to F. |
||
85 | */ |
||
86 | protected function setUp(): void |
||
154 | |||
155 | /** |
||
156 | * @return Resource |
||
157 | * @throws \ErrorException |
||
158 | */ |
||
159 | public function doTestRequest() |
||
169 | |||
170 | /** |
||
171 | * @covers VDB\Spider\Spider |
||
172 | * |
||
173 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
174 | */ |
||
175 | public function testCrawlDFSDefaultBehaviour() |
||
176 | { |
||
177 | $this->spider->getDiscovererSet()->maxDepth = 10; |
||
178 | |||
179 | $this->spider->crawl(); |
||
180 | |||
181 | $expected = array( |
||
182 | $this->linkA, |
||
183 | $this->linkE, |
||
184 | $this->linkF, |
||
185 | $this->linkC, |
||
186 | $this->linkG, |
||
187 | $this->linkB, |
||
188 | $this->linkD |
||
189 | ); |
||
190 | |||
191 | $this->compareUriArray($expected, $this->spider->getDownloader()->getPersistenceHandler()); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @covers VDB\Spider\Spider |
||
196 | */ |
||
197 | public function testCrawlBFSDefaultBehaviour() |
||
198 | { |
||
199 | $this->spider->getQueueManager()->setTraversalAlgorithm(InMemoryQueueManager::ALGORITHM_BREADTH_FIRST); |
||
200 | $this->spider->getDiscovererSet()->maxDepth = 1000; |
||
201 | |||
202 | $this->spider->crawl(); |
||
203 | |||
204 | $expected = array( |
||
205 | $this->linkA, |
||
206 | $this->linkB, |
||
207 | $this->linkC, |
||
208 | $this->linkE, |
||
209 | $this->linkD, |
||
210 | $this->linkF, |
||
211 | $this->linkG |
||
212 | ); |
||
213 | |||
214 | $this->compareUriArray($expected, $this->spider->getDownloader()->getPersistenceHandler()); |
||
215 | } |
||
216 | |||
217 | private function compareUriArray($expected, $actual) |
||
223 | |||
224 | /** |
||
225 | * @covers VDB\Spider\Spider |
||
226 | * |
||
227 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
228 | * |
||
229 | * Given the following structure: |
||
230 | * |
||
231 | * 0: A |
||
232 | * /|\ |
||
233 | * 1: B C E |
||
234 | * /| | | |
||
235 | * 2: D F G | |
||
236 | * | _ | |
||
237 | * |
||
238 | * We expect the following result: A, E, C, B |
||
239 | * |
||
240 | */ |
||
241 | public function testCrawlDFSMaxDepthOne() |
||
256 | |||
257 | /** |
||
258 | * @covers VDB\Spider\Spider |
||
259 | */ |
||
260 | public function testCrawlBFSMaxDepthOne() |
||
261 | { |
||
262 | $this->spider->getQueueManager()->setTraversalAlgorithm(InMemoryQueueManager::ALGORITHM_BREADTH_FIRST); |
||
263 | $this->spider->getDiscovererSet()->maxDepth = 1; |
||
264 | |||
265 | $this->spider->crawl(); |
||
266 | |||
267 | $expected = array( |
||
268 | $this->linkA, |
||
269 | $this->linkB, |
||
270 | $this->linkC, |
||
271 | $this->linkE, |
||
272 | ); |
||
273 | |||
274 | $this->compareUriArray($expected, $this->spider->getDownloader()->getPersistenceHandler()); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @covers VDB\Spider\Spider |
||
279 | */ |
||
280 | public function testCrawlDFSMaxQueueSize() |
||
281 | { |
||
282 | $this->spider->getDiscovererSet()->maxDepth = 1000; |
||
283 | $this->spider->getDownloader()->setDownloadLimit(3); |
||
284 | |||
285 | $this->spider->crawl(); |
||
286 | |||
287 | $expected = array( |
||
288 | $this->linkA, |
||
289 | $this->linkE, |
||
290 | $this->linkF, |
||
291 | ); |
||
292 | |||
293 | $this->compareUriArray($expected, $this->spider->getDownloader()->getPersistenceHandler()); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @covers VDB\Spider\Spider |
||
298 | */ |
||
299 | public function testCrawlBFSMaxQueueSize() |
||
300 | { |
||
301 | $this->spider->getQueueManager()->setTraversalAlgorithm(InMemoryQueueManager::ALGORITHM_BREADTH_FIRST); |
||
302 | $this->spider->getDiscovererSet()->maxDepth = 1000; |
||
303 | $this->spider->getDownloader()->setDownloadLimit(3); |
||
304 | |||
305 | $this->spider->crawl(); |
||
306 | |||
307 | $expected = array( |
||
308 | $this->linkA, |
||
309 | $this->linkB, |
||
310 | $this->linkC, |
||
311 | ); |
||
312 | |||
313 | $this->compareUriArray($expected, $this->spider->getDownloader()->getPersistenceHandler()); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @covers VDB\Spider\Spider |
||
318 | */ |
||
319 | public function testCrawlFailedRequest() |
||
332 | } |
||
333 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..