Testing
Unit testing is done using PHPUnit, it requires the "WordPress Test Suite" in order to be able to run tests against WordPress.
The framework simplifies the process of installing and putting all the pieces together with Ayuco commands.
Setup
It is important to understand the following key components before proceeding with the setup.
Component | Description |
PHPUnit | PHPUnit is a programmer-oriented testing framework for PHP. |
WordPress Test Suite | The suite is a library compatible with PHPUnit and provided by WordPress to implement unit testing. |
WP testing config file | Is a configuration file, very similar to wp-config.php , installed at the root of WordPress, and used to define the testing environment (such as the database connection, WordPress location, and default theme). |
phpunit.xml | PHPUnit configuration file, located inside your project's root folder, it defines where the test cases are located and how to bootstrap the execution. |
phpunit.json | WordPress MVC unit testing configuration file, located inside your project's root folder, defines where is the "WordPress Test Suite" and "WP testing config file" located. |
The setup command is used to set up unit testing:
php ayuco setup tests
The command above will call a setup wizard (in the command line) that will walk you through to configure unit testing in your project.
The wizard will download WordPress Test Suite and PHPUnit, and will put all pieces together to make unit testing functional.
When it finishes, the subfolder /tests
would have been created inside your project, alongside the setup and configuration of the components described in the table above.
Create a test case
Ayuco's create command is used to create a test case.
php ayuco create test:{handler}
{handler} | The handler can be the name of the test case (i.e. Hooks ) or the name of the test case and a test method (i.e. Hooks@test_yolo_filter ). |
Usage example:
php ayuco create test:Hooks@test_yolo_filter
The command above will create the file /tests/cases/HooksTest.php
and will create inside the test method test_yolo_filter()
.
Suffix
The command will always add "Test" as suffix.Once the test case is created, inside you will be able to use any of PHPUnit's or WordPress Test Suite functionality, for example:
class HooksTest extends WP_UnitTestCase { public function test_yolo_filter() { // Prepare and execute $string = apply_filters( 'yolo', 'A string value' ); // Assert $this->assertEquals( 'A string value [YOLO]', $string ); } }
Running the tests
Use PHPUnit's command to run unit testing (see their documentation for more information).
phpunit