File: sortTestFixtureFiles.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 (132 lines) | stat: -rw-r--r-- 4,326 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
<?php

declare(strict_types=1);

use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\AbstractParser;
use DeviceDetector\Parser\Device\AbstractDeviceParser;

include __DIR__ . '/../vendor/autoload.php';

AbstractParser::setVersionTruncation(AbstractParser::VERSION_TRUNCATION_NONE);

$fixtureFiles = glob(__DIR__ . '/../Tests/fixtures/*.yml');

$overwrite = !empty($argv[1]) && '--f' === $argv[1];
$data      = [];

foreach ($fixtureFiles as $file) {
    if ('unknown' !== basename($file, '.yml') && !array_key_exists(preg_replace('/-\d+$/', '', str_replace('_', ' ', basename($file, '.yml'))), AbstractDeviceParser::getAvailableDeviceTypes())) {
        continue;
    }

    $fileFixtures = Spyc::YAMLLoad(file_get_contents($file));

    foreach ($fileFixtures as $fixture) {
        if ($overwrite) {
            $fixture = DeviceDetector::getInfoFromUserAgent($fixture['user_agent']);
        }

        $data[$fixture['device']['type']][] = $fixture;
    }
}

foreach ($data as $deviceType => $fixtures) {
    $fixtures = array_values(array_map('unserialize', array_unique(array_map('serialize', $fixtures))));

    usort($fixtures, static function ($a, $b) {
        if (empty($b)) {
            return -1;
        }

        if (@$a['device']['brand'] === @$b['device']['brand']) {
            if ($a['device']['model'] === $b['device']['model']) {
                if (@$a['os']['name'] === @$b['os']['name']) {
                    if (@$a['os']['version'] === @$b['os']['version']) {
                        if (@$a['client']['name'] === @$b['client']['name']) {
                            if (@$a['client']['version'] === @$b['client']['version']) {
                                if ($a['user_agent'] === $b['user_agent']) {
                                    return 0;
                                }

                                return strtolower($a['user_agent']) < strtolower($b['user_agent']) ? -1 : 1;
                            }

                            return ($a['client']['version'] ?? '') < ($b['client']['version'] ?? '') ? -1 : 1;
                        }

                        return ($a['client']['name'] ?? '') < ($b['client']['name'] ?? '') ? -1 : 1;
                    }

                    return ($a['os']['version'] ?? '') < ($b['os']['version'] ?? '') ? -1 : 1;
                }

                return ($a['os']['name'] ?? '') < ($b['os']['name'] ?? '') ? -1 : 1;
            }

            return ($a['device']['model'] ?? '') < ($b['device']['model'] ?? '') ? -1 : 1;
        }

        return ($a['device']['brand'] ?? '') < ($b['device']['brand'] ?? '') ? -1 : 1;
    });

    $chunks = array_chunk($fixtures, 500);

    foreach ($chunks as $nr => $chunk) {
        $content = Spyc::YAMLDump($chunk, false, 0);

        $content = str_replace(": ON\n", ": 'ON'\n", $content);
        $content = str_replace(": NO\n", ": 'NO'\n", $content);

        if (empty($deviceType)) {
            $deviceType = 'unknown';
        }

        if ($nr > 0) {
            file_put_contents(
                sprintf(
                    __DIR__ . '/../Tests/fixtures/%s-%s.yml',
                    str_replace(' ', '_', $deviceType),
                    $nr
                ),
                $content
            );
        } else {
            file_put_contents(sprintf(__DIR__ . '/../Tests/fixtures/%s.yml', str_replace(' ', '_', $deviceType)), $content);
        }
    }
}

shell_exec("sed -i -e 's/version: \\([^\"].*\\)/version: \"\\1\"/g' " . __DIR__ . '/../Tests/fixtures/*.yml');

$botFixtures = Spyc::YAMLLoad(file_get_contents(__DIR__ . '/../Tests/fixtures/bots.yml'));

foreach ($botFixtures as &$fixture) {
    if (!$overwrite) {
        continue;
    }

    $fixture = DeviceDetector::getInfoFromUserAgent($fixture['user_agent']);
}

unset($fixture);

usort($botFixtures, static function ($a, $b) {
    if (empty($b)) {
        return -1;
    }

    if (@$a['bot']['name'] === @$b['bot']['name']) {
        if ($a['user_agent'] === $b['user_agent']) {
            return 0;
        }

        return strtolower($a['user_agent']) < strtolower($b['user_agent']) ? -1 : 1;
    }

    return @$a['bot']['name'] < @$b['bot']['name'] ? -1 : 1;
});

file_put_contents(__DIR__ . '/../Tests/fixtures/bots.yml', Spyc::YAMLDump($botFixtures, false, 0));

echo "done.\n";