AbstractParserTest::parseValidUserAgent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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