Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors from undefined indexes #61

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CachedEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
89 changes: 89 additions & 0 deletions tests/Integration/CachedEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down