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\Discoverer; |
13
|
|
|
|
14
|
|
|
use DOMDocument; |
15
|
|
|
use DOMElement; |
16
|
|
|
use GuzzleHttp\Psr7\Response; |
17
|
|
|
use VDB\Spider\Resource; |
18
|
|
|
use VDB\Spider\Spider; |
19
|
|
|
use VDB\Spider\Tests\TestCase; |
20
|
|
|
use VDB\Spider\Uri\DiscoveredUri; |
21
|
|
|
use VDB\Spider\Discoverer\DiscovererInterface; |
22
|
|
|
use VDB\Uri\Uri; |
23
|
|
|
|
24
|
|
|
abstract class DiscovererTestCase extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** @var \DomDocument */ |
27
|
|
|
protected $domDocument; |
28
|
|
|
|
29
|
|
|
/** @var \DomElement */ |
30
|
|
|
protected $domAnchor; |
31
|
|
|
|
32
|
|
|
/** @var Resource */ |
33
|
|
|
protected $spiderResource; |
34
|
|
|
|
35
|
|
|
/** @var DiscoveredUri */ |
36
|
|
|
protected $uri; |
37
|
|
|
|
38
|
|
|
protected $uriInBody1; |
39
|
|
|
|
40
|
|
|
protected function setUp() |
41
|
|
|
{ |
42
|
|
|
$this->uriInBody1 = 'http://php-spider.org/contact/'; |
43
|
|
|
|
44
|
|
|
// Setup DOM |
45
|
|
|
$this->domDocument = new \DOMDocument('1', 'UTF-8'); |
46
|
|
|
|
47
|
|
|
$html = $this->domDocument->createElement('html'); |
48
|
|
|
$this->domAnchor = $this->domDocument->createElement('a', 'fake'); |
49
|
|
|
$this->domAnchor->setAttribute('href', $this->uriInBody1); |
50
|
|
|
|
51
|
|
|
$this->domDocument->appendChild($html); |
52
|
|
|
$html->appendChild($this->domAnchor); |
53
|
|
|
|
54
|
|
|
$this->uri = new DiscoveredUri('http://php-spider.org/'); |
55
|
|
|
|
56
|
|
|
// Setup Spider\Resource |
57
|
|
|
$content = $this->domDocument->saveHTML(); |
58
|
|
|
|
59
|
|
|
$this->spiderResource = new Resource( |
60
|
|
|
$this->uri, |
61
|
|
|
new Response(200, [], $content) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function executeDiscoverer(DiscovererInterface $discoverer) |
66
|
|
|
{ |
67
|
|
|
$uris = $discoverer->discover($this->spiderResource); |
68
|
|
|
$uri = $uris[0]; |
69
|
|
|
|
70
|
|
|
$this->assertInstanceOf('VDB\\Spider\\Uri\\DiscoveredUri', $uri); |
71
|
|
|
$this->assertEquals($this->uriInBody1, $uri->toString()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|