PHPackages                             wmde/hamcrest-html-matchers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. wmde/hamcrest-html-matchers

ActiveLibrary[Testing &amp; Quality](/categories/testing)

wmde/hamcrest-html-matchers
===========================

Set of Hamcrest matchers for HTML assertions

v1.1.0(1y ago)2704.6k↑42.2%1[2 issues](https://github.com/wmde/hamcrest-html-matchers/issues)[4 PRs](https://github.com/wmde/hamcrest-html-matchers/pulls)2LGPL-2.1PHPPHP &gt;=7.4

Since Feb 9Pushed 1y ago19 watchersCompare

[ Source](https://github.com/wmde/hamcrest-html-matchers)[ Packagist](https://packagist.org/packages/wmde/hamcrest-html-matchers)[ RSS](/packages/wmde-hamcrest-html-matchers/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (6)Versions (9)Used By (2)

This is the set of Hamcrest matchers for HTML assertions
========================================================

[](#this-is-the-set-of-hamcrest-matchers-for-html-assertions)

[![Build Status](https://github.com/wmde/hamcrest-html-matchers/actions/workflows/php.yml/badge.svg)](https://github.com/wmde/hamcrest-html-matchers/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4080b909ef6bd42a5a64ed9a95c7e166f51279af4b05015db92646a7519d1361/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f68616d63726573742d68746d6c2d6d617463686572732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/wmde/hamcrest-html-matchers/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/90dc66a9b2dd04412de53e730dd26a2f4384e4c5a4e249e33e062945a2fc1201/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f68616d63726573742d68746d6c2d6d617463686572732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/wmde/hamcrest-html-matchers/?branch=master)

Usage examples
--------------

[](#usage-examples)

Hamcrest allows you to create pretty complicated and flexible assertions. Just remember:

*"You can" does not mean "you should".*

The following example shows how we can ensure that there is an HTML form and password entered in it is not weak:

```
$html = '

         ';

assertThat($html, is(htmlPiece(
    havingChild(
        both(withTagName('form'))
        ->andAlso(
            havingDirectChild(
                allOf(
                    withTagName('input'),
                    withAttribute('name')->havingValue('password'),
                    withAttribute('value')->havingValue(not('weak password')))))))));
```

Usage limitations:

- Each HTML assertion starts with `htmlPiece()` call (`is()` can be used to improve readability)
- One of `havingRootElement()`, `havingDirectChild()` or `havingChild()` needed to be passed as an argument to `htmlPiece()`

Documentation
-------------

[](#documentation)

Information about general Hamcrest usage can be found at [Hamcrest GitHub repository](https://github.com/hamcrest/hamcrest-php).

Available Matchers
------------------

[](#available-matchers)

- `htmlPiece()` - checks that string is a valid HTML, parses it and passes control to given matcher if one present

    ```
    assertThat('', is(htmlPiece())); // Just checking that string is a valid piece of HTML
    ```
- `havingRootElement` - checks given constraint against root element of HTML. *NOTE: Can be passed only to `htmlPiece()`*

    ```
    assertThat('', htmlPiece(havingRootElement(withTagName('p'))));
    ```
- `havingDirectChild` - checks given constraint against direct children

    ```
    assertThat('', htmlPiece(havingRootElement(havingDirectChild(withTagName('b')))));
    ```
- `havingChild` - checks given constraint against all children

    ```
    assertThat('', htmlPiece(havingChild(withTagName('b'))));
    ```
- `withTagName` - checks given constraint against tag name

    ```
    assertThat('', htmlPiece(havingChild(withTagName(
        either(equalTo('i'))->orElse(equalTo('b'))
    ))));
    ```
- `withAttribute` - checks given constraint against elements attributes comparing names and values

    ```
    assertThat('', htmlPiece(havingChild(withAttribute('required'))));
    ```

    ```
    assertThat('', htmlPiece(havingChild(
        withAttribute(startsWith('data-'))->havingValue('some data'))));
    ```
- `withClass` - checks given constraint against element's class list

    ```
    assertThat('', htmlPiece(havingChild(
          withClass('class2'))));
    ```
- `havingTextContents` - checks given constraint against elements text contents

    ```
    assertThat('this is Some Text', htmlPiece(havingChild(
        havingTextContents(containsString('some text')->ignoringCase()))));
    ```
- `tagMatchingOutline` - tolerantly checks that tag matches given *outline* (*outline* - tag representation in HTML format)

    That means:

    - Element's tag name is equal to outline's tag name
    - Element has all the attributes that outline has with the same values. If element has more attributes than outline it still matches.
        - **NOTE:** Attribute `class` is treated in a different manner (see further).
        - **NOTE:** If attribute outlined is boolean, then its value in element won't be checked, just presence.
    - Element has all html classes that outline has.

    This will pass:

    ```
    assertThat('',
        htmlPiece(havingChild(
            tagMatchingOutline('')
      )));
    ```

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~955 days

Total

4

Last Release

565d ago

Major Versions

v0.1.1 → v1.0.02021-03-22

PHP version history (3 changes)v0.1.0PHP &gt;=5.5

v1.0.0PHP &gt;=7.2

v1.1.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/054adb441e7ee248ec924bc45fa793835c284710eb31627587fa5de21bab9e96?d=identicon)[wmde](/maintainers/wmde)

![](https://www.gravatar.com/avatar/fe4220e5a109212ea1a84969fbcb0795ceaf975145c1f0577bd758d0500e6428?d=identicon)[manicki](/maintainers/manicki)

---

Top Contributors

[![bekh6ex](https://avatars.githubusercontent.com/u/4264331?v=4)](https://github.com/bekh6ex "bekh6ex (47 commits)")[![reedy](https://avatars.githubusercontent.com/u/67615?v=4)](https://github.com/reedy "reedy (17 commits)")[![thiemowmde](https://avatars.githubusercontent.com/u/6576639?v=4)](https://github.com/thiemowmde "thiemowmde (8 commits)")[![mariushoch](https://avatars.githubusercontent.com/u/2446964?v=4)](https://github.com/mariushoch "mariushoch (8 commits)")[![lucaswerkmeister](https://avatars.githubusercontent.com/u/2346599?v=4)](https://github.com/lucaswerkmeister "lucaswerkmeister (7 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![WMDE-Fisch](https://avatars.githubusercontent.com/u/8479008?v=4)](https://github.com/WMDE-Fisch "WMDE-Fisch (3 commits)")[![Ladsgroup](https://avatars.githubusercontent.com/u/5351225?v=4)](https://github.com/Ladsgroup "Ladsgroup (3 commits)")[![addshore](https://avatars.githubusercontent.com/u/3308769?v=4)](https://github.com/addshore "addshore (2 commits)")

---

Tags

assertassertion-libraryhamcresthtmlhtml-assertrionsmatchertesting

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wmde-hamcrest-html-matchers/health.svg)

```
[![Health](https://phpackages.com/badges/wmde-hamcrest-html-matchers/health.svg)](https://phpackages.com/packages/wmde-hamcrest-html-matchers)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k526.2M26.9k](/packages/mockery-mockery)[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M361](/packages/dms-phpunit-arraysubset-asserts)[danrevah/shortifypunit

PHP Mocking framework

334.2k1](/packages/danrevah-shortifypunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
