### PHP Integration Test Case Example Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md Demonstrates the basic implementation of a WordPress integration test class using WP Test Utils. It extends the provided `TestCase` and includes a `set_up` method for custom test setup, ensuring compatibility with WordPress core testing utilities. ```PHP stubTranslationFunctions(); // No HTML escaping will be applied. // Or: \Brain\Monkey\Functions\stubTranslationFunctions(); // HTML escaping will be applied. // Test your code. } public function testAFunctionContainingOutputEscaping() { $this->stubEscapeFunctions(); // No HTML escaping will be applied. // Or: \Brain\Monkey\Functions\stubEscapeFunctions(); // HTML escaping will be applied. // Test your code. } } ``` -------------------------------- ### PHP: Create Test Doubles for Unavailable Classes (Test Class) Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md This snippet shows how to create test doubles for unavailable classes within a PHP test class using `Yoast\WPTestUtils\BrainMonkey\TestCase::makeDoublesForUnavailableClasses` or `Yoast\WPTestUtils\BrainMonkey\TestCase::makeDoubleForUnavailableClass`. It includes an example of setting properties on a mocked 'WP_Post' object. ```PHP post_title = 'my test title'; $wp_post->post_type = 'my_custom_type'; $this->assertSame( 'expected', \function_under_test( $wp_post ) ); } } ``` -------------------------------- ### Bootstrap WordPress Integration Tests (PHP) Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md This snippet demonstrates how to use the WPIntegration bootstrap utilities to set up WordPress integration tests. It includes defining the plugin directory, setting active plugins, and calling the bootstrap function to load WordPress and necessary test components. ```php use Yoast\WPTestUtils\WPIntegration; if ( getenv( 'WP_PLUGIN_DIR' ) !== false ) { define( 'WP_PLUGIN_DIR', getenv( 'WP_PLUGIN_DIR' ) ); } $GLOBALS['wp_tests_options'] = [ 'active_plugins' => [ 'plugin-name/main-file.php' ], ]; require_once dirname( __DIR__ ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php'; /* * Bootstrap WordPress. This will also load the Composer autoload file, the PHPUnit Polyfills * and the custom autoloader for the TestCase and the mock object classes. */ WPIntegration\bootstrap_it(); if ( ! defined( 'WP_PLUGIN_DIR' ) || file_exists( WP_PLUGIN_DIR . '/plugin-name/main-file.php' ) === false ) { echo PHP_EOL, 'ERROR: Please check whether the WP_PLUGIN_DIR environment variable is set and set to the correct value. The integration test suite won't be able to run without it.', PHP_EOL; exit( 1 ); } ``` -------------------------------- ### BrainMonkey Bootstrap File for WordPress Testing Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md A bootstrap file for PHPUnit that defines essential WordPress constants and clears the PHP Opcache. This is crucial for testing WordPress plugins and themes, ensuring a consistent environment. ```PHP // File: tests/bootstrap.php require_once dirname( __DIR__ ) . '/vendor/yoast/wp-test-utils/src/BrainMonkey/bootstrap.php'; require_once dirname( __DIR__ ) . '/vendor/autoload.php'; ``` -------------------------------- ### Yoast TestCase for BrainMonkey Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md Extends YoastTestCase to leverage BrainMonkey's stubbing capabilities for WordPress functions. This provides a testing environment with pre-defined return values for common WordPress functions. ```PHP namespace PackageName\Tests; use Yoast\WPTestUtils\BrainMonkey\YoastTestCase; class FooTest extends YoastTestCase { // Your test code. } ``` -------------------------------- ### Update WP Test Utils with Composer Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md This command updates the WP Test Utils package to the latest version and also updates its dependencies. The `--with-dependencies` flag ensures that any changes in the package's dependencies are also handled. ```Bash composer update --dev yoast/wp-test-utils --with-dependencies ``` -------------------------------- ### PHP: Create Test Doubles for Unavailable Classes (Bootstrap) Source: https://github.com/yoast/wp-test-utils/blob/develop/README.md This snippet demonstrates how to use `Yoast\WPTestUtils\BrainMonkey\makeDoublesForUnavailableClasses` in a test bootstrap file to create fake test double classes for unavailable classes like 'WP_Post' and 'wpdb'. This is useful for setting up test doubles for an entire test suite. ```PHP