Helpers
Mantle provides some testing helpers to make it easier to write tests for your project.
HTML String
The Mantle\Testing\html_string
helper is used to create a DOMDocument object
from an HTML string. This can be useful when you need to test a function that
returns HTML output and want to make assertions about the generated HTML. You
can use the same methods as the Element Assertions
feature to make assertions about the HTML string.
use Mantle\Testing\html_string;
html_string(
'
<div>
<section>Example Section</section>
<div class="test-class">Example Div By Class</div>
<div id="test-id">Example Div By ID</div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li data-testid="test-item">Item 3</li>
</ul>
</div>
'
)
->assertContains( 'Example Section' )
->assertElementExists( '//section' )
->assertQuerySelectorExists( 'section' )
->assertElementExistsById( 'test-id' )
->assertElementExistsByTestId( 'test-item' );
The HTML string helper returns a HTML object.
Getting Output
\Mantle\Testing\Utils::get_echo()
is a replacement for the core WordPress test suite's get_echo()
function. It takes a callable, starts an output buffer, calls the given callable, and returns the contents of the output buffer. Example:
use Mantle\Testing\Utils;
$demo = Utils::get_echo( fn ( $subject ) => echo "Hello {$subject}!", 'world' );
$title = Utils::get_echo( 'the_title' );
$method = Utils::get_echo( [ 'My_Class', 'method_that_outputs' ] );