Skip to content

Commit

Permalink
feat: Add tests for ClientHints FormFactors (#7845)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak authored Sep 23, 2024
1 parent 4df1139 commit 05f27a2
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Tests/DeviceDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,89 @@ public function getFixturesDevice(): array
return $fixtures;
}

public function getFixturesDeviceTypeFromClientHints(): array
{
$useragent = 'Some Unknown UA';
$deviceName = '"Some Unknown Model"';

return [
[
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"EInk", "Watch"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_WEARABLE,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"EInk"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_TABLET,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"Desktop", "Mobile"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"Unknown Type", "Mobile"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"Tablet", "Mobile"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"EInk", "Tablet"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_TABLET,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"Tablet", "Automotive"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_CAR_BROWSER,
], [
'useragent' => $useragent,
'headers' => [
'sec-ch-ua-form-factors' => '"EInk", "Xr"',
'sec-ch-ua-model' => $deviceName,
],
'device' => AbstractDeviceParser::DEVICE_TYPE_WEARABLE,
],
];
}

/**
* @dataProvider getFixturesDeviceTypeFromClientHints
*/
public function testDetectDeviceTypeFromClientHints(string $useragent, array $headers, int $device): void
{
$clientHints = ClientHints::factory($headers);
$deviceDetector = new DeviceDetector();
$deviceDetector->setClientHints($clientHints);
$deviceDetector->setUserAgent($useragent);
$deviceDetector->parse();

$this->assertEquals($device, $deviceDetector->getDevice());
$this->assertEquals('Some Unknown Model', $deviceDetector->getModel());
$this->assertEquals('', $deviceDetector->getBrandName());
$this->assertEquals(AbstractDeviceParser::getDeviceName($device), $deviceDetector->getDeviceName());
}

public function testInstanceReusage(): void
{
$userAgents = [
Expand Down

0 comments on commit 05f27a2

Please sign in to comment.