File: BrowserTest.php

package info (click to toggle)
matomo-device-detector 6.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,684 kB
  • sloc: php: 7,758; xml: 79; makefile: 15; sh: 3
file content (191 lines) | stat: -rw-r--r-- 6,056 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php

/**
 * Device Detector - The Universal Device Detection library for parsing User Agents
 *
 * @link https://matomo.org
 *
 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
 */

declare(strict_types=1);

namespace DeviceDetector\Tests\Parser\Client;

use DeviceDetector\ClientHints;
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\Client\Browser\Engine;
use DeviceDetector\Parser\Client\Hints\BrowserHints;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Spyc;

class BrowserTest extends TestCase
{
    protected static $browsersTested = [];

    /**
     * @dataProvider getFixtures
     */
    #[DataProvider('getFixtures')]
    public function testParse(string $useragent, array $client, ?array $headers = null): void
    {
        $browserParser = new Browser();
        $browserParser::setVersionTruncation(Browser::VERSION_TRUNCATION_NONE);
        $browserParser->setUserAgent($useragent);

        if (null !== $headers) {
            $browserParser->setClientHints(ClientHints::factory($headers));
        }

        $browser = $browserParser->parse();
        unset($browser['short_name']);

        $this->assertEquals($client, $browser, "UserAgent: {$useragent}");
        $this->assertTrue($this->checkBrowserEngine($browser['engine']), \sprintf(
            "UserAgent: %s\nEngine wrong name: `%s`",
            $useragent,
            $browser['engine']
        ));

        self::$browsersTested[] = $client['name'];
    }

    public static function getFixtures(): array
    {
        $fixtureData = Spyc::YAMLLoad(\realpath(__DIR__) . '/fixtures/browser.yml');

        $fixtureData = \array_map(static function (array $item): array {
            return ['useragent' => $item['user_agent'], 'client' => $item['client'], 'headers' => $item['headers'] ?? null];
        }, $fixtureData);

        return $fixtureData;
    }

    public function testGetAvailableBrowserFamilies(): void
    {
        $this->assertGreaterThan(5, Browser::getAvailableBrowserFamilies());
    }

    public function testAllBrowsersTested(): void
    {
        $allBrowsers       = \array_values(Browser::getAvailableBrowsers());
        $browsersNotTested = \array_diff($allBrowsers, self::$browsersTested);
        $this->assertEmpty($browsersNotTested, 'This browsers are not tested: ' . \implode(', ', $browsersNotTested));
    }

    public function testGetAvailableClients(): void
    {
        $available = Browser::getAvailableClients();
        $this->assertGreaterThanOrEqual(\count($available), \count(Browser::getAvailableBrowsers()));
    }

    public function testStructureBrowsersYml(): void
    {
        $ymlDataItems = Spyc::YAMLLoad(__DIR__ . '/../../../regexes/client/browsers.yml');

        foreach ($ymlDataItems as $item) {
            $this->assertTrue(\array_key_exists('regex', $item), 'key "regex" not exist');
            $this->assertTrue(\array_key_exists('name', $item), 'key "name" not exist');
            $this->assertTrue(\array_key_exists('version', $item), 'key "version" not exist');
            $this->assertIsString($item['regex']);
            $this->assertIsString($item['name']);
            $this->assertIsString($item['version']);
        }
    }

    public function testBrowserFamiliesNoDuplicates(): void
    {
        foreach (Browser::getAvailableBrowserFamilies() as $browser => $families) {
            foreach (\array_count_values($families) as $shortcode => $count) {
                $this->assertEquals(
                    $count,
                    1,
                    "Family {$browser}: contains duplicate of shortcode {$shortcode}"
                );
            }
        }
    }

    public function testShortCodesComparisonWithBrowsers(): void
    {
        $reflectionClass = new \ReflectionClass(Browser::class);
        $browserProperty = $reflectionClass->getProperty('availableBrowsers');

        if (PHP_VERSION_ID < 80500) {
            $browserProperty->setAccessible(true);
        }

        $availableBrowsers = $browserProperty->getValue();

        $browserFamilyProperty = $reflectionClass->getProperty('browserFamilies');

        if (PHP_VERSION_ID < 80500) {
            $browserFamilyProperty->setAccessible(true);
        }

        $browserFamilies = $browserFamilyProperty->getValue();
        $result          = [];

        foreach ($browserFamilies as $codes) {
            foreach ($codes as $code) {
                if (isset($availableBrowsers[$code])) {
                    continue;
                }

                $result[] = $code;
            }
        }

        $this->assertEquals([], $result, 'These shortcode does not match the list of browsers');
    }

    /**
     * @return array
     * @throws \ReflectionException
     */
    public static function getFixturesBrowserHints(): array
    {
        $method = new \ReflectionMethod(BrowserHints::class, 'getRegexes');

        if (PHP_VERSION_ID < 80500) {
            $method->setAccessible(true);
        }

        $hints    = $method->invoke(new BrowserHints());
        $fixtures = [];

        foreach ($hints as $name) {
            $fixtures[] = \compact('name');
        }

        return $fixtures;
    }

    /**
     * @dataProvider getFixturesBrowserHints
     */
    #[DataProvider('getFixturesBrowserHints')]
    public function testBrowserHintsForAvailableBrowsers(string $name): void
    {
        $browserShort = Browser::getBrowserShortName($name);
        $this->assertNotEquals(
            null,
            $browserShort,
            \sprintf('Browser name "%s" was not found in $availableBrowsers.', $name)
        );
    }

    protected function checkBrowserEngine(string $engine): bool
    {
        if ('' === $engine) {
            return true;
        }

        $engines         = Engine::getAvailableEngines();
        $enginePos       = \array_search($engine, $engines, false);
        $engineReference = $engines[$enginePos] ?? null;

        return $engineReference === $engine;
    }
}