Skip to content

Commit

Permalink
fix(Command) add command to download Chromedriver
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 22, 2023
1 parent 83cce7f commit a50084c
Show file tree
Hide file tree
Showing 24 changed files with 1,415 additions and 10,537 deletions.
1 change: 1 addition & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ extensions:
- "lucatume\\WPBrowser\\Command\\RunOriginal"
- "lucatume\\WPBrowser\\Command\\RunAll"
- "lucatume\\WPBrowser\\Command\\GenerateWPUnit"
- "lucatume\\WPBrowser\\Command\\ChromedriverUpdate"
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ext-fileinfo": "*",
"ext-json": "*",
"ext-curl": "*",
"ext-zip": "*",
"composer-runtime-api": "^2.2",
"codeception/codeception": "^5.0",
"codeception/module-asserts": "^2.0 || ^3.0",
Expand Down
8 changes: 8 additions & 0 deletions config/containers/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ RUN chmod a+x /usr/local/bin/xdebug-on && chmod a+x /usr/local/bin/xdebug-off
# Ensure the gropup and user exist.
RUN getent group ${USER_GID} || groupadd -g ${USER_GID} ${USER_NAME}
RUN getent passwd ${USER_UID} || useradd -u ${USER_UID} -g ${USER_GID} -m -s /bin/bash ${USER_NAME}

# Create a Chrome mock where expected.
RUN if [ "${TARGET}" = "codeception" ]; then \
mkdir -p /usr/bin && \
echo '#!/bin/bash' > /usr/bin/google-chrome && \
echo 'echo 'Google Chrome 116.0.5845.96'' >> /usr/bin/google-chrome && \
chmod a+x /usr/bin/google-chrome; \
fi
73 changes: 73 additions & 0 deletions src/Command/ChromedriverUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace lucatume\WPBrowser\Command;

use Codeception\CustomCommandInterface;
use JsonException;
use lucatume\WPBrowser\Exceptions\InvalidArgumentException;
use lucatume\WPBrowser\Utils\ChromedriverInstaller;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ChromedriverUpdate extends Command implements CustomCommandInterface
{
public static function getCommandName(): string
{
return 'chromedriver:update';
}

public function configure(): void
{
$this->setDescription('Updates the Chromedriver binary.')
->addArgument(
'version',
InputArgument::OPTIONAL,
'The version of Chrome to install chromedriver for.',
''
)->addOption(
'platform',
'',
InputOption::VALUE_REQUIRED,
'The platform to install Chromedriver for.',
''
)->addOption(
'binary',
'',
InputOption::VALUE_REQUIRED,
'The path to the Chrome binary to download Chromedriver for.',
false
);
}

/**
* @throws JsonException
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$platform = $input->getOption('platform') ?: null;

if ($platform && !is_string($platform)) {
throw new InvalidArgumentException('The platform option must be a string.');
}

$version = $input->getArgument('version') ?: null;

if ($version && !is_string($version)) {
throw new InvalidArgumentException('The version argument must be a string.');
}

$binary = $input->getOption('binary') ?: null;

if ($binary && !is_string($binary)) {
throw new InvalidArgumentException('The binary option must be a string.');
}

$chromedriverInstaller = new ChromedriverInstaller($version, $platform, $binary, $output);
$chromedriverInstaller->install();

return 0;
}
}
9 changes: 6 additions & 3 deletions src/Project/ContentProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Closure;
use Codeception\InitTemplate;
use lucatume\WPBrowser\Command\ChromedriverUpdate;
use lucatume\WPBrowser\Command\DevInfo;
use lucatume\WPBrowser\Command\DevRestart;
use lucatume\WPBrowser\Command\DevStart;
use lucatume\WPBrowser\Command\DevStop;
use lucatume\WPBrowser\Exceptions\RuntimeException;
use lucatume\WPBrowser\Extension\BuiltInServerController;
use lucatume\WPBrowser\Extension\ChromeDriverController;
use lucatume\WPBrowser\Utils\ChromedriverInstaller;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\Utils\Random;
use lucatume\WPBrowser\WordPress\Database\SQLiteDatabase;
Expand All @@ -20,8 +22,6 @@

abstract class ContentProject extends InitTemplate implements ProjectInterface
{
use SetupTemplateTrait;

protected TestEnvironment $testEnvironment;

abstract protected function getProjectType(): string;
Expand Down Expand Up @@ -123,7 +123,9 @@ public function setup(): void
}
$this->sayInfo('Created database dump in <info>tests/Support/Data/dump.sql</info>.');

$this->addChromedriverDevDependency();
$this->sayInfo('Installing Chromedriver ...');
$chromedriverPath = (new ChromedriverInstaller())->install();
$this->sayInfo("Chromedriver installed in $chromedriverPath");
$chromedriverPort = Random::openLocalhostPort();
$this->testEnvironment->testTablePrefix = 'test_';
$this->testEnvironment->wpTablePrefix = 'wp_';
Expand Down Expand Up @@ -157,6 +159,7 @@ public function setup(): void
$this->testEnvironment->customCommands[] = DevStop::class;
$this->testEnvironment->customCommands[] = DevInfo::class;
$this->testEnvironment->customCommands[] = DevRestart::class;
$this->testEnvironment->customCommands[] = ChromedriverUpdate::class;
$this->testEnvironment->wpRootDir = FS::relativePath($this->workDir, $wpRootDir);
$this->testEnvironment->dbUrl = 'sqlite://%codecept_root_dir%/tests/_wordpress/data/db.sqlite';

Expand Down
30 changes: 0 additions & 30 deletions src/Project/SetupTemplateTrait.php

This file was deleted.

9 changes: 6 additions & 3 deletions src/Project/SiteProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace lucatume\WPBrowser\Project;

use Codeception\InitTemplate;
use lucatume\WPBrowser\Command\ChromedriverUpdate;
use lucatume\WPBrowser\Command\DevInfo;
use lucatume\WPBrowser\Command\DevRestart;
use lucatume\WPBrowser\Command\DevStart;
use lucatume\WPBrowser\Command\DevStop;
use lucatume\WPBrowser\Exceptions\RuntimeException;
use lucatume\WPBrowser\Extension\BuiltInServerController;
use lucatume\WPBrowser\Extension\ChromeDriverController;
use lucatume\WPBrowser\Utils\ChromedriverInstaller;
use lucatume\WPBrowser\Utils\Filesystem as FS;
use lucatume\WPBrowser\Utils\Random;
use lucatume\WPBrowser\WordPress\Database\SQLiteDatabase;
Expand All @@ -22,8 +24,6 @@

class SiteProject extends InitTemplate implements ProjectInterface
{
use SetupTemplateTrait;

private Installation $installation;
private TestEnvironment $testEnvironment;

Expand Down Expand Up @@ -117,7 +117,9 @@ public function setup(): void
}
$this->sayInfo('Created database dump in <info>tests/Support/Data/dump.sql</info>.');

$this->addChromedriverDevDependency();
$this->sayInfo('Installing Chromedriver ...');
$chromedriverPath = (new ChromedriverInstaller())->install();
$this->sayInfo("Chromedriver installed in $chromedriverPath");
$chromedriverPort = Random::openLocalhostPort();
$this->testEnvironment->testTablePrefix = 'test_';
$this->testEnvironment->wpTablePrefix = 'wp_';
Expand Down Expand Up @@ -152,6 +154,7 @@ public function setup(): void
$this->testEnvironment->customCommands[] = DevStop::class;
$this->testEnvironment->customCommands[] = DevInfo::class;
$this->testEnvironment->customCommands[] = DevRestart::class;
$this->testEnvironment->customCommands[] = ChromedriverUpdate::class;
$this->testEnvironment->wpRootDir = '.';
$this->testEnvironment->dbUrl = 'sqlite://%codecept_root_dir%/tests/Support/Data/db.sqlite';

Expand Down
Loading

0 comments on commit a50084c

Please sign in to comment.