PHP code example of sorge-it / phpunit-pest-html-assertions
1. Go to this page and download the library: Download sorge-it/phpunit-pest-html-assertions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sorge-it / phpunit-pest-html-assertions example snippets
// The total is empty on the page. The test stays green:
// it finds the marker, not the amount.
expect($html)->toContain('data-total');
// This one goes red and shows the region.
expect($html)->toHaveSelectorText('[data-total]', '42.00 EUR');
it('lists the items of the cart', function () {
expect($this->get('/cart'))
->toHaveSelectorCount('[data-cart] li', 3)
->toHaveAnySelectorText('[data-cart] li', 'Apple');
});
use SorgeIt\PhpunitPestHtmlAssertions\PHPUnit\AssertsHtml;
final class CartTest extends TestCase
{
use AssertsHtml;
public function test_it_lists_the_items_of_the_cart(): void
{
$response = $this->get('/cart');
self::assertHtmlSelectorCount($response, '[data-cart] li', 3);
self::assertHtmlAnySelectorTextSame($response, '[data-cart] li', 'Apple');
}
}
expect($response)->toHaveSelectorCount('[data-cart] li', 3, 'The cart keeps the items of the last visit.');
self::assertHtmlSelectorCount($response, '[data-cart] li', 3, 'The cart keeps the items of the last visit.');
use function SorgeIt\PhpunitPestHtmlAssertions\Pest\html;
// A region: the page holds it exactly once, or the test stops with NotOneNode. Every check after it looks inside it.
expect(html($page))->within('[data-cart]')
->toHaveSelectorCount('li', 3)
->not->toHaveSelector('[data-errors]');
// The srcdoc of an iframe, as a page of its own.
expect(html($page))->frame('iframe[data-preview]')->toHaveSelectorText('p', 'Hello');
// Values, in the order of the page.
expect(html($page))->texts('[data-cart] [data-name]')->toBe(['Apple', 'Pear']);
expect(html($page))->rawTexts('[data-note]')->toBe(["Line one\nLine two"]);
expect(html($page))->attributes('[data-item]', 'data-id')->toBe(['1', '2']);
// The same checks on every match. No match stops the test with NoMatch: a loop over nothing would check nothing.
// `:scope` is the match itself.
expect($page)->eachMatch('[data-avatar]', fn ($avatar) => $avatar->toHaveSelectorClass(':scope', 'rounded-full'));
// Every attribute value of the region, whatever the name of the attribute.
expect(html($page)->attributeValues())->each->not->toContain('javascript:');
expect($html)->toContain('lg:grid-cols-4'); // reported
expect(str_contains($html, 'lg:grid-cols-4'))->toBeTrue(); // reported
expect($html)->toHaveSelectorClass('[data-highlights]', 'lg:grid-cols-4'); // the element has the class
expect($rewritten)->toBe($expected); // @phpstan-ignore html.markupAsString (the rewriter keeps every other byte)
expect($script)->toContain('lg:hidden'); // @phpstan-ignore html.classAsString (the script adds the class)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.