1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class UserAgentTest extends PHPUnit_Framework_TestCase |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected $CrawlerDetect; |
6
|
|
|
|
7
|
|
|
public function setUp() |
8
|
|
|
{ |
9
|
|
|
$this->CrawlerDetect = new Jaybizzle\CrawlerDetect\CrawlerDetect(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
View Code Duplication |
public function testBots() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
15
|
|
|
|
16
|
|
|
foreach ($lines as $line) { |
17
|
|
|
$test = $this->CrawlerDetect->isCrawler($line); |
18
|
|
|
$this->assertEquals($test, true, $line); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testDevices() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
25
|
|
|
|
26
|
|
|
foreach ($lines as $line) { |
27
|
|
|
$test = $this->CrawlerDetect->isCrawler($line); |
28
|
|
|
$this->assertEquals($test, false, $line); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testReturnsCorrectMatchedBotName() |
33
|
|
|
{ |
34
|
|
|
$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)'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$matches = $this->CrawlerDetect->getMatches(); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($this->CrawlerDetect->getMatches(), 'Yahoo Ad monitoring', $matches); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testForRegexCollision() |
42
|
|
|
{ |
43
|
|
|
$crawlers = $this->CrawlerDetect->getCrawlers(); |
44
|
|
|
|
45
|
|
|
foreach ($crawlers as $regex) { |
46
|
|
|
foreach ($crawlers as $compare) { |
47
|
|
|
// Dont check this regex against itself |
48
|
|
|
if ($regex != $compare) { |
49
|
|
|
preg_match('/'.$regex.'/i', stripslashes($compare), $matches); |
50
|
|
|
|
51
|
|
|
$this->assertEmpty($matches, $regex.' collided with '.$compare); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.