DownloaderTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testDownload() 0 4 1
A testFilterNotMatches() 0 12 1
A testDownloadFailed() 0 12 1
A testFilterMatches() 0 12 1
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(): void
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->getMockBuilder('VDB\Spider\RequestHandler\RequestHandlerInterface')->getMock();
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
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
72
     */
73
    public function testDownloadFailed()
74
    {
75
        $requestHandler = $this->getMockBuilder('VDB\Spider\RequestHandler\RequestHandlerInterface')->getMock();
76
        $requestHandler
77
            ->expects($this->any())
78
            ->method('request')
79
            ->will($this->throwException(new \Exception));
80
        $this->downloader->setRequestHandler($requestHandler);
81
82
        $resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org')));
83
84
        $this->assertFalse($resource);
85
    }
86
87
    /**
88
     * @covers VDB\Spider\Downloader\Downloader
89
     */
90
    public function testFilterNotMatches()
91
    {
92
        $filterNeverMatch = $this->getMockBuilder('VDB\Spider\Filter\PostFetchFilterInterface')->getMock();
93
        $filterNeverMatch
94
            ->expects($this->any())
95
            ->method('match')
96
            ->will($this->returnValue(false));
97
        $this->downloader->addPostFetchFilter($filterNeverMatch);
98
99
        $resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org')));
100
101
        $this->assertInstanceOf('VDB\\Spider\\Resource', $resource);
102
    }
103
104
    /**
105
     * @covers VDB\Spider\Downloader\Downloader
106
     */
107
    public function testFilterMatches()
108
    {
109
        $filterAlwaysMatch = $this->getMockBuilder('VDB\Spider\Filter\PostFetchFilterInterface')->getMock();
110
        $filterAlwaysMatch
111
            ->expects($this->any())
112
            ->method('match')
113
            ->will($this->returnValue(true));
114
        $this->downloader->addPostFetchFilter($filterAlwaysMatch);
115
116
        $resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org')));
117
118
        $this->assertFalse($resource);
119
    }
120
}
121