Do you write unit tests for your code? If you don't, you should. Unit tests are a fast, inexpensive, and efficient tool that helps guarantee code quality.

Why Do We Need Unit Tests?

The main purpose of unit tests is to test the smallest piece of a code, such as a function. Unit tests must focus on testing the system logic and avoid testing other function calls, database access, and external APIs. How do we do this without these calls? We can use mocks.

What is a Mock?

Mocking is used in unit tests when the unit being tested has external dependencies. Mock object simulates the external dependency behavior so that it is possible to focus the unit test on the logic of what is being tested. 

Mocking framework is a complement to unit testing framework. It helps developers build fast and efficient unit tests.

WP_Mock is an API mocking framework that enables unit tests in WordPress. It is very helpful because WordPress has a lot of classes, functions, and hooks, so it would be hard to write unit tests without WP_Mock.

Using WP_Mock

WP_Mock is installable as a composer dependency using this command:

composer require --dev 10up/wp_mock:0.4.2

After installation, you have to create a test class extending TestCase from WP_Mock. The function setUp is responsible for running all necessary configurations before each test case, and it is common to add a require_once call from the class that is being tested.  After each test, tearDown function is called, and you'll also be able to add some scripts to run in this portion of code, as shown below:

class Your_Class_Test extends WP_Mock\Tools\TestCase {



public function setUp() {

\WP_Mock::setUp();

}



public function tearDown() {

\WP_Mock::tearDown();

}

}
Mocking WordPress Core Functions

You can also mock WordPress core functions using \WP_Mock::userFunction(). This function must receive as an argument the function that is being mocked. It's also optional to specify how many times it should be called, the arguments it should receive, and what is being returned.

Let’s imagine that you are testing a function that uses the WordPress get_post function. The code below is an example of when function get_post is called once, with array(1) as parameter, and the return being a post object:

public function test_uses_get_post() {

$post = new \stdClass;

$post->ID = 1;




\WP_Mock::userFunction( 'get_post', array(

'times' => 1,

'args' => array( $post->ID ),

'return' => $post,

) );

}

WordPress objects can be mocked using another framework called Mockery, which is a dependency from WP_Mock. It is useful to mock calls to wpdb or WP_Query without accessing the database itself. The example below mocks WP_Query object and asserts that function have_posts returns false:

public function test_wp_query(): void {

$wp_query = Mockery::mock(WP_Query' );

$wp_query->shouldReceive( 'have_posts' )->andReturn( false );

}
Conclusion

In today's environment, unit testing is an essential part of a good development process, especially since it's fast to run and ensures that your code is reliable. WP_Mock is a framework that helps developers build tests in WordPress, providing tools to mock core functions and objects. WP_Mock is especially helpful because it will likely reduce the time you spend building tests to improve performance.

 

References

"What is Unit Testing?" SmartBear.

"Unit Testing for Software Craftsmanship." Telerik.

WordPress API Mocking Framework. GitHub.


Author

Luiz Henrique Santos

Luiz Henrique Santos is a Full Stack PHP Developer at Avenue Code with 7+ years of experience in IT. He always tries to understand functionalities to propose the best solution while working to deadlines. He applies SOLID standards, develops code that is easy to read and change, and writes unit tests for everything he does!


How to Build Unit Tests Using Jest

READ MORE

How Heuristics Can Improve Your Tests

READ MORE

How to Use WireMock for Integration Testing

READ MORE

The Best Way to Use Request Validation in Laravel REST API

READ MORE