diff --git a/CachedEntry.php b/CachedEntry.php index ef5c2345..5291e71a 100644 --- a/CachedEntry.php +++ b/CachedEntry.php @@ -28,12 +28,12 @@ public function __construct(string $userAgent, $clientHints, array $values) $clientHints = $clientHints ? ClientHints::factory($clientHints) : null; parent::__construct($userAgent, $clientHints); - $this->bot = $values['bot'] ?? ''; + $this->bot = $values['bot'] ?? null; $this->brand = $values['brand'] ?? ''; - $this->client = $values['client'] ?? ''; - $this->device = $values['device'] ?? ''; + $this->client = $values['client'] ?? null; + $this->device = $values['device'] ?? null; $this->model = $values['model'] ?? ''; - $this->os = $values['os'] ?? ''; + $this->os = $values['os'] ?? null; // Or cached entries only use the useragents, so if we have some client hints provided, // We use some special parsers, which use the cached user agent result and parses it again using client hints diff --git a/tests/Integration/CachedEntryTest.php b/tests/Integration/CachedEntryTest.php index 35fc358d..4e4c987e 100644 --- a/tests/Integration/CachedEntryTest.php +++ b/tests/Integration/CachedEntryTest.php @@ -115,7 +115,7 @@ public function testConstruct() $instance = new CachedEntry($userAgent, $clientHints, $values); $this->assertIsObject($instance); $this->assertInstanceOf(CachedEntry::class, $instance); - $this->assertSame('', $instance->getBot()); + $this->assertSame(null, $instance->getBot()); $this->assertSame($values['brand'], $instance->getBrandName()); $this->assertSame($expectedClient, $instance->getClient()); $this->assertSame($values['device'], $instance->getDevice()); @@ -123,6 +123,41 @@ public function testConstruct() $this->assertSame($expectedOs, $instance->getOs()); } + public function testConstructDefault() + { + $userAgent = "unknown"; + + $clientHints = [ + 'HTTP_SEC_CH_UA_PLATFORM' => '"Windows"', + 'HTTP_SEC_CH_UA' => '" Not A;Brand";v="99", "Chromium";v="95", "Microsoft Edge";v="95"', + 'HTTP_SEC_CH_UA_MOBILE' => "?0", + 'HTTP_SEC_CH_UA_FULL_VERSION' => '"98.0.0.1"', + 'HTTP_SEC_CH_UA_PLATFORM_VERSION' => '"14.0.0"', + 'HTTP_SEC_CH_UA_ARCH' => "x86", + 'HTTP_SEC_CH_UA_BITNESS' => "64", + 'HTTP_SEC_CH_UA_MODEL' => "" + ]; + + $expectedOs = [ + 'name' => 'Windows', + 'short_name' => 'WIN', + 'version' => '11', + 'platform' => 'x64', + 'family' => 'Windows', + ]; + + + $instance = new CachedEntry($userAgent, $clientHints, []); + $this->assertIsObject($instance); + $this->assertInstanceOf(CachedEntry::class, $instance); + $this->assertSame(null, $instance->getBot()); + $this->assertSame('', $instance->getBrandName()); + $this->assertSame(null, $instance->getClient()); + $this->assertSame(null, $instance->getDevice()); + $this->assertSame('', $instance->getModel()); + $this->assertSame($expectedOs, $instance->getOs()); + } + public function testGetNumCacheFiles_noneCached() { $this->assertEquals(0, CachedEntry::getNumEntriesInCacheDir());