File: OperatingSystemTest.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 (121 lines) | stat: -rw-r--r-- 3,561 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
<?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;

use DeviceDetector\ClientHints;
use DeviceDetector\Parser\OperatingSystem;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Spyc;

class OperatingSystemTest extends TestCase
{
    protected static $osTested = [];

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

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

        $this->assertEquals($os, $osParser->parse(), "UserAgent: {$useragent}");
        self::$osTested[] = $os['name'];
    }

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

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

        return $fixtureData;
    }

    /**
     * @dataProvider getAllOs
     */
    #[DataProvider('getAllOs')]
    public function testOSInGroup(string $os): void
    {
        $familyOs = \call_user_func_array('array_merge', \array_values(OperatingSystem::getAvailableOperatingSystemFamilies()));
        $this->assertContains($os, $familyOs);
    }

    public static function getAllOs(): array
    {
        $allOs = \array_keys(OperatingSystem::getAvailableOperatingSystems());

        return \array_map(static function ($os) {
            return [$os];
        }, $allOs);
    }

    /**
     * @dataProvider getAllFamilyOs
     */
    #[DataProvider('getAllFamilyOs')]
    public function testFamilyOSExists(string $os): void
    {
        $allOs = \array_keys(OperatingSystem::getAvailableOperatingSystems());
        $this->assertContains($os, $allOs);
    }

    public static function getAllFamilyOs(): array
    {
        $allFamilyOs = \call_user_func_array('array_merge', \array_values(OperatingSystem::getAvailableOperatingSystemFamilies()));

        return \array_map(static function ($os) {
            return [$os];
        }, $allFamilyOs);
    }

    public function testGetAvailableOperatingSystems(): void
    {
        $this->assertGreaterThan(70, OperatingSystem::getAvailableOperatingSystems());
    }

    /**
     * @dataProvider getNameFromIds
     */
    #[DataProvider('getNameFromIds')]
    public function testGetNameFromId(string $os, string $version, ?string $expected): void
    {
        $this->assertEquals($expected, OperatingSystem::getNameFromId($os, $version));
    }

    public static function getNameFromIds(): array
    {
        return [
            ['DEB', '4.5', 'Debian 4.5'],
            ['WRT', '', 'Windows RT'],
            ['WIN', '98', 'Windows 98'],
            ['XXX', '4.5', null],
        ];
    }

    public function testAllOperatingSystemsTested(): void
    {
        $allBrowsers = OperatingSystem::getAvailableOperatingSystems();
        $osNotTested = \array_diff($allBrowsers, self::$osTested);
        $this->assertEmpty($osNotTested, 'Following oss are not tested: ' . \implode(', ', $osNotTested));
    }
}