testOnCrawlPreRequestSameDomain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
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