Skip to content

Commit

Permalink
WIP WordPress test
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Mar 12, 2024
1 parent f294c56 commit 1e17256
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 23 deletions.
12 changes: 6 additions & 6 deletions config/scripts/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}

download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
Expand Down Expand Up @@ -186,4 +186,4 @@ install_db() {

install_wp
install_test_suite
install_db
install_db
4 changes: 2 additions & 2 deletions phpunit-wp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
cacheResult="false"
backupGlobals="false"
stopOnError="false"
bootstrap="./tests/Unit/Bootstrap.php"
bootstrap="./tests/WordPress/Bootstrap.php"
processIsolation="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
cacheDirectory="var/cache/phpunit/wordPress"
backupStaticProperties="false"
requireCoverageMetadata="true">
<testsuites>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
bootstrap="./tests/Unit/Bootstrap.php"
processIsolation="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
cacheDirectory="var/cache/phpunit/unit"
backupStaticProperties="false"
requireCoverageMetadata="true">
<testsuites>
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class Bootstrap

public function __construct()
{
$this->wpTestsDir = getenv( 'WP_TESTS_DIR' )
? getenv( 'WP_TESTS_DIR' )
$this->wpTestsDir = getenv('WP_TESTS_DIR')
? getenv('WP_TESTS_DIR')
: sys_get_temp_dir() . '/wordpress-tests-lib';

/**
* Load PHPUnit Polyfills for the WP testing suite.
* @see https://github.com/WordPress/wordpress-develop/pull/1563/
*/
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', __DIR__ . '/../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php' );
define('WP_TESTS_PHPUNIT_POLYFILLS_PATH', __DIR__ . '/../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php');

/**
* Load the WP testing environment.
Expand Down
93 changes: 82 additions & 11 deletions tests/WordPress/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,102 @@ class Bootstrap
{
private static ?self $instance = null;

protected string $wpDirectory = '';

/**
* Directory where wordpress-tests-lib is installed.
* @var string
* @var string|null
*/
protected string $wpTestsDir = '';
protected ?string $wpTestsDir = null;

public function __construct()
{
$this->wpTestsDir = getenv( 'WP_TESTS_DIR' )
? getenv( 'WP_TESTS_DIR' )
: sys_get_temp_dir() . '/wordpress-tests-lib';
$this->wpTestsDir = $this->getPathToWpTestDir();
$this->initBoostrap();
}

/**
* @param string $path
* @return string
*/
private function normalizePath(string $path): string
{
return \str_replace('\\', '/', $path);
}

/**
* @return string|null
*/
protected function getPathToWpTestDir(): ?string
{
if (\getenv('WP_TESTS_DIR') !== false) {
$testsDir = \getenv('WP_TESTS_DIR');
$testsDir = \realpath($testsDir);

if ($testsDir !== false) {
$testsDir = $this->normalizePath($testsDir) . '/';
if (\is_dir($testsDir) === true
&& @\file_exists($testsDir . 'includes/bootstrap.php')
) {
return $testsDir;
}
}

unset($testsDir);
}

if (\getenv('WP_DEVELOP_DIR') !== false) {
$devDir = \getenv('WP_DEVELOP_DIR');
$devDir = \realpath($devDir);
if ($devDir !== false) {
$devDir = $this->normalizePath($devDir) . '/';
if (\is_dir($devDir) === true
&& @\file_exists($devDir . 'tests/phpunit/includes/bootstrap.php')
) {
return $devDir . 'tests/phpunit/';
}
}

unset($devDir);
}

/**
* Last resort: see if this is a typical WP-CLI scaffold command set-up where a subset of
* the WP test files have been put in the system temp directory.
*/
$testsDir = \sys_get_temp_dir() . '/wordpress-tests-lib';
$testsDir = \realpath($testsDir);
if ($testsDir !== false) {
$testsDir = $this->normalizePath($testsDir) . '/';
if (\is_dir($testsDir) === true
&& @\file_exists($testsDir . 'includes/bootstrap.php')
) {
return $testsDir;
}
}

return null;
}

/**
* @return void
*/
protected function initBoostrap(): void
{
if ($this->wpTestsDir === null) {
echo \PHP_EOL, 'ERROR: The WordPress native unit test bootstrap file could not be found. Please set either the WP_TESTS_DIR or the WP_DEVELOP_DIR environment variable, either in your OS or in a custom phpunit.xml file.', \PHP_EOL;
exit(1);
}

/**
* Load PHPUnit Polyfills for the WP testing suite.
* @see https://github.com/WordPress/wordpress-develop/pull/1563/
* @todo Maybe check if composer installed
*/
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', __DIR__ . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php');
require_once __DIR__ . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

/**
* We can safely load the bootstrap - already verifies it exists.
* Load the WP testing environment.
*/
require_once $this->wpTestsDir . '/includes/bootstrap.php';

require_once sprintf('%s/includes/bootstrap.php', rtrim($this->wpTestsDir, '/'));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/WordPress/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Copyright (c) 2024 Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Tests\WordPress;

Expand Down

0 comments on commit 1e17256

Please sign in to comment.