119 lines
3.4 KiB
PHP
119 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use Facebook\WebDriver\Chrome\ChromeOptions;
|
|
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
|
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
|
use Laravel\Dusk\TestCase as BaseTestCase;
|
|
use PHPUnit\Framework\Attributes\BeforeClass;
|
|
|
|
abstract class DuskTestCase extends BaseTestCase
|
|
{
|
|
use CreatesApplication;
|
|
|
|
/**
|
|
* Prepare for Dusk test execution.
|
|
*/
|
|
#[BeforeClass]
|
|
public static function prepare(): void
|
|
{
|
|
if (! static::runningInSail()) {
|
|
static::ensureChromeDriverBinaryAlias();
|
|
static::startChromeDriver(['--port=9515']);
|
|
static::waitForChromeDriver();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create the RemoteWebDriver instance.
|
|
*/
|
|
protected function driver(): RemoteWebDriver
|
|
{
|
|
$options = (new ChromeOptions())->addArguments([
|
|
'--disable-gpu',
|
|
'--headless=new',
|
|
'--window-size=1920,1080',
|
|
'--no-sandbox',
|
|
'--disable-dev-shm-usage',
|
|
'--disable-search-engine-choice-screen',
|
|
'--ignore-certificate-errors',
|
|
'--allow-insecure-localhost',
|
|
]);
|
|
|
|
return RemoteWebDriver::create(
|
|
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
|
|
DesiredCapabilities::chrome()->setCapability(
|
|
ChromeOptions::CAPABILITY,
|
|
$options
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Dusk may install a differently named macOS Apple Silicon binary.
|
|
*
|
|
* This method intentionally does not use base_path(), app(), or Artisan because
|
|
* PHPUnit calls BeforeClass before Laravel has booted the application container.
|
|
*/
|
|
private static function ensureChromeDriverBinaryAlias(): void
|
|
{
|
|
$binDirectory = dirname(__DIR__).'/vendor/laravel/dusk/bin';
|
|
$expected = $binDirectory.'/chromedriver-mac-arm';
|
|
|
|
if (is_file($expected)) {
|
|
@chmod($expected, 0755);
|
|
|
|
return;
|
|
}
|
|
|
|
if (! is_dir($binDirectory)) {
|
|
@mkdir($binDirectory, 0755, true);
|
|
}
|
|
|
|
$candidates = array_values(array_filter(
|
|
glob($binDirectory.'/chromedriver*') ?: [],
|
|
static function (string $path) use ($expected): bool {
|
|
return is_file($path)
|
|
&& $path !== $expected
|
|
&& ! str_ends_with($path, '.zip')
|
|
&& ! str_ends_with($path, '.txt');
|
|
}
|
|
));
|
|
|
|
usort($candidates, static function (string $left, string $right): int {
|
|
$score = static fn (string $path): int =>
|
|
str_contains($path, 'mac-arm64') ? 0
|
|
: (str_contains($path, 'mac-arm') ? 1
|
|
: (str_contains($path, 'mac') ? 2 : 3));
|
|
|
|
return $score($left) <=> $score($right);
|
|
});
|
|
|
|
if ($candidates !== []) {
|
|
@copy($candidates[0], $expected);
|
|
@chmod($expected, 0755);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Wait until ChromeDriver is listening before the first browser session starts.
|
|
*/
|
|
private static function waitForChromeDriver(): void
|
|
{
|
|
$deadline = microtime(true) + 5.0;
|
|
|
|
do {
|
|
$connection = @fsockopen('127.0.0.1', 9515);
|
|
|
|
if (is_resource($connection)) {
|
|
fclose($connection);
|
|
|
|
return;
|
|
}
|
|
|
|
usleep(100_000);
|
|
} while (microtime(true) < $deadline);
|
|
}
|
|
}
|