/
UATests.php
75 lines (59 loc) 路 2.2 KB
/
UATests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*
* This file is part of Crawler Detect - the web crawler detection library.
*
* (c) Mark Beech <m@rkbee.ch>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
class UserAgentTest extends PHPUnit_Framework_TestCase
{
protected $CrawlerDetect;
public function setUp()
{
$this->CrawlerDetect = new CrawlerDetect();
}
public function testBots()
{
$lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$test = $this->CrawlerDetect->isCrawler($line);
$this->assertEquals($test, true, $line);
}
}
public function testDevices()
{
$lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$test = $this->CrawlerDetect->isCrawler($line);
$this->assertEquals($test, false, $line);
}
}
public function testReturnsCorrectMatchedBotName()
{
$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)');
$matches = $this->CrawlerDetect->getMatches();
$this->assertEquals($this->CrawlerDetect->getMatches(), 'Yahoo Ad monitoring', $matches);
}
public function testEmptyUserAgent()
{
$test = $this->CrawlerDetect->isCrawler(' ');
$this->assertEquals($test, false);
}
public function testForRegexCollision()
{
$crawlers = new Crawlers();
foreach ($crawlers->getAll() as $key1 => $regex) {
foreach ($crawlers->getAll() as $key2 => $compare) {
// Dont check this regex against itself
if ($key1 != $key2) {
preg_match('/'.$regex.'/i', stripslashes($compare), $matches);
$this->assertEmpty($matches, $regex.' collided with '.$compare);
}
}
}
}
}