WordPressMVC

  1. Get Started
  2. Documentation
  3. Tutorials
  4. Blog
  1. Documentation
  2. Tutorials
v1.0.8 Login
  • Blog
  • Download
  • Get Started
  • The Basics
    • Ayuco
    • Main Class
    • Configuration
  • Hooks
    • Actions
    • Filters
    • Widgets
    • Shortcodes
  • MVC
    • Models
    • Post Models
    • Option Models
    • Term Models
    • User Models
    • Views
    • Controllers
  • Resources
    • Assets
    • Styles
    • Scripts
  • Advanced
    • Request
    • Response
    • Localization
    • Cache
    • Logger
    • Database
    • Testing
    • Deployment
    • Functions
    • Add-ons

Testing

Last updated: October 5, 2020

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.

ComponentDescription
PHPUnitPHPUnit is a programmer-oriented testing framework for PHP.
WordPress Test SuiteThe suite is a library compatible with PHPUnit and provided by WordPress to implement unit testing.
WP testing config fileIs 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.xmlPHPUnit configuration file, located inside your project's root folder, it defines where the test cases are located and how to bootstrap the execution.
phpunit.jsonWordPress 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
© 2023 10 Quality Studio . All rights reserved.
Search in: