Failed Conditions
Push — master ( fc7301...8aadf7 )
by Matthijs
06:40
created

DownloaderTest::testFilterNotMatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
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(): 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);
0 ignored issues
show
Documentation introduced by
$requestHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<VDB\Spider\Reques...equestHandlerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 View Code Duplication
    public function testDownloadFailed()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$requestHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<VDB\Spider\Reques...equestHandlerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 View Code Duplication
    public function testFilterNotMatches()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$filterNeverMatch is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<VDB\Spider\Filter...stFetchFilterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 View Code Duplication
    public function testFilterMatches()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$filterAlwaysMatch is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<VDB\Spider\Filter...stFetchFilterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
116
        $resource = $this->downloader->download(new DiscoveredUri(new Uri('http://foobar.org')));
117
118
        $this->assertFalse($resource);
119
    }
120
}
121