|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Dziki\MonologSentryBundle\Tests\Unit\UserAgent; |
|
6
|
|
|
|
|
7
|
|
|
use Dziki\MonologSentryBundle\UserAgent\Parser; |
|
8
|
|
|
use Dziki\MonologSentryBundle\UserAgent\UserAgent; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractParserTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Parser */ |
|
14
|
|
|
private $parser; |
|
15
|
|
|
|
|
16
|
|
|
public function validUserAgentsDataProvider(): array |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
|
|
[ |
|
20
|
|
|
'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0', |
|
21
|
|
|
UserAgent::create('Firefox', '62.0', 'Linux'), |
|
22
|
|
|
], |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function invalidUserAgentsDataProvider(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
[''], |
|
30
|
|
|
['Googlebot/2.1 (+http://www.google.com/bot.html)'], |
|
31
|
|
|
['AppleTV5,3/9.1.1'], |
|
32
|
|
|
['Googlebot'], |
|
33
|
|
|
['Google (+https://developers.google.com/+/web/snippet/)'], |
|
34
|
|
|
['Bingbot'], |
|
35
|
|
|
['Mozilla/5.0 (compatible; Bingbot/2.0; +http://www.bing.com/bingbot.htm)'], |
|
36
|
|
|
['Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'], |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function setParser(Parser $parser): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->parser = $parser; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
* @dataProvider validUserAgentsDataProvider |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $userAgent |
|
50
|
|
|
* @param UserAgent $expectedUserAgent |
|
51
|
|
|
*/ |
|
52
|
|
|
public function parseValidUserAgent(string $userAgent, UserAgent $expectedUserAgent): void |
|
53
|
|
|
{ |
|
54
|
|
|
$resultedUserAgent = $this->parser->parse($userAgent); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals($expectedUserAgent, $resultedUserAgent); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
* @dataProvider invalidUserAgentsDataProvider |
|
62
|
|
|
* @doesNotPerformAssertions |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $userAgent |
|
65
|
|
|
*/ |
|
66
|
|
|
public function doNotBreakOnInvalidUserAgents(string $userAgent): void |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$this->parser->parse($userAgent); |
|
70
|
|
|
} catch (\Throwable $exception) { |
|
71
|
|
|
$this->fail(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|