Completed
Push — master ( a28997...0d991d )
by Mark
11s
created

tests/UATests.php (1 issue)

PSR1 classes have a namespace declaration.

Coding Style Compatibility Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of Crawler Detect - the web crawler detection library.
5
 *
6
 * (c) Mark Beech <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
use Jaybizzle\CrawlerDetect\CrawlerDetect;
13
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
14
15
class UserAgentTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
    protected $CrawlerDetect;
18
19
    public function setUp()
20
    {
21
        $this->CrawlerDetect = new CrawlerDetect();
22
    }
23
24
    /** @test */
25 View Code Duplication
    public function user_agents_are_bots()
26
    {
27
        $lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
28
29
        foreach ($lines as $line) {
30
            $test = $this->CrawlerDetect->isCrawler($line);
31
            $this->assertEquals($test, true, $line);
32
        }
33
    }
34
35
    /** @test */
36 View Code Duplication
    public function user_agents_are_devices()
37
    {
38
        $lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
39
40
        foreach ($lines as $line) {
41
            $test = $this->CrawlerDetect->isCrawler($line);
42
            $this->assertEquals($test, false, $line);
43
        }
44
    }
45
46
    /** @test */
47 View Code Duplication
    public function it_returns_correct_matched_bot_name()
48
    {
49
        $test = $this->CrawlerDetect->isCrawler('Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit (KHTML, like Gecko) Mobile (compatible; Yahoo Ad monitoring; https://help.yahoo.com/kb/yahoo-ad-monitoring-SLN24857.html)');
50
51
        $matches = $this->CrawlerDetect->getMatches();
52
53
        $this->assertEquals($this->CrawlerDetect->getMatches(), 'Yahoo Ad monitoring', $matches);
54
    }
55
56
    /** @test */
57 View Code Duplication
    public function it_returns_null_when_no_bot_detected()
58
    {
59
        $test = $this->CrawlerDetect->isCrawler('nothing to see here');
60
61
        $matches = $this->CrawlerDetect->getMatches();
62
63
        $this->assertEquals($this->CrawlerDetect->getMatches(), null, $matches);
64
    }
65
66
    /** @test */
67
    public function empty_user_agent()
68
    {
69
        $test = $this->CrawlerDetect->isCrawler('      ');
70
71
        $this->assertEquals($test, false);
72
    }
73
74
    /** @test */
75 View Code Duplication
    public function current_visitor()
76
    {
77
        $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"bingbot(at)microsoft.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}');
78
79
        $cd = new CrawlerDetect($headers);
80
81
        $this->assertEquals($cd->isCrawler(), true);
82
    }
83
84
    /** @test */
85
    public function user_agent_passed_via_contructor()
86
    {
87
        $cd = new CrawlerDetect(null, 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit (KHTML, like Gecko) Mobile (compatible; Yahoo Ad monitoring; https://help.yahoo.com/kb/yahoo-ad-monitoring-SLN24857.html)');
88
89
        $this->assertEquals($cd->isCrawler(), true);
90
    }
91
92
    /** @test */
93 View Code Duplication
    public function http_from_header()
94
    {
95
        $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"googlebot(at)googlebot.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/28.0.1500.71 Safari\/537.36","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}');
96
97
        $cd = new CrawlerDetect($headers);
98
99
        $this->assertEquals($cd->isCrawler(), true);
100
    }
101
102
    /** @test */
103
    public function the_regex_patterns_are_unique()
104
    {
105
        $crawlers = new Crawlers();
106
107
        $this->assertEquals(count($crawlers->getAll()), count(array_unique($crawlers->getAll())));
108
    }
109
110
    /** @test */
111
    public function there_are_no_regex_collisions()
112
    {
113
        $crawlers = new Crawlers();
114
115
        foreach ($crawlers->getAll() as $key1 => $regex) {
116
            foreach ($crawlers->getAll() as $key2 => $compare) {
117
                // Dont check this regex against itself
118
                if ($key1 != $key2) {
119
                    preg_match('/'.$regex.'/i', stripslashes($compare), $matches);
120
121
                    $this->assertEmpty($matches, $regex.' collided with '.$compare);
122
                }
123
            }
124
        }
125
    }
126
}
127