ssddanbrown/asserthtml

HTML Content Assertions for PHPUnit

Fund package maintenance!
ssddanbrown

Installs: 88 624

Dependents: 2

Suggesters: 0

Security: 0

Stars: 7

Watchers: 3

Forks: 1

v3.0.0 2023-05-11 14:26 UTC

This package is auto-updated.

Last update: 2024-10-26 10:57:48 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.