Mocking WordPress Functions
Mocking WordPress core functions
namespace MyPlugin;
class MyClass
{
public function myFunction(int $postId) : string
{
$post = get_post($postId);
return $post ? $post->post_content : 'Post not found';
}
}use MyPlugin\MyClass;
use stdClass
use WP_Mock;
final class MyClassTest extends WP_Mock\Tools\TestCase
{
public function testMyFunction() : void
{
$post = new stdClass();
$post->post_content = 'Hello World';
WP_Mock::userFunction('get_post')
->once()
->with(123)
->andReturn($post);
$this->assertSame('Hello World', (MyClass::myFunction(123));
}
}Using Mockery expectations
Using expectations in arguments
Arguments
Examples
Times
Return
Example
Return in order
Example
Return argument
Example
Last updated