Completed
Pull Request — master (#27)
by Matthijs
02:37
created

DownloaderTest::testDownload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
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