Skip to content

Commit

Permalink
build(v35) transpile from v4
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Sep 1, 2024
1 parent d4a2067 commit c2f29a2
Show file tree
Hide file tree
Showing 56 changed files with 5,389 additions and 469 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

## Added

- The `MysqlServerController` extension.

### Fixed

- Ensure the `WPLoader` module will initialize correctly when used in `loadOnly` mode not using the `EventDispatcherBridge` extension. (thanks @lxbdr)
- Support loading the `wpdb` class from either the `class-wpdb.php` file or the `wp-db.php` one, supporting older versions of WordPress (thanks @BrianHenryIE)
- Read content, plugins and mu-plugins directories paths from the `WPLoader` configuration parameters correctly.
- In the `WPTestCase` class, remove attachments created during tests between tests.

## [3.6.5] 2024-06-26;

### Changed
Expand Down
35 changes: 25 additions & 10 deletions includes/core-phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,31 @@ public function wp_die_handler( $message, $title, $args ) {
* @since 3.7.0
*/
public function expectDeprecated() {
if ( method_exists( $this, 'getAnnotations' ) ) {
// PHPUnit < 9.5.0.
$annotations = $this->getAnnotations();
} else {
// PHPUnit >= 9.5.0.
$annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations(
static::class,
$this->getName( false )
);
}
if ( method_exists( $this, 'getAnnotations' ) ) {
// PHPUnit < 9.5.0.
$annotations = $this->getAnnotations();
} else if( version_compare(tests_get_phpunit_version(),'10.0.0','<')) {
// PHPUnit >= 9.5.0 < 10.0.0.
$annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations(
static::class,
$this->getName( false )
);
} else {
// PHPUnit >= 10.0.0.
if (method_exists(static::class, $this->name())) {
$reflectionMethod = new \ReflectionMethod(static::class, $this->name());
$docBlock = \PHPUnit\Metadata\Annotation\Parser\DocBlock::ofMethod($reflectionMethod);
$annotations = [
'method' => $docBlock->symbolAnnotations(),
'class' => [],
];
} else {
$annotations = [
'method' => null,
'class' => [],
];
}
}

foreach ( array( 'class', 'method' ) as $depth ) {
if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
Expand Down
11 changes: 10 additions & 1 deletion includes/core-phpunit/includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@
require_once ABSPATH . 'wp-settings.php';

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
require_once ABSPATH . 'wp-includes/class-wpdb.php';

/**
* File was renamed in WordPress 6.1.
*
* @see https://core.trac.wordpress.org/ticket/56268
* @see https://github.com/WordPress/WordPress/commit/8484c7babb6b6ee951f83babea656a294157665d
*/
require_once file_exists( ABSPATH . 'wp-includes/class-wpdb.php' )
? ABSPATH . 'wp-includes/class-wpdb.php'
: ABSPATH . 'wp-includes/wp-db.php';

// Override the PHPMailer.
global $phpmailer;
require_once __DIR__ . '/mock-mailer.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ protected function check_post_data( $post, $data, $context, $links ) {
// Check filtered values.
if ( post_type_supports( $post->post_type, 'title' ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
$this->assertSame( get_the_title( $post->ID ), $data['title']['rendered'] );
remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
if ( 'edit' === $context ) {
$this->assertSame( $post->post_title, $data['title']['raw'] );
} else {
Expand Down
1 change: 1 addition & 0 deletions includes/core-phpunit/includes/unregister-blocks-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
remove_action( 'init', 'register_block_core_archives' );
remove_action( 'init', 'register_block_core_avatar' );
remove_action( 'init', 'register_block_core_block' );
remove_action( 'init', 'register_block_core_button' );
remove_action( 'init', 'register_block_core_calendar' );
remove_action( 'init', 'register_block_core_categories' );
remove_action( 'init', 'register_block_core_comment_author_name' );
Expand Down
2 changes: 0 additions & 2 deletions src/Adapters/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function __construct(
// @phpstan-ignore-next-line
$this->inheritEnvironmentVariables(true);
}

parent::__construct($command, $cwd, $env, $input, $timeout);
}

public function getStartTime(): float
Expand Down
3 changes: 3 additions & 0 deletions src/Extension/ChromeDriverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ private function getPort(): int
return (int)($config['port'] ?? 4444);
}

/**
* @throws ExtensionException
*/
private function getBinary(): ?string
{
$config = $this->config;
Expand Down
237 changes: 237 additions & 0 deletions src/Extension/MysqlServerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php

declare(strict_types=1);

namespace lucatume\WPBrowser\Extension;

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

class MysqlServerController extends ServiceExtension
{
use PidBasedController;

public const PID_FILE_NAME = 'mysql-server.pid';
/**
* @var \lucatume\WPBrowser\ManagedProcess\MysqlServer
*/
private $mysqlServer;

public function start(OutputInterface $output): void
{
$pidFile = $this->getPidFile();

if (is_file($pidFile)) {
$output->writeln('MySQL server already running.');

return;
}

$port = $this->getPort();
$database = $this->getDatabase();
$user = $this->getUser();
$password = $this->getPassword();
$binary = $this->getBinary();
$shareDir = $this->getShareDir($binary);

$output->write("Starting MySQL server on port $port ...");
try {
$this->mysqlServer = new MysqlServer(
codecept_output_dir('_mysql_server'),
$port,
$database,
$user,
$password,
$binary,
$shareDir
);
$this->mysqlServer->setOutput($output);
$this->mysqlServer->start();
} catch (\Exception $e) {
throw new ExtensionException($this, "Error while starting MySQL server. {$e->getMessage()}", $e);
}
$output->write(' ok', true);
}

public function getPidFile(): string
{
return codecept_output_dir(self::PID_FILE_NAME);
}

private function getDatabase(): string
{
/** @var array{database?: string} $config */
$config = $this->config;

if (isset($config['database']) && !(is_string($config['database']) && !empty($config['database']))) {
throw new ExtensionException(
$this,
'The "database" configuration option must be a string.'
);
}

return $config['database'] ?? 'wordpress';
}

private function getUser(): string
{
/** @var array{user?: string} $config */
$config = $this->config;

if (isset($config['user']) && !(is_string($config['user']) && !empty($config['user']))) {
throw new ExtensionException(
$this,
'The "user" configuration option must be a string.'
);
}

return $config['user'] ?? 'wordpress';
}

private function getPassword(): string
{
/** @var array{password?: string} $config */
$config = $this->config;

if (isset($config['password']) && !is_string($config['password'])) {
throw new ExtensionException(
$this,
'The "password" configuration option must be a string.'
);
}

return $config['password'] ?? 'wordpress';
}

/**
* @throws ExtensionException
*/
public function getPort(): int
{
$config = $this->config;
if (isset($config['port'])
&& !(
is_numeric($config['port'])
&& (int)$config['port'] == $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 */
return (int)($config['port'] ?? 8906);
}

public function stop(OutputInterface $output): void
{
$pidFile = $this->getPidFile();
$mysqlServerPid = (int)file_get_contents($pidFile);

if (!$mysqlServerPid) {
$output->writeln('MySQL server not running.');
return;
}

$output->write("Stopping MySQL server with PID $mysqlServerPid ...", false);
$this->kill($mysqlServerPid);
$this->removePidFile($pidFile);
$output->write(' ok', true);
}

public function getPrettyName(): string
{
return 'MySQL Community Server';
}

/**
* @return array{
* running: string,
* pidFile: string,
* port: int
* }
* @throws ExtensionException
*/
public function getInfo(): array
{
$isRunning = is_file($this->getPidFile());

$info = [
'running' => $isRunning ? 'yes' : 'no',
'pidFile' => Filesystem::relativePath(codecept_root_dir(), $this->getPidFile()),
'host' => '127.0.0.1',
'port' => $this->getPort(),
'user' => $this->getUser(),
'password' => $this->getPassword(),
'root user' => 'root',
'root password' => $this->getUser() === 'root' ? $this->getPassword() : ''
];

if ($isRunning) {
$info['mysql command'] = $this->getCliConnectionCommandline();
$info['mysql root command'] = $this->getRootCliConnectionCommandline();
}

return $info;
}

private function getCliConnectionCommandline(): string
{
if ($this->getPassword() === '') {
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u {$this->getUser()}";
}

return "mysql -h 127.0.0.1 -P {$this->getPort()} -u {$this->getUser()} -p '{$this->getPassword()}'";
}

private function getRootCliConnectionCommandline(): string
{
$rootPassword = $this->getUser() === 'root' ? $this->getPassword() : '';
if ($rootPassword === '') {
return "mysql -h 127.0.0.1 -P {$this->getPort()} -u root";
}

return "mysql -h 127.0.0.1 -P {$this->getPort()} -u root -p '{$rootPassword}'";
}

private function getBinary(): ?string
{
$config = $this->config;
if (isset($config['binary']) && !(is_string($config['binary']) && is_executable($config['binary']))) {
throw new ExtensionException(
$this,
'The "binary" configuration option must be an executable file.'
);
}

/** @var array{binary?: string} $config */
return ($config['binary'] ?? null);
}

private function getShareDir(?string $binary): ?string
{
/** @var array{shareDir?: string} $config */
$config = $this->config;
if (isset($config['shareDir']) && !(is_string($config['shareDir']) && is_dir($config['shareDir']))) {
throw new ExtensionException(
$this,
'The "shareDir" configuration option must be a directory.'
);
}

$shareDir = $config['shareDir'] ?? null;

if ($binary && $shareDir === null) {
throw new ExtensionException(
$this,
'The "shareDir" configuration option must be set when using a custom binary.'
);
}

return $shareDir;
}
}
4 changes: 2 additions & 2 deletions src/ManagedProcess/ChromeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
/**
* @throws RuntimeException
*/
public function doStart(): void
private function doStart(): void
{
$command = array_merge([$this->chromeDriverBinary, '--port=' . $this->port], $this->arguments);
$process = new Process($command);
Expand All @@ -79,7 +79,7 @@ private function confirmStart(Process $process): void
$start = time();
$output = $process->getOutput();
while (time() < $start + 30) {
if (strpos($output, 'ChromeDriver was started successfully.') !== false) {
if (strpos($output, 'ChromeDriver was started successfully') !== false) {
return;
}
if ($process->getExitCode() !== null) {
Expand Down
3 changes: 2 additions & 1 deletion src/ManagedProcess/ManagedProcessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public function stop(): ?int
$exitCode = $process->stop();

if (is_file(static::getPidFile()) && !unlink(static::getPidFile())) {
$pidFile = static::getPidFile();
throw new RuntimeException(
"Could not remove PID file '{static::getPidFile(}'.",
"Could not remove PID file {$pidFile}.",
ManagedProcessInterface::ERR_PID_FILE_DELETE
);
}
Expand Down
Loading

0 comments on commit c2f29a2

Please sign in to comment.