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 f763fd4 commit 8125347
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 79 deletions.
32 changes: 3 additions & 29 deletions src/Concerns/DetectsChromeVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,14 @@

use Exception;
use InvalidArgumentException;
use function Orchestra\DuskUpdater\chromedriver;
use Orchestra\DuskUpdater\OperatingSystem;
use Symfony\Component\Process\Process;

/**
* @copyright Originally created by Jonas Staudenmeir: https://github.com/staudenmeir/dusk-updater
*/
trait DetectsChromeVersion
{
/**
* The default commands to detect the installed Chrome/Chromium version.
*
* @var array
*/
protected $chromeCommands = [
'linux' => [
'/usr/bin/google-chrome --version',
'/usr/bin/chromium-browser --version',
'/usr/bin/chromium --version',
'/usr/bin/google-chrome-stable --version',
],
'mac' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
'mac-intel' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
'mac-arm' => [
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version',
],
'win' => [
'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version',
],
];

/**
* The legacy versions for the ChromeDriver.
*
Expand Down Expand Up @@ -144,7 +118,7 @@ protected function installedChromeVersion(string $operatingSystem, $directory =

$commands = [$directory.' --version'];
} else {
$commands = $this->chromeCommands[$operatingSystem];
$commands = OperatingSystem::chromeVersionCommands($operatingSystem);
}

foreach ($commands as $command) {
Expand Down Expand Up @@ -183,7 +157,7 @@ protected function installedChromeVersion(string $operatingSystem, $directory =
*/
protected function installedChromeDriverVersion(string $operatingSystem, string $directory)
{
$filename = chromedriver($operatingSystem);
$filename = OperatingSystem::chromeDriverBinary($operatingSystem);

if (! file_exists($directory.$filename)) {
return [
Expand Down
17 changes: 17 additions & 0 deletions src/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class OperatingSystem
],
];

/**
* Resolve Chrome version commands.
*/
public static function chromeVersionCommands(string $operatingSystem): array
{
$commands = static::$platforms[$operatingSystem]['commands'] ?? null;
Expand All @@ -63,6 +66,20 @@ public static function chromeVersionCommands(string $operatingSystem): array
return $commands;
}

/**
* Resolve ChromeDriver binary.
*/
public static function chromeDriverBinary(string $operatingSystem): string
{
$binary = static::$platforms[$operatingSystem]['binary'] ?? null;

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

return $binary;
}

/**
* Resolve ChromeDriver slug.
*
Expand Down
24 changes: 0 additions & 24 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@

namespace Orchestra\DuskUpdater;

use InvalidArgumentException;

/**
* ChromeDriver name by Operating System.
*
* @throws \InvalidArgumentException
*/
function chromedriver(string $operatingSystem): string
{
$filenames = [
'linux' => 'chromedriver-linux',
'mac' => 'chromedriver-mac',
'mac-intel' => 'chromedriver-mac-intel',
'mac-arm' => 'chromedriver-mac-arm',
'win' => 'chromedriver-win.exe',
];

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

return $filename;
}

/**
* Rename exported ChromeDriver binary filename.
*/
Expand Down
26 changes: 0 additions & 26 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,12 @@

namespace Orchestra\DuskUpdater\Tests;

use function Orchestra\DuskUpdater\chromedriver;
use function Orchestra\DuskUpdater\rename_chromedriver_binary;
use function Orchestra\DuskUpdater\request_context_payload;
use PHPUnit\Framework\TestCase;

class HelpersTest extends TestCase
{
/**
* @dataProvider resolveChromeDriverDataProvider
*/
public function test_it_can_resolve_chromedriver($os, $expected)
{
$this->assertSame($expected, chromedriver($os));
}

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

chromedriver('window_os');
}

public static function resolveChromeDriverDataProvider()
{
yield ['linux', 'chromedriver-linux'];
yield ['mac', 'chromedriver-mac'];
yield ['mac-intel', 'chromedriver-mac-intel'];
yield ['mac-arm', 'chromedriver-mac-arm'];
yield ['win', 'chromedriver-win.exe'];
}

/**
* @dataProvider chromedriverBinaryFilenameDataProvider
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/OperatingSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ public function test_it_has_correct_os()
], OperatingSystem::all());
}

/**
* @dataProvider resolveChromeDriverBinaryDataProvider
*/
public function test_it_can_resolve_chromedriver_binary($os, $expected)
{
$this->assertSame($expected, OperatingSystem::chromeDriverBinary($os));
}

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

OperatingSystem::chromeDriverBinary('window_os');
}

public static function resolveChromeDriverBinaryDataProvider()
{
yield ['linux', 'chromedriver-linux'];
yield ['mac', 'chromedriver-mac'];
yield ['mac-intel', 'chromedriver-mac-intel'];
yield ['mac-arm', 'chromedriver-mac-arm'];
yield ['win', 'chromedriver-win.exe'];
}

/**
* @dataProvider resolveChromeDriverSlugDataProvider
*/
Expand Down

0 comments on commit 8125347

Please sign in to comment.