Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Setup PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Secchi committed Feb 28, 2024
1 parent efecbed commit 89f2780
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"plugin-zip": "wp-scripts plugin-zip",
"start": "wp-scripts start --webpack-copy-php",
"start:hot": "wp-scripts start --hot",
"test:unit": "wp-env run tests-cli --env-cwd=wp-content/plugins/write-poetry ./vendor/bin/phpunit",
"wp-env": "wp-env",
"env:start": "wp-env start && (npm run start)",
"env:start:debug": "wp-env start --xdebug",
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
Expand All @@ -7,8 +8,9 @@
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<testsuite name="testing">
<directory prefix="test-" suffix=".php">./tests/</directory>
<exclude>./tests/test-sample.php</exclude>
</testsuite>
</testsuites>
</phpunit>
21 changes: 14 additions & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* PHPUnit bootstrap file
* PHPUnit bootstrap file.
*
* @package Sample_Plugin
* @package WritePoetry
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
Expand All @@ -11,21 +11,28 @@
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?";
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
}

if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
require_once "{$_tests_dir}/includes/functions.php";

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __DIR__ ) . '/sample-plugin.php';
require dirname( dirname( __FILE__ ) ) . '/write-poetry.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
require "{$_tests_dir}/includes/bootstrap.php";
4 changes: 2 additions & 2 deletions tests/test-sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Class SampleTest
*
* @package Sample_Plugin
* @package Write_Poetry
*/

/**
Expand All @@ -13,7 +13,7 @@ class SampleTest extends WP_UnitTestCase {
/**
* A single example test.
*/
function test_sample() {
public function test_sample() {
// Replace this with some actual testing code.
$this->assertTrue( true );
}
Expand Down

0 comments on commit 89f2780

Please sign in to comment.