ssddanbrown/asserthtml
HTML Content Assertions for PHPUnit
Maintainers
Requires
- php: >=8.1
- ext-dom: *
- ext-json: *
- phpunit/phpunit: ^10.0|^11.0|^12.0|^13.0
- symfony/css-selector: ^6.0|^7.0
- symfony/dom-crawler: ^6.0|^7.0
Requires (Dev)
- phpstan/phpstan: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 0811b5c8d541f344c193bd7f2c2d79d13d23b141
This package is auto-updated.
Last update: 2026-06-04 15:12:54 UTC
README
👁 phpunit
👁 Latest Stable Version
👁 Total Downloads
Introduction
This package provides a class with a range of PHPUnit assertions for testing HTML content. This is heavily inspired, and uses code from, Laravel's browser-kit-testing package.
Installation
You can install the package via composer:
composer require ssddanbrown/asserthtml
Usage
use Ssddanbrown\AssertHtml\HtmlTest; // Create an HtmlTest instance with html content $html = new HtmlTest('<p>Hello</p>'); // Make assertions against the instance $html->assertElementContains('p', 'Hello');
Available Methods
Assertion Methods
$html->assertElementExists(string $selector, array $attributes = []): self $html->assertElementNotExists(string $selector, array $attributes = []): self $html->assertElementCount(string $selector, int $count): self $html->assertElementContains(string $selector, string $text): self $html->assertElementNotContains(string $selector, string $text): self $html->assertLinkExists(string $url, string $text = null): self $html->assertLinkNotExists(string $url, string $text = null): self $html->assertFieldHasValue(string $fieldNameOrId, string $expected): self $html->assertFieldNotHasValue(string $fieldNameOrId, string $value): self $html->assertFieldHasSelected(string $fieldNameOrId, string $value): self $html->assertFieldNotHasSelected(string $fieldNameOrId, string $value): self $html->assertCheckboxChecked(string $inputNameOrId): self $html->assertCheckboxNotChecked(string $inputNameOrId): self
Helper Methods
$html->getOuterHtml(?string $selector = null): string $html->getInnerHtml(string $selector): string
Laravel Usage
A simple trait is included for easier usage within Laravel project. Use the trait in your base TestCase class, or within specific test class files, and access by passing any TestResponse's to $this->withHtml($response);
Adding the trait
<?php ... use Ssddanbrown\AssertHtml\TestsHtml; ... abstract class TestCase extends BaseTestCase { ... use TestsHtml; ... }
Making assertions
<?php class HtmlTest extends TestCase { public function test_login_has_header() { $response = $this->get('/login'); $this->withHtml($response)->assertElementContains('h1#title', 'Login to my app!'); } }
Development
Psalm is included for static analysis. It can be run like so:
vendor/bin/phpstan
PHPUnit is used for testing. It can be run like so:
./vendor/bin/phpunit
Low Maintenance Project
This is a low maintenance project. The scope of features and support are purposefully kept narrow to my requirements to ensure longer term maintenance is viable. I'm not looking to grow this into a bigger project at all.
Issues and PRs raised for bugs are perfectly fine assuming they don't significantly increase the scope of the project. Please don't open PRs for significant new features.
License
This project is licensed under the MIT License. See the license file for more info.
Much of the logic used in this project has been taken from the laravel/browser-kit-testing project so is subject to their license also.
