Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak committed May 2, 2024
2 parents 8548095 + 95af827 commit 7b1448b
Show file tree
Hide file tree
Showing 14 changed files with 1,888 additions and 94 deletions.
5 changes: 5 additions & 0 deletions Parser/Device/AbstractDeviceParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ abstract class AbstractDeviceParser extends AbstractParser
'C3' => 'China Mobile',
'U9' => 'China Telecom',
'CI' => 'Chico Mobile',
'CIA' => 'C Idea',
'CIP' => 'CipherLab',
'CIT' => 'Citycall',
'1C' => 'Chuwi',
Expand Down Expand Up @@ -379,13 +380,15 @@ abstract class AbstractDeviceParser extends AbstractParser
'CTR' => 'Ctroniq',
'CU' => 'Cube',
'CB' => 'CUBOT',
'CUI' => 'Cuiud',
'CV' => 'CVTE',
'CWO' => 'Cwowdefu',
'C4' => 'Cyrus',
'D5' => 'Daewoo',
'DA' => 'Danew',
'DAN' => 'Dany',
'DA1' => 'DangcapHD',
'DAR' => 'Daria',
'DT' => 'Datang',
'D7' => 'Datawind',
'7D' => 'Datamini',
Expand Down Expand Up @@ -701,6 +704,7 @@ abstract class AbstractDeviceParser extends AbstractParser
'HL' => 'Hi-Level',
'3H' => 'Hi',
'HIB' => 'Hiberg',
'HBY' => 'HiBy',
'HIH' => 'HiHi',
'HIK' => 'HiKing',
'H2' => 'Highscreen',
Expand Down Expand Up @@ -1034,6 +1038,7 @@ abstract class AbstractDeviceParser extends AbstractParser
'MEO' => 'MEO',
'MX' => 'MEU',
'MI' => 'MicroMax',
'MIP' => 'mipo',
'MS' => 'Microsoft',
'6Q' => 'Microtech',
'1X' => 'Minix',
Expand Down
12 changes: 6 additions & 6 deletions README.md

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions Tests/DeviceDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,95 @@ public function getFixtures(): array
return $fixtures;
}

/**
* @dataProvider getFixturesClient
*/
public function testParseClient(array $fixtureData): void
{
$ua = $fixtureData['user_agent'];
$clientHints = !empty($fixtureData['headers']) ? ClientHints::factory($fixtureData['headers']) : null;

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);

try {
$uaInfo = DeviceDetector::getInfoFromUserAgent($ua, $clientHints);
} catch (\Exception $exception) {
throw new \Exception(
\sprintf('Error: %s from useragent %s', $exception->getMessage(), $ua),
$exception->getCode(),
$exception
);
}

$messageError = \sprintf(
"Fixture:\n%s\nResult:\n%s",
\Spyc::YAMLDump($fixtureData, 2, 0),
\Spyc::YAMLDump($uaInfo, 2, 0)
);

unset($fixtureData['headers']); // ignore headers in result
unset($fixtureData['client']['family']);

$this->assertArrayNotHasKey('bot', $uaInfo, $messageError);
$this->assertEquals($fixtureData['client'], $uaInfo['client'], $messageError);
}

public function getFixturesClient(): array
{
$fixtures = [];
$fixtureFiles = \glob(\realpath(__DIR__) . '/Parser/Client/fixtures/*.yml');

foreach ($fixtureFiles as $fixturesPath) {
$typeFixtures = \Spyc::YAMLLoad($fixturesPath);

$fixtures = \array_merge(\array_map(static function ($elem) {
return [$elem];
}, $typeFixtures), $fixtures);
}

return $fixtures;
}

/**
* @dataProvider getFixturesDevice
*/
public function testParseDevice(array $fixtureData): void
{
$ua = $fixtureData['user_agent'];
$clientHints = !empty($fixtureData['headers']) ? ClientHints::factory($fixtureData['headers']) : null;

AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);

try {
$uaInfo = DeviceDetector::getInfoFromUserAgent($ua, $clientHints);
} catch (\Exception $exception) {
throw new \Exception(
\sprintf('Error: %s from useragent %s', $exception->getMessage(), $ua),
$exception->getCode(),
$exception
);
}

$this->assertArrayNotHasKey('bot', $uaInfo);
$this->assertEquals($fixtureData['device'], $uaInfo['device']);
}

public function getFixturesDevice(): array
{
$fixtures = [];
$fixtureFiles = \glob(\realpath(__DIR__) . '/Parser/Device/fixtures/*.yml');

foreach ($fixtureFiles as $fixturesPath) {
$typeFixtures = \Spyc::YAMLLoad($fixturesPath);

$fixtures = \array_merge(\array_map(static function ($elem) {
return [$elem];
}, $typeFixtures), $fixtures);
}

return $fixtures;
}

public function testInstanceReusage(): void
{
$userAgents = [
Expand Down
18 changes: 18 additions & 0 deletions Tests/fixtures/phablet-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4354,3 +4354,21 @@
model: Galaxy Note 20 Ultra
os_family: Android
browser_family: Opera
-
user_agent: Mozilla/5.0 (Linux; arm_64; Android 12; SM-F907B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.324 YaApp_Android/24.19/apad YaSearchBrowser/24.19/apad BroPP/1.0 SA/3 Mobile Safari/537.36
os:
name: Android
version: "12"
platform: ARM
client:
type: browser
name: Yandex Browser
version: "24.19"
engine: Blink
engine_version: 120.0.6099.324
device:
type: phablet
brand: Samsung
model: Galaxy Fold
os_family: Android
browser_family: Unknown
18 changes: 18 additions & 0 deletions Tests/fixtures/portable_media_player.yml
Original file line number Diff line number Diff line change
Expand Up @@ -915,3 +915,21 @@
model: iPod Touch 7
os_family: iOS
browser_family: Safari
-
user_agent: Mozilla/5.0 (Linux; Android 12; R6III Build/SKQ1.211006.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
os:
name: Android
version: "12"
platform: ""
client:
type: browser
name: Chrome Webview
version: 91.0.4472.114
engine: Blink
engine_version: 91.0.4472.114
device:
type: portable media player
brand: HiBy
model: R6 Gen III
os_family: Android
browser_family: Chrome
54 changes: 54 additions & 0 deletions Tests/fixtures/smartphone-36.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7257,6 +7257,24 @@
model: Galaxy S23
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 14; SM-A346B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
os:
name: Android
version: "14"
platform: ""
client:
type: browser
name: Chrome Mobile
version: 123.0.0.0
engine: Blink
engine_version: 123.0.0.0
device:
type: smartphone
brand: Samsung
model: Galaxy A34 5G
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 13; SAMSUNG SM-A346B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/21.0 Chrome/110.0.5481.154 Mobile Safari/537.36
os:
Expand Down Expand Up @@ -9975,3 +9993,39 @@
model: Joe
os_family: Android
browser_family: Chrome
-
user_agent: Mozilla/5.0 (Linux; Android 13; BL8000) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.149 Mobile Safari/537.36 OPR/81.3.4292.78688
os:
name: Android
version: "13"
platform: ""
client:
type: browser
name: Opera Mobile
version: 81.3.4292.78688
engine: Blink
engine_version: 122.0.6261.149
device:
type: smartphone
brand: Blackview
model: BL8000
os_family: Android
browser_family: Opera
-
user_agent: Mozilla/5.0 (Linux; Android 12; ZTE Blade A53 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
os:
name: Android
version: "12"
platform: ""
client:
type: browser
name: Chrome Mobile
version: 123.0.0.0
engine: Blink
engine_version: 123.0.0.0
device:
type: smartphone
brand: ZTE
model: Blade A53 Pro
os_family: Android
browser_family: Chrome
18 changes: 0 additions & 18 deletions Tests/fixtures/smartphone-37.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4523,24 +4523,6 @@
model: M5A
os_family: Android
browser_family: Unknown
-
user_agent: Mozilla/5.0 (Linux; arm_64; Android 13; Active 8 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.232 YaBrowser/23.11.0.232.01 Safari/537.36
os:
name: Android
version: "13"
platform: ARM
client:
type: browser
name: Yandex Browser
version: 23.11.0.232.01
engine: Blink
engine_version: 118.0.5993.232
device:
type: smartphone
brand: Blackview
model: Active 8 Pro
os_family: Android
browser_family: Unknown
-
user_agent: Mozilla/5.0 (Linux; Android 10; Blade L210) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36 OPR/77.0.4054.90
os:
Expand Down
Loading

0 comments on commit 7b1448b

Please sign in to comment.