|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Spider package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Matthijs van den Bos <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace VDB\Spider\Tests\Downloader; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\Response; |
|
15
|
|
|
use VDB\Spider\Tests\TestCase; |
|
16
|
|
|
use VDB\Spider\Downloader\DownloaderInterface; |
|
17
|
|
|
use VDB\Spider\Downloader\Downloader; |
|
18
|
|
|
use VDB\Spider\Uri\DiscoveredUri; |
|
19
|
|
|
use VDB\Spider\Resource; |
|
20
|
|
|
use VDB\Uri\Uri; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class DownloaderTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Downloader |
|
29
|
|
|
*/ |
|
30
|
|
|
private $downloader; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Resource |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $resource; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $html; |
|
41
|
|
|
|
|
42
|
|
|
public function setUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->html = file_get_contents(__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html'); |
|
45
|
|
|
$this->resource = new Resource( |
|
46
|
|
|
new DiscoveredUri(new Uri('/domains/special', 'http://example.org')), |
|
47
|
|
|
new Response(200, [], $this->html) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->downloader = new Downloader(); |
|
51
|
|
|
|
|
52
|
|
|
$requestHandler = $this->getMock('VDB\Spider\RequestHandler\RequestHandlerInterface'); |
|
53
|
|
|
$requestHandler |
|
54
|
|
|
->expects($this->any()) |
|
55
|
|
|
->method('request') |
|
56
|
|
|
->will($this->returnValue($this->resource)); |
|
57
|
|
|
|
|
58
|
|
|
$this->downloader->setRequestHandler($requestHandler); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @covers VDB\Spider\Downloader\Downloader::download() |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testDownload() |
|
65
|
|
|
{ |
|
66
|
|
|
$resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org'))); |
|
67
|
|
|
$this->assertInstanceOf('VDB\\Spider\\Resource', $resource); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @covers VDB\Spider\Downloader\Downloader::download() |
|
72
|
|
|
* @covers VDB\Spider\Downloader\Downloader::matchesPostfetchFilter() |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testFilter() |
|
75
|
|
|
{ |
|
76
|
|
|
$filterAlwaysMatch = $this->getMock('VDB\Spider\Filter\PostFetchFilterInterface'); |
|
77
|
|
|
$filterAlwaysMatch |
|
78
|
|
|
->expects($this->any()) |
|
79
|
|
|
->method('match') |
|
80
|
|
|
->will($this->returnValue(true)); |
|
81
|
|
|
$this->downloader->addPostFetchFilter($filterAlwaysMatch); |
|
82
|
|
|
|
|
83
|
|
|
$resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org'))); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertFalse($resource); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|