Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Jul 22, 2023
1 parent b4c2dd6 commit f763fd4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 81 deletions.
3 changes: 0 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/OperatingSystem.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">
Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function fetchUrl(string $url): string
*/
protected function resolveChromeDriverDownloadUrl(string $version, string $operatingSystem): string
{
$slug = chromedriver_slug($version, $operatingSystem);
$slug = OperatingSystem::chromeDriverSlug($operatingSystem, $version);

if (version_compare($version, '115.0', '<')) {
return sprintf('https://chromedriver.storage.googleapis.com/%s/chromedriver_%s.zip', $version, $slug);
Expand Down
91 changes: 84 additions & 7 deletions src/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,97 @@

namespace Orchestra\DuskUpdater;

use InvalidArgumentException;

class OperatingSystem
{
/**
* List of available Operating System platforms.
*
* @var array<string, array{slug: string, binary: string, commands: array<int, string>}>
*/
protected static $platforms = [
'linux' => [
'slug' => 'linux64',
'binary' => 'chromedriver-linux',
'commands' => [
'/usr/bin/google-chrome --version',
'/usr/bin/chromium-browser --version',
'/usr/bin/chromium --version',
'/usr/bin/google-chrome-stable --version',
],
],
'mac' => [
'slug' => 'mac-x64',
'binary' => 'chromedriver-mac',
'commands' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
],
'mac-intel' => [
'slug' => 'mac-x64',
'binary' => 'chromedriver-mac-intel',
'commands' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
],
'mac-arm' => [
'slug' => 'mac-arm64',
'binary' => 'chromedriver-mac-arm',
'commands' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
],
'win' => [
'slug' => 'win32',
'binary' => 'chromedriver-win.exe',
'commands' => [
'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
],
],
];

public static function chromeVersionCommands(string $operatingSystem): array
{
$commands = static::$platforms[$operatingSystem]['commands'] ?? null;

if (is_null($commands)) {
throw new InvalidArgumentException("Unable to find commands for Operating System [{$operatingSystem}]");
}

return $commands;
}

/**
* Resolve ChromeDriver slug.
*
* @param string|null $version
*/
public static function chromeDriverSlug(string $operatingSystem, $version = null): string
{
$slug = static::$platforms[$operatingSystem]['slug'] ?? null;

if (is_null($slug)) {
throw new InvalidArgumentException("Unable to find ChromeDriver slug for Operating System [{$operatingSystem}]");
}

if (! is_null($version) && version_compare($version, '115.0', '<')) {
if ($slug === 'mac-arm64') {
return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64';
} elseif ($slug === 'mac-x64') {
return 'mac64';
}
}

return $slug;
}

/**
* Returns all possible OS.
*/
public static function all(): array
{
return [
'linux',
'mac',
'mac-intel',
'mac-arm',
'win',
];
return array_keys(static::$platforms);
}

/**
Expand Down
30 changes: 0 additions & 30 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@ function chromedriver(string $operatingSystem): string
return $filename;
}

/**
* Determine ChromeDriver slug.
*
* @throws \InvalidArgumentException
*/
function chromedriver_slug(string $version, string $operatingSystem): string
{
$slugs = [
'linux' => 'linux64',
'mac' => 'mac-x64',
'mac-intel' => 'mac-x64',
'mac-arm' => 'mac-arm64',
'win' => 'win32',
];

if (is_null($slug = ($slugs[$operatingSystem] ?? null))) {
throw new InvalidArgumentException("Unable to find ChromeDriver slug for Operating System [{$operatingSystem}]");
}

if (version_compare($version, '115.0', '<')) {
if ($slug === 'mac-arm64') {
return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64';
} elseif ($slug === 'mac-x64') {
return 'mac64';
}
}

return $slug;
}

/**
* Rename exported ChromeDriver binary filename.
*/
Expand Down
40 changes: 0 additions & 40 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orchestra\DuskUpdater\Tests;

use function Orchestra\DuskUpdater\chromedriver;
use function Orchestra\DuskUpdater\chromedriver_slug;
use function Orchestra\DuskUpdater\rename_chromedriver_binary;
use function Orchestra\DuskUpdater\request_context_payload;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -35,45 +34,6 @@ public static function resolveChromeDriverDataProvider()
yield ['win', 'chromedriver-win.exe'];
}

/**
* @dataProvider resolveChromeDriverSlugDataProvider
*/
public function test_it_can_resolve_chromedriver_slug($version, $os, $expected)
{
$this->assertSame($expected, chromedriver_slug($version, $os));
}

public function test_it_cant_resolve_invalid_chromedriver_slug()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Unable to find ChromeDriver slug for Operating System [window_os]');

chromedriver_slug('115.0', 'window_os');
}

public static function resolveChromeDriverSlugDataProvider()
{
yield ['115.0', 'linux', 'linux64'];
yield ['113.0', 'linux', 'linux64'];
yield ['105.0', 'linux', 'linux64'];

yield ['115.0', 'mac', 'mac-x64'];
yield ['113.0', 'mac', 'mac64'];
yield ['105.0', 'mac', 'mac64'];

yield ['115.0', 'mac-intel', 'mac-x64'];
yield ['113.0', 'mac-intel', 'mac64'];
yield ['105.0', 'mac-intel', 'mac64'];

yield ['115.0', 'mac-arm', 'mac-arm64'];
yield ['113.0', 'mac-arm', 'mac_arm64'];
yield ['105.0', 'mac-arm', 'mac64_m1'];

yield ['115.0', 'win', 'win32'];
yield ['113.0', 'win', 'win32'];
yield ['105.0', 'win', 'win32'];
}

/**
* @dataProvider chromedriverBinaryFilenameDataProvider
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/OperatingSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,43 @@ public function test_it_has_correct_os()
'win',
], OperatingSystem::all());
}

/**
* @dataProvider resolveChromeDriverSlugDataProvider
*/
public function test_it_can_resolve_chromedriver_slug($version, $os, $expected)
{
$this->assertSame($expected, OperatingSystem::chromeDriverSlug($os, $version));
}

public function test_it_cant_resolve_invalid_chromedriver_slug()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Unable to find ChromeDriver slug for Operating System [window_os]');

OperatingSystem::chromeDriverSlug('window_os');
}

public static function resolveChromeDriverSlugDataProvider()
{
yield ['115.0', 'linux', 'linux64'];
yield ['113.0', 'linux', 'linux64'];
yield ['105.0', 'linux', 'linux64'];

yield ['115.0', 'mac', 'mac-x64'];
yield ['113.0', 'mac', 'mac64'];
yield ['105.0', 'mac', 'mac64'];

yield ['115.0', 'mac-intel', 'mac-x64'];
yield ['113.0', 'mac-intel', 'mac64'];
yield ['105.0', 'mac-intel', 'mac64'];

yield ['115.0', 'mac-arm', 'mac-arm64'];
yield ['113.0', 'mac-arm', 'mac_arm64'];
yield ['105.0', 'mac-arm', 'mac64_m1'];

yield ['115.0', 'win', 'win32'];
yield ['113.0', 'win', 'win32'];
yield ['105.0', 'win', 'win32'];
}
}

0 comments on commit f763fd4

Please sign in to comment.