5
5
6
6
use Codeception \Test \Unit ;
7
7
use Exception ;
8
+ use lucatume \WPBrowser \Tests \Traits \Fork ;
8
9
use lucatume \WPBrowser \Tests \Traits \LoopIsolation ;
9
10
use lucatume \WPBrowser \Tests \Traits \TmpFilesCleanup ;
11
+ use lucatume \WPBrowser \Traits \UopzFunctions ;
10
12
use lucatume \WPBrowser \Utils \Env ;
11
13
use lucatume \WPBrowser \Utils \Filesystem as FS ;
12
14
use lucatume \WPBrowser \Utils \Random ;
@@ -24,6 +26,7 @@ class LoadSandboxTest extends Unit
24
26
{
25
27
use LoopIsolation;
26
28
use TmpFilesCleanup;
29
+ use UopzFunctions;
27
30
28
31
/**
29
32
* It should correctly load installed WordPress
@@ -322,4 +325,68 @@ public function should_handle_wp_die_called_during_loading(): void
322
325
$ loadSandbox ->load ();
323
326
});
324
327
}
328
+
329
+ /**
330
+ * It should handle an unexpected early exit if something interferes with Codeception
331
+ *
332
+ * @test
333
+ */
334
+ public function should_handle_codeception_command_not_finished_error (): void {
335
+ $ wpRootDir = FS ::tmpDir ('sandbox_ ' );
336
+ $ dbName = Random::dbName ();
337
+ $ dbHost = Env::get ('WORDPRESS_DB_HOST ' );
338
+ $ dbUser = Env::get ('WORDPRESS_DB_USER ' );
339
+ $ dbPassword = Env::get ('WORDPRESS_DB_PASSWORD ' );
340
+ $ db = new MysqlDatabase ($ dbName , $ dbUser , $ dbPassword , $ dbHost , 'wp_ ' );
341
+ $ installation = Installation::scaffold ($ wpRootDir , '6.1.1 ' )
342
+ ->configure ($ db )
343
+ ->install (
344
+ 'http://wordpress.test ' ,
345
+ 'admin ' ,
346
+ 'admin ' ,
347
+ 'admin@wordpress.test ' ,
348
+ 'Sandbox '
349
+ );
350
+
351
+ $ exitingPluginCode = <<<'PHP'
352
+ <?php
353
+ /**
354
+ * Plugin Name: Codeception Early Shutdown Mock
355
+ *
356
+ * Suppose a plugin or CLI package messes up and exits early, e.g. `exit(1)`, prior to `wp_loaded`.
357
+ * That will trigger Codeception's shutdown handler. If the suite is not finished running, and an error has not occurred,
358
+ * Codeception echoes a message and exits. LoadSandbox's output buffer will catch this.
359
+ * This plugin mocks the Codeception behavior.
360
+ *
361
+ * @see \Codeception\Subscriber\ErrorHandler::shutdownHandler()
362
+ */
363
+ add_action('after_setup_theme', function () {
364
+ // Output and exit from \Codeception\Subscriber\ErrorHandler::shutdownHandler.
365
+ echo "\n\n\nCOMMAND DID NOT FINISH PROPERLY.\n";
366
+ exit(125);
367
+ });
368
+ PHP;
369
+
370
+ $ muPluginsDir = $ installation ->getMuPluginsDir ();
371
+ if (
372
+ !is_dir ($ muPluginsDir )
373
+ && !(
374
+ mkdir ($ muPluginsDir , 0755 , true )
375
+ && is_dir ($ muPluginsDir )
376
+ )
377
+ ) {
378
+ throw new \RuntimeException ('Could not create mu-plugins directory. ' );
379
+ }
380
+ if (!file_put_contents ($ muPluginsDir . '/exiting-mu-plugin.php ' , $ exitingPluginCode )){
381
+ throw new \RuntimeException ('Could not write exiting-mu-plugin.php. ' );
382
+ }
383
+
384
+ $ this ->expectException (InstallationException::class);
385
+ $ this ->expectExceptionMessage (InstallationException::becauseCodeceptionCommandDidNotFinish ()->getMessage ());
386
+
387
+ $ this ->assertInIsolation (static function () use ($ wpRootDir ) {
388
+ $ loadSandbox = new LoadSandbox ($ wpRootDir , 'wordpress.test ' );
389
+ $ loadSandbox ->load ();
390
+ });
391
+ }
325
392
}
0 commit comments