Skip to content

Commit

Permalink
test(unit) cover process and controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Aug 17, 2023
1 parent e25eb24 commit 00ce9c5
Show file tree
Hide file tree
Showing 55 changed files with 9,397 additions and 219,255 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ clean_tmp:
.PHONY: clean_tmp

update_core_phpunit_includes:
bin/update-core-phpunit-includes
bin/update_core_phpunit_includes
.PHONY: update_core_phpunit_includes

package: update_core_phpunit_includes test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ includes_src="https://github.com/WordPress/wordpress-develop/branches/trunk/test
rm -rf "${root_dir}"/includes/core-phpunit/includes &&
mkdir -p "${root_dir}"/includes/core-phpunit &&
cd "${root_dir}"/includes/core-phpunit &&
svn export $includes_src &&
svn export $includes_src
git apply "${root_dir}"/config/patches/core-phpunit/includes/abstract-testcase.php.patch &&
git apply "${root_dir}"/config/patches/core-phpunit/includes/testcase-ajax.php.patch &&
git apply "${root_dir}"/config/patches/core-phpunit/includes/testcase-canonical.php.patch &&
Expand Down
20 changes: 0 additions & 20 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,7 @@ coverage:
- src/*
wpFolder: '%WORDPRESS_ROOT_DIR%'
extensions:
enabled:
- lucatume\WPBrowser\Extension\ChromeDriverController
- lucatume\WPBrowser\Extension\BuiltInServerController
- lucatume\WPBrowser\Extension\DockerComposeController
config:
lucatume\WPBrowser\Extension\ChromeDriverController:
port: '%CHROMEDRIVER_PORT%'
lucatume\WPBrowser\Extension\BuiltInServerController:
docroot: '%WORDPRESS_ROOT_DIR%'
workers: 5
port: '%BUILT_IN_SERVER_PORT%'
lucatume\WPBrowser\Extension\DockerComposeController:
compose-file: 'tests/docker-compose.yml'
env-file: 'tests/.env'
commands:
- "lucatume\\WPBrowser\\Command\\RunOriginal"
- "lucatume\\WPBrowser\\Command\\RunAll"
- "lucatume\\WPBrowser\\Command\\GenerateWPUnit"
- "lucatume\\WPBrowser\\Command\\DevStart"
- "lucatume\\WPBrowser\\Command\\DevStop"
- "lucatume\\WPBrowser\\Command\\DevInfo"
- "lucatume\\WPBrowser\\Command\\DevRestart"
- "lucatume\\WPBrowser\\Command\\DbExport"
- "lucatume\\WPBrowser\\Command\\DbImport"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/includes/core-phpunit/includes/testcase-rest-api.php b/includes/core-phpunit/includes/testcase-rest-api.php
index 45e7c5e1..44e8c1b0 100644
index 54644f13..670a2cd1 100644
--- a/includes/core-phpunit/includes/testcase-rest-api.php
+++ b/includes/core-phpunit/includes/testcase-rest-api.php
@@ -1,6 +1,8 @@
Expand All @@ -11,4 +11,4 @@ index 45e7c5e1..44e8c1b0 100644
+abstract class WPRestApiTestCase extends WPTestCase {
protected function assertErrorResponse( $code, $response, $status = null ) {

if ( is_a( $response, 'WP_REST_Response' ) ) {
if ( $response instanceof WP_REST_Response ) {
21 changes: 14 additions & 7 deletions config/patches/sqlite-database-integration/db.copy.patch
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
diff --git a/includes/sqlite-database-integration/db.copy b/includes/sqlite-database-integration/db.copy
index 0b0797e8..23caff78 100644
index 0b0797e8..e164139d 100644
--- a/includes/sqlite-database-integration/db.copy
+++ b/includes/sqlite-database-integration/db.copy
@@ -32,6 +32,17 @@ if ( ! defined( 'DATABASE_TYPE' ) ) {
if ( ! defined( 'DB_ENGINE' ) ) {
define( 'DB_ENGINE', 'sqlite' );
@@ -26,11 +26,22 @@ if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugi

// Constant for backward compatibility.
if ( ! defined( 'DATABASE_TYPE' ) ) {
- define( 'DATABASE_TYPE', 'sqlite' );
+ define( 'DATABASE_TYPE', getenv( 'DATABASE_TYPE' ) ?: 'sqlite' );
}
// Define SQLite constant.
if ( ! defined( 'DB_ENGINE' ) ) {
- define( 'DB_ENGINE', 'sqlite' );
+ define( 'DB_ENGINE', getenv( 'DB_ENGINE' ) ?: 'sqlite' );
+}
+// Define SQLite main file constant to avoid having the plugin loaded automatically.
+if ( ! defined( 'SQLITE_MAIN_FILE' ) ) {
+ define( 'SQLITE_MAIN_FILE', '{SQLITE_MAIN_FILE}' );
Expand All @@ -16,7 +24,6 @@ index 0b0797e8..23caff78 100644
+}
+if( ! defined( 'DB_FILE' ) && getenv( 'DB_FILE' ) ) {
+ define( 'DB_FILE', getenv( 'DB_FILE' ) );
+}

}
// Require the implementation from the plugin.
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php';
2 changes: 1 addition & 1 deletion includes/core-phpunit/includes/testcase-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
abstract class WPRestApiTestCase extends WPTestCase {
protected function assertErrorResponse( $code, $response, $status = null ) {

if ( is_a( $response, 'WP_REST_Response' ) ) {
if ( $response instanceof WP_REST_Response ) {
$response = $response->as_error();
}

Expand Down
24 changes: 13 additions & 11 deletions src/Extension/BuiltInServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Codeception\Exception\ExtensionException;
use lucatume\WPBrowser\ManagedProcess\PhpBuiltInServer;
use lucatume\WPBrowser\Utils\Arr;
use lucatume\WPBrowser\Utils\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;

class BuiltInServerController extends ServiceExtension
Expand Down Expand Up @@ -46,7 +48,7 @@ public function stop(OutputInterface $output): void
if ($read === false) {
throw new ExtensionException(
$this,
'Failed to read the PHP built-in server PID file.'
'Could not read the PHP built-in server PID file.'
);
}

Expand All @@ -66,10 +68,10 @@ public function getPrettyName(): string
/**
* @return array{
* running: string,
* pidFile: string,
* port: int,
* docroot: string,
* workers: int,
* pid: int|null,
* url: string,
* env: array<string,string|int|float>
* }
Expand All @@ -82,9 +84,9 @@ public function getInfo(): array

return [
'running' => is_file($pidFile) ? 'yes' : 'no',
'pid' => is_file($pidFile) ? (int)file_get_contents($pidFile) : null,
'pidFile' => Filesystem::relativePath(codecept_root_dir(), $this->getPidFile()),
'port' => $port,
'docroot' => $docRoot,
'docroot' => Filesystem::relativePath(codecept_root_dir(), $docRoot),
'workers' => $workers,
'url' => 'http://localhost:' . $port . '/',
'env' => $env
Expand Down Expand Up @@ -130,24 +132,24 @@ private function parseConfig(): array
/** @var array{workers?: number} $config */
$workers = (int)($config['workers'] ?? 5);

if (isset($config['env']) && !is_array($config['env'])) {
if (isset($config['env']) && !(is_array($config['env']) && Arr::isAssociative($config['env']))) {
throw new ExtensionException(
$this,
'The "env" configuration option must be an array.'
'The "env" configuration option must be an associative array.'
);
}
$env = $config['env'] ?? [];
$env = array_map( static function ( $value ): mixed {
return is_string( $value ) ?
str_replace( '%codecept_root_dir%', rtrim( codecept_root_dir(), '\\/' ), $value )
$env = array_map(static function ($value): mixed {
return is_string($value) ?
str_replace('%codecept_root_dir%', rtrim(codecept_root_dir(), '\\/'), $value)
: $value;
}, $env );
}, $env);

return [$port, $docRoot, $workers, $env];
}

private function getPidFile(): string
{
return codecept_output_dir(PhpBuiltInServer::PID_FILE_NAME);
return PhpBuiltInServer::getPidFile();
}
}
24 changes: 10 additions & 14 deletions src/Extension/ChromeDriverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Codeception\Exception\ExtensionException;
use lucatume\WPBrowser\ManagedProcess\ChromeDriver;
use lucatume\WPBrowser\Utils\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;

class ChromeDriverController extends ServiceExtension
Expand Down Expand Up @@ -42,7 +43,7 @@ public function stop(OutputInterface $output): void
if ($read === false) {
throw new ExtensionException(
$this,
'Failed to read the ChromeDriver PID file.'
'Could not read the ChromeDriver PID file.'
);
}

Expand All @@ -62,27 +63,23 @@ public function getPrettyName(): string
/**
* @return array{
* running: string,
* port: int,
* pidFile: string
* pidFile: string,
* port: int
* }
* @throws ExtensionException
*/
public function getInfo(): array
{
$pidFile = $this->getPidFile();
$port = $this->getPort();

return [
'running' => is_file($pidFile) ? 'yes' : 'no',
'pidFile' => $pidFile,
'port' => $port,
'running' => is_file($this->getPidFile()) ? 'yes' : 'no',
'pidFile' => Filesystem::relativePath(codecept_root_dir(), $this->getPidFile()),
'port' => $this->getPort(),
];
}

private function getPidFile(): string
{
$pidFile = codecept_output_dir(ChromeDriver::PID_FILE_NAME);
return $pidFile;
return ChromeDriver::getPidFile();
}

/**
Expand All @@ -91,14 +88,13 @@ private function getPidFile(): string
private function getPort(): int
{
$config = $this->config;
if (isset($config['port']) && !is_numeric($config['port']) && $config['port'] > 0) {
if (isset($config['port']) && !(is_numeric($config['port']) && $config['port'] > 0)) {
throw new ExtensionException(
$this,
'The "port" configuration option must be an integer greater than 0.'
);
}
/** @var array{port?: number} $config */
$port = (int)($config['port'] ?? 4444);
return $port;
return (int)($config['port'] ?? 4444);
}
}
20 changes: 11 additions & 9 deletions src/Extension/DockerComposeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;
use Throwable;

class DockerComposeController extends ServiceExtension
{
Expand All @@ -17,7 +18,7 @@ class DockerComposeController extends ServiceExtension
*/
public function start(OutputInterface $output): void
{
$runningFile = $this->getRunningFile();
$runningFile = self::getRunningFile();

if (is_file($runningFile)) {
$output->writeln('Docker Compose stack already up.');
Expand All @@ -33,7 +34,7 @@ public function start(OutputInterface $output): void
$process->mustRun(function () use ($output) {
$output->write('.', false);
});
} catch (ProcessFailedException $e) {
} catch (Throwable $e) {
throw new ExtensionException(
$this,
'Failed to start Docker Compose services: ' . $e->getMessage()
Expand Down Expand Up @@ -71,13 +72,13 @@ public function stop(OutputInterface $output): void
$output->write('.', false);
});
$output->write(' ok', true);
} catch (ProcessFailedException $e) {
} catch (Throwable $e) {
throw new ExtensionException(
$this,
'Failed to stop Docker Compose: ' . $e->getMessage()
);
}
$runningFile = $this->getRunningFile();
$runningFile = self::getRunningFile();
if (is_file($runningFile) && !unlink($runningFile)) {
throw new ExtensionException(
$this,
Expand Down Expand Up @@ -142,13 +143,14 @@ public function getPrettyName(): string
*/
public function getInfo(): array
{
$runningFile = $this->getRunningFile();
$runningFile = self::getRunningFile();
if (is_file($runningFile)) {
// run the docker compose config command and return the output
$dockerComposeConfig = (new Process([
// Run the docker compose config command and return the output.
$process = new Process([
...$this->getCommand($this->config),
'config'
]))->mustRun()->getOutput();
]);
$dockerComposeConfig = $process->mustRun()->getOutput();

return [
'status' => 'up',
Expand All @@ -162,7 +164,7 @@ public function getInfo(): array
];
}

private function getRunningFile(): string
public static function getRunningFile(): string
{
return codecept_output_dir(self::PID_FILENAME);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Extension/ServiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use lucatume\WPBrowser\Utils\Arr;
use Symfony\Component\Console\Output\OutputInterface;

abstract class ServiceExtension extends Extension
Expand All @@ -24,7 +25,9 @@ public function onModuleInit(SuiteEvent $event): void
{
$config = $this->config;

if (isset($config['suites']) && !is_array($config['suites'])) {
if (isset($config['suites']) && !(
is_array($config['suites']) && Arr::containsOnly($config['suites'], 'string'))
) {
throw new ExtensionException($this, 'The "suites" configuration option must be an array.');
}
/** @var string[] $suites */
Expand Down
3 changes: 2 additions & 1 deletion src/ManagedProcess/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ChromeDriver implements ManagedProcessInterface
use ManagedProcessTrait;

public const PID_FILE_NAME = 'chromedriver.pid';
public const PORT_DEFAULT = 9515;

private string $binary;
private string $prettyName = 'ChromeDriver';
Expand All @@ -21,7 +22,7 @@ class ChromeDriver implements ManagedProcessInterface
* @throws RuntimeException
*/
public function __construct(
private int $port = 0,
private int $port = self::PORT_DEFAULT,
private array $arguments = ['--url-base=/wd/hub'],
?string $binary = null
) {
Expand Down
Loading

0 comments on commit 00ce9c5

Please sign in to comment.