Skip to content

Commit

Permalink
Merge pull request #24 from humanmade/split-tmp-file-handling
Browse files Browse the repository at this point in the history
Codeception command enhancements
  • Loading branch information
shadyvb authored Jan 13, 2022
2 parents 94e4b3d + 656d579 commit 4944b00
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions inc/command/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected function codecept( InputInterface $input, OutputInterface $output ) {
*/
protected function run_command( InputInterface $input, OutputInterface $output, string $command, array $options = [] ) {
$use_chassis = $input->getOption( 'chassis' );
$cli = $this->getApplication()->find( $use_chassis ? 'chassis' : 'local-server' );
$cli = $this->getApplication()->find( $use_chassis ? 'chassis' : 'server' );

// Add the command, default options and input options together.
$options = array_merge(
Expand Down Expand Up @@ -562,6 +562,13 @@ protected function codecept_run( InputInterface $input, OutputInterface $output
$temp_run_file_path = $this->get_root_dir() . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . '.test-running';
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
file_put_contents( $temp_run_file_path, 'true' );
register_shutdown_function( function() use ( $temp_run_file_path ) {
unlink( $temp_run_file_path );
} );

// Track db and browser container creation.
$has_created_db = false;
$has_created_browser = false;

// Iterate over the suites.
foreach ( $suites as $suite ) {
Expand All @@ -571,20 +578,22 @@ protected function codecept_run( InputInterface $input, OutputInterface $output
$this->run_command( $input, $output, 'wp', [ 'cache', 'flush', '--quiet' ] );

// Create database if needed.
if ( $this->suite_has_module( 'vendor/' . $tests_folder . '/' . $suite . '.suite.yml', 'WPDb' ) ) {
if ( ! $has_created_db && $this->suite_has_module( 'vendor/' . $tests_folder . '/' . $suite . '.suite.yml', 'WPDb' ) ) {
$this->create_test_db( $input, $output );
$has_created_db = true;

register_shutdown_function( function() use ( $input, $output, $temp_run_file_path ) {
// Remove the db on shutdown.
register_shutdown_function( function() use ( $input, $output ) {
$output->write( '<info>Removing test databases..</info>', true, $output::VERBOSITY_NORMAL );
$this->delete_test_db( $input, $output );
unlink( $temp_run_file_path );
} );
}

// Run the headless browser container if needed.
if ( $this->suite_has_module( 'vendor/' . $tests_folder . '/' . $suite . '.suite.yml', 'WPWebDriver' ) ) {
if ( ! $has_created_browser && $this->suite_has_module( 'vendor/' . $tests_folder . '/' . $suite . '.suite.yml', 'WPWebDriver' ) ) {
// Start a new container.
$this->start_browser_container( $input, $output );
$has_created_browser = true;

// Stop the container on shutdown.
register_shutdown_function( function() use ( $input, $output ) {
Expand All @@ -608,7 +617,6 @@ protected function codecept_run( InputInterface $input, OutputInterface $output
return $return;
}


/**
* Run Codeception arbitrary commands.
*
Expand Down Expand Up @@ -648,7 +656,7 @@ protected function create_test_db( InputInterface $input, OutputInterface $outpu
],
] ), $output );
} else {
$cli = $this->getApplication()->find( 'local-server' );
$cli = $this->getApplication()->find( 'server' );

$return_val = $cli->run( new ArrayInput( [
'subcommand' => 'db',
Expand Down Expand Up @@ -687,7 +695,7 @@ protected function delete_test_db( InputInterface $input, OutputInterface $outpu
],
] ), $output );
} else {
$cli = $this->getApplication()->find( 'local-server' );
$cli = $this->getApplication()->find( 'server' );

$return_val = $cli->run( new ArrayInput( [
'subcommand' => 'db',
Expand Down

0 comments on commit 4944b00

Please sign in to comment.