Skip to content

Commit

Permalink
Cleaning up the bootstrap.php file (#26)
Browse files Browse the repository at this point in the history
* Cleaning up the bootstrap.php file

- Moving some of the conditionals and functionalities from bootstrap.php file into a separate class
- Moving the "require" bits into the Bootstrap::init() function
- Moving the test environment inclusion into a separate function
  • Loading branch information
aldavigdis authored Mar 22, 2024
1 parent 4b69ba2 commit a028cfb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
28 changes: 5 additions & 23 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,12 @@

require_once 'vendor/autoload.php';

# The WP test suite assumes the existence of classes that only exist in
# version 9 and older of PHPUnit. This solves that.
if ( 1 === version_compare( \PHPUnit\Runner\Version::id(), '10' ) ) {
require __DIR__ . '/supressors/SupressFramework.php';
require __DIR__ . '/supressors/SupressFrameworkError.php';
}

use Aldavigdis\WpTestsStrapon\Bootstrap;
use Aldavigdis\WpTestsStrapon\FetchWP;

if (getenv('WP_VERSION') === false) {
putenv('WP_VERSION=master');
}
use Aldavigdis\WpTestsStrapon\SetEnv;

if (defined('WP_TESTS_CONFIG_FILE_PATH') === false) {
define(
'WP_TESTS_CONFIG_FILE_PATH',
Aldavigdis\WpTestsStrapon\Config::path()
);
}
SetEnv::supress();
SetEnv::setWpVersion();
SetEnv::setConfigFilePath();

Bootstrap::init(getenv('WP_VERSION'));

require FetchWP::extractDirPath() . 'wordpress-develop-trunk/tests/phpunit/includes/functions.php';
ob_start();
require FetchWP::extractDirPath() . 'wordpress-develop-trunk/tests/phpunit/includes/bootstrap.php';
ob_end_clean();
Bootstrap::requireWordPressTestEnv();
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,21 @@ public static function init(string $wp_version): void
self::displaySeparator();
}

/**
* Require the WP test environment
*
* This enables us to use WordPress' built-in functions in our tests.
*/
public static function requireWordPressTestEnv(): void {
require FetchWP::extractDirPath() .
'wordpress-develop-trunk/tests/phpunit/includes/functions.php';

ob_start();
require FetchWP::extractDirPath() .
'wordpress-develop-trunk/tests/phpunit/includes/bootstrap.php';
ob_end_clean();
}

/**
* Get the width for the current terminal
*
Expand Down
49 changes: 49 additions & 0 deletions src/SetEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Aldavigdis\WpTestsStrapon;

use Aldavigdis\WpTestsStrapon\Config;

/**
* The SetEnv class initiates environment variables and global constants and
* helps us with going around limitations set by PHPUnit 9
*/
class SetEnv {
/**
* Supress PHPUnit framework errors if the current version of PHPUnit is
* version 10 or newer.
*
* This is because WordPress itself is stuck with version 9 of PHPUnit and
* thus assumes certain classes to exsist. If we don't fake them like this,
* the WP test suite will throw an error.
*/
public static function supress(): void {
if ( 1 === version_compare( \PHPUnit\Runner\Version::id(), '10' ) ) {
require __DIR__ . '/../supressors/SupressFramework.php';
require __DIR__ . '/../supressors/SupressFrameworkError.php';
}
}

/**
* Set the WP_VERSION environment variable if it is not set already
*/
public static function setWpVersion(): void {
if (getenv('WP_VERSION') === false) {
putenv('WP_VERSION=master');
}
}

/**
* Set the global WP_TESTS_CONFIG_FILE_PATH constant if not set already
*/
public static function setConfigFilePath(): void {
if (defined('WP_TESTS_CONFIG_FILE_PATH') === false) {
define(
'WP_TESTS_CONFIG_FILE_PATH',
Config::path()
);
}
}
}

0 comments on commit a028cfb

Please sign in to comment.