From e7ab7c5aa1c321b4bf8a86d91fd7687c4b34ba4c Mon Sep 17 00:00:00 2001 From: Jacob Ransom Date: Tue, 3 Oct 2023 11:10:25 +1300 Subject: [PATCH] Fix errors from undefined indexes --- CachedEntry.php | 14 ++--- tests/Integration/CachedEntryTest.php | 89 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/CachedEntry.php b/CachedEntry.php index 1784c089..ef5c2345 100644 --- a/CachedEntry.php +++ b/CachedEntry.php @@ -28,16 +28,16 @@ public function __construct(string $userAgent, $clientHints, array $values) $clientHints = $clientHints ? ClientHints::factory($clientHints) : null; parent::__construct($userAgent, $clientHints); - $this->bot = $values['bot']; - $this->brand = $values['brand']; - $this->client = $values['client']; - $this->device = $values['device']; - $this->model = $values['model']; - $this->os = $values['os']; + $this->bot = $values['bot'] ?? ''; + $this->brand = $values['brand'] ?? ''; + $this->client = $values['client'] ?? ''; + $this->device = $values['device'] ?? ''; + $this->model = $values['model'] ?? ''; + $this->os = $values['os'] ?? ''; // 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 - if (!empty($clientHints) && $values['client']['type'] === 'browser') { + if (!empty($clientHints) && !empty($values['client']['type']) && $values['client']['type'] === 'browser') { $browserParser = new CachedBrowserParser($userAgent, $clientHints); $browserParser->setCachedResult($this->client); $this->client = $browserParser->parse(); diff --git a/tests/Integration/CachedEntryTest.php b/tests/Integration/CachedEntryTest.php index 73975755..35fc358d 100644 --- a/tests/Integration/CachedEntryTest.php +++ b/tests/Integration/CachedEntryTest.php @@ -34,6 +34,95 @@ public function tearDown(): void Filesystem::unlinkRecursive(CachedEntry::getCacheDir(), true); } + public function testConstructEmptyParams() + { + $instance = new CachedEntry('', [], []); + $this->assertIsObject($instance); + $this->assertInstanceOf(CachedEntry::class, $instance); + } + + public function testConstructSomeValues() + { + $values = [ + 'bot' => 'testBot', + 'brand' => 'testBrand', + 'client'=> 'testClient', + 'device'=> 2, + 'model'=> 'testModel', + 'os' => 'testOs', + ]; + + $instance = new CachedEntry('', [], $values); + $this->assertIsObject($instance); + $this->assertInstanceOf(CachedEntry::class, $instance); + $this->assertSame($values['bot'], $instance->getBot()); + $this->assertSame($values['brand'], $instance->getBrandName()); + $this->assertSame($values['client'], $instance->getClient()); + $this->assertSame($values['device'], $instance->getDevice()); + $this->assertSame($values['model'], $instance->getModel()); + $this->assertSame($values['os'], $instance->getOs()); + } + + public function testConstruct() + { + $userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.66"; + + $values = [ + 'bot' => null, + 'brand' => '', + 'client' => [ + 'type' => 'browser', + 'name' => 'Microsoft Edge', + 'version' => '97.0.1072.66' + ], + 'device' => 1, + 'model' => '', + 'os' => [ + 'name' => 'Windows', + 'version' => '10', + ], + ]; + + $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' => "" + ]; + + $expectedClient = [ + 'type' => 'browser', + 'name' => 'Microsoft Edge', + 'short_name' => 'PS', + 'version' => '98.0', + 'engine' => '', + 'engine_version' => '', + 'family' => 'Internet Explorer', + ]; + + $expectedOs = [ + 'name' => 'Windows', + 'short_name' => 'WIN', + 'version' => '11', + 'platform' => 'x64', + 'family' => 'Windows', + ]; + + $instance = new CachedEntry($userAgent, $clientHints, $values); + $this->assertIsObject($instance); + $this->assertInstanceOf(CachedEntry::class, $instance); + $this->assertSame('', $instance->getBot()); + $this->assertSame($values['brand'], $instance->getBrandName()); + $this->assertSame($expectedClient, $instance->getClient()); + $this->assertSame($values['device'], $instance->getDevice()); + $this->assertSame($values['model'], $instance->getModel()); + $this->assertSame($expectedOs, $instance->getOs()); + } + public function testGetNumCacheFiles_noneCached() { $this->assertEquals(0, CachedEntry::getNumEntriesInCacheDir());