idearia / wp-tests
A simple class to run PHPUnit tests in WordPress.
Installs: 1 100
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- coccoinomane/phpunit-log: ^1.0.3
Requires (Dev)
- coccoinomane/phpunit-log: ^1.0
- phpstan/extension-installer: ^1.1
- phpunit/phpunit: ^9.5
- szepeviktor/phpstan-wordpress: ^1.1
README
Defines the class WordPressTestCase
to run PHPUnit tests in a WordPress installation, with support for logging and multisite.
Usage
- Install with
composer require idearia/wp-tests --dev
. - Create a test case by extending
Idearia\WpTests\WordPressTestCase
. - When you run the test, WordPress will be automatically loaded.
Have a look in the the example folder. You'll find:
- an example test that creates, fetches and deletes a WordPress post;
- an example phpunit.xml file with the available options.
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>