1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Spider package. |
4
|
|
|
* |
5
|
|
|
* (c) Matthijs van den Bos <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace VDB\Spider\Tests\EventListener; |
12
|
|
|
|
13
|
|
|
use VDB\Spider\Tests\TestCase; |
14
|
|
|
use VDB\Spider\EventListener\PolitenessPolicyListener; |
15
|
|
|
use VDB\Spider\Event\SpiderEvents; |
16
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
17
|
|
|
use VDB\Uri\Uri; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class PolitenessPolicyListenerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @covers VDB\Spider\EventListener\PolitenessPolicyListener |
26
|
|
|
*/ |
27
|
|
|
public function testOnCrawlPreRequestSameDomain() |
28
|
|
|
{ |
29
|
|
|
$politenessPolicyListener = new PolitenessPolicyListener(500); |
30
|
|
|
|
31
|
|
|
$uri = new Uri('http://php-spider.org/', 'http://php-spider.org/'); |
32
|
|
|
$event = new GenericEvent(SpiderEvents::SPIDER_CRAWL_PRE_REQUEST, array('uri' => $uri)); |
33
|
|
|
|
34
|
|
|
$politenessPolicyListener->onCrawlPreRequest($event); |
35
|
|
|
|
36
|
|
|
$start = microtime(true); |
37
|
|
|
$politenessPolicyListener->onCrawlPreRequest($event); |
38
|
|
|
$interval = microtime(true) - $start; |
39
|
|
|
|
40
|
|
|
$this->assertGreaterThanOrEqual(0.5, $interval, 'Actual delay'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers VDB\Spider\EventListener\PolitenessPolicyListener |
45
|
|
|
*/ |
46
|
|
|
public function testOnCrawlPreRequestDifferentDomain() |
47
|
|
|
{ |
48
|
|
|
$politenessPolicyListener = new PolitenessPolicyListener(500); |
49
|
|
|
|
50
|
|
|
$uri = new Uri('http://php-spider.org/', 'http://php-spider.org/'); |
51
|
|
|
$event = new GenericEvent(SpiderEvents::SPIDER_CRAWL_PRE_REQUEST, array('uri' => $uri)); |
52
|
|
|
|
53
|
|
|
$uri2 = new Uri('http://example.com/', 'http://example.com/'); |
54
|
|
|
$event2 = new GenericEvent(SpiderEvents::SPIDER_CRAWL_PRE_REQUEST, array('uri' => $uri2)); |
55
|
|
|
|
56
|
|
|
$politenessPolicyListener->onCrawlPreRequest($event); |
57
|
|
|
|
58
|
|
|
$start = microtime(true); |
59
|
|
|
$politenessPolicyListener->onCrawlPreRequest($event2); |
60
|
|
|
$interval = microtime(true) - $start; |
61
|
|
|
|
62
|
|
|
$this->assertLessThan(0.5, $interval, 'Actual delay'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|