Skip to content

Commit

Permalink
refactor(LoadSandbox) move debug information to exception
Browse files Browse the repository at this point in the history
Since the output of `codecept_debug` will only show if the user is
running the Codeception command using the `--debug` output, I've moved
the information that will help the user debug the issue to the exception
to make sure it will always print. I've changed the wording a bit to be
more general and cover other commands (e.g. the `console` one) that will
use the LoadSandbox class and might fail like the `run` command does.
Fix test.
  • Loading branch information
lucatume committed Oct 14, 2024
1 parent e18119f commit 95babfe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/WordPress/InstallationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public static function becauseWordPressMultsiteIsNotInstalled(bool $isSubdomainI
public static function becauseCodeceptionCommandDidNotFinish(): self
{
return new self(
"Codeception `run` command did not finish properly; WordPress exited early during sandbox installation. "
."A plugin, theme, or WP-CLI package may have exited before the wp_loaded action could run.",
"The current Codeception command did not finish properly. WordPress exited early while loading. "
."A plugin, theme, or WP-CLI package may have exited before the wp_loaded action could be fired. " .
"If there is error output above, it may provide clues.",
self::COMMAND_DID_NOT_FINISH_PROPERLY
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/WordPress/LoadSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,8 @@ class_exists(InstallationException::class);
}

if ($bodyContent === 'COMMAND DID NOT FINISH PROPERLY.') {
// we got here from \Codeception\Subscriber\ErrorHandler::shutdownHandler()
codecept_debug('Codeception error: ' .$bodyContent);
codecept_debug(
'DEBUG: Something caused WordPress to exit early. If there is error output above, it may provide clues. '
.'(For instance, a WP-CLI package mistakenly attempting to handle the `codecept run` command.)'
);
// We got here from \Codeception\Subscriber\ErrorHandler::shutdownHandler().
// We'll try to provide some clues to the user by adding some debug output.
throw InstallationException::becauseCodeceptionCommandDidNotFinish();
}

Expand Down
30 changes: 17 additions & 13 deletions tests/unit/lucatume/WPBrowser/WordPress/LoadSandboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace Unit\lucatume\WPBrowser\WordPress;

use Codeception\Subscriber\ErrorHandler;
use Codeception\Test\Unit;
use Exception;
use lucatume\WPBrowser\Process\ProcessException;
use lucatume\WPBrowser\Process\WorkerException;
use lucatume\WPBrowser\Tests\Traits\Fork;
use lucatume\WPBrowser\Tests\Traits\LoopIsolation;
use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup;
use lucatume\WPBrowser\Traits\UopzFunctions;
Expand Down Expand Up @@ -363,25 +361,31 @@ public function should_handle_codeception_command_not_finished_error(): void {
* @see \Codeception\Subscriber\ErrorHandler::shutdownHandler()
*/
add_action('after_setup_theme', function () {
// Output and exit from \Codeception\Subscriber\ErrorHandler::shutdownHandler
// Output and exit from \Codeception\Subscriber\ErrorHandler::shutdownHandler.
echo "\n\n\nCOMMAND DID NOT FINISH PROPERLY.\n";
exit(125);
});
PHP;

if (!file_exists($installation->getMuPluginsDir())) {
mkdir($installation->getMuPluginsDir());
$muPluginsDir = $installation->getMuPluginsDir();
if (
!is_dir($muPluginsDir)
&& !(
mkdir($muPluginsDir, 0755, true)
&& is_dir($muPluginsDir)
)
) {
throw new \RuntimeException('Could not create mu-plugins directory.');
}
if(!file_put_contents($muPluginsDir . '/exiting-mu-plugin.php', $exitingPluginCode)){
throw new \RuntimeException('Could not write exiting-mu-plugin.php.');
}
file_put_contents($installation->getMuPluginsDir() . '/exiting-mu-plugin.php', $exitingPluginCode);

$this->preventExit();

$this->expectException(InstallationException::class);
$this->expectExceptionMessageMatches('WordPress exited early during sandbox installation');
$this->expectExceptionMessage(InstallationException::becauseCodeceptionCommandDidNotFinish()->getMessage());

$loadSandbox = new LoadSandbox($wpRootDir, 'wordpress.test');

$this->assertInIsolation(static function () use ($loadSandbox) {
$this->assertInIsolation(static function () use ($wpRootDir) {
$loadSandbox = new LoadSandbox($wpRootDir, 'wordpress.test');
$loadSandbox->load();
});
}
Expand Down

0 comments on commit 95babfe

Please sign in to comment.