WP_Mock Test Case
WP_Mock comes with a base test case class that provides a number of useful methods for testing WordPress plugins and themes.
This class is located in the WP_Mock\Tools\TestCase
namespace, and can be used by extending it in your test classes:
WP_Mock TestCase
extends PHPUnit own TestCase
so all methods and assertions from the latter are available in the former.
On top of those, WP_Mock TestCase
will include a collection of handy methods for helping you test your code.
Methods
Assert conditions met
The TestCase::assertConditionsMet()
function will assert that the current test conditions have been met. This is useful when your test assertions are purely WP_Mock expectations, and you don't want to have to call Mockery::close()
in your test, otherwise PHPUnit might raise a warning that no assertions were performed.
Assert equals HTML
The TestCase::assertEqualsHtml()
function will evaluate a string as HTML and compare it to another string. This is useful when you want to compare HTML strings that may have different formatting, but are otherwise identical.
Expect output string
The TestCase::expectOutputString()
function will assert that the output of a function matches a given string. This is useful when you want to test the output of a function that echoes HTML.
Mock static method
The TestCase::mockStaticMethod()
function will mock a static method on a class, via Patchwork, returning a Mockery object.
Access inaccessible class members
The following methods can be used to access methods or properties of classes that are inaccessible (private or protected), using Reflection:
Get or invoke an inaccessible method
Use the following methods to get and invoke an inaccessible (private or protected) method from a class through ReflectionMethod
:
Handle inaccessible properties
Similar to the methods above, there is a similar collection of test methods that will use ReflectionProperty
to manipulate inaccessible (private or protected) class properties:
Last updated