idearia/wp-tests

A simple class to run PHPUnit tests in WordPress.

2.0.1 2023-02-02 13:57 UTC

This package is auto-updated.

Last update: 2024-04-30 00:28:39 UTC


README

Defines the class WordPressTestCase to run PHPUnit tests in a WordPress installation, with support for logging and multisite.

Usage

  1. Install with composer require idearia/wp-tests --dev.
  2. Create a test case by extending Idearia\WpTests\WordPressTestCase.
  3. When you run the test, WordPress will be automatically loaded.

Have a look in the the example folder. You'll find:

Logging support

  • To log a message to screen, call self::print( $message ).
  • To log a message to file, call self::log( $message ).
  • The file will be named after the test class and placed in the subfolder tests/logs.
  • The log file will reset at each run unless you set protected static $deleteLogFile = false; in your test case.
  • Customize the log folder via the logsPath environment variable.
  • For further customizations, please refer to the documentation of the Loggable trait.

Multisite support

By default, the tests will be run on the main blog.

To run the tests on a different blog, add the siteUrl environment variable to your phpunit.xml file:

<php>
  <env name="siteUrl" value=""/>
</php>

Then, you are free to set siteUrl the way you see fit:

  • In a dotenv file.
  • When you run phpunit: siteUrl=http://example.com/blog phpunit.
  • At the shell level: export siteUrl=http://example.com/blog.
  • In your test case setup: putenv( 'siteUrl=http://example.com/blog' );.
  • Hard-code it in phpunit.xml.

Custom WordPress path

If your WordPress installation is non-standard, you can specify a custom WordPress path by setting the wordPressPath environment variable. You can use both relative and absolute paths.

To set wordPressPath in phpunit.xml:

<php>
    <env name="wordPressPath" value="/path/to/wordpress/"/>
</php>