{"id":15226383,"url":"https://github.com/stevegrunwell/phpunit-markup-assertions","last_synced_at":"2025-08-19T04:31:33.844Z","repository":{"id":52451783,"uuid":"108187051","full_name":"stevegrunwell/phpunit-markup-assertions","owner":"stevegrunwell","description":"Assertions for PHPUnit to verify the presence or state of elements within markup","archived":false,"fork":false,"pushed_at":"2024-06-18T10:21:48.000Z","size":122,"stargazers_count":15,"open_issues_count":4,"forks_count":3,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2024-12-07T11:22:03.307Z","etag":null,"topics":["php","phpunit","testing","xpath"],"latest_commit_sha":null,"homepage":"https://stevegrunwell.com/blog/phpunit-markup-assertions/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevegrunwell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["stevegrunwell"]}},"created_at":"2017-10-24T21:43:36.000Z","updated_at":"2024-10-08T15:14:20.000Z","dependencies_parsed_at":"2023-11-16T02:30:58.402Z","dependency_job_id":"37226fef-6b1c-4079-905d-b2c274a8c6b7","html_url":"https://github.com/stevegrunwell/phpunit-markup-assertions","commit_stats":{"total_commits":71,"total_committers":6,"mean_commits":"11.833333333333334","dds":0.5492957746478873,"last_synced_commit":"d36263169025bc99e76b5b30ca9b73c47fd7f1f3"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fphpunit-markup-assertions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fphpunit-markup-assertions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fphpunit-markup-assertions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fphpunit-markup-assertions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevegrunwell","download_url":"https://codeload.github.com/stevegrunwell/phpunit-markup-assertions/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230113562,"owners_count":18175214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","phpunit","testing","xpath"],"created_at":"2024-09-28T20:04:54.172Z","updated_at":"2024-12-18T18:07:37.176Z","avatar_url":"https://github.com/stevegrunwell.png","language":"PHP","funding_links":["https://github.com/sponsors/stevegrunwell"],"categories":[],"sub_categories":[],"readme":"# PHPUnit Markup Assertions\n\n![Build Status](https://github.com/stevegrunwell/phpunit-markup-assertions/workflows/Unit%20Tests/badge.svg)\n[![Code Coverage](https://coveralls.io/repos/github/stevegrunwell/phpunit-markup-assertions/badge.svg?branch=develop)](https://coveralls.io/github/stevegrunwell/phpunit-markup-assertions?branch=develop)\n[![GitHub Release](https://img.shields.io/github/release/stevegrunwell/phpunit-markup-assertions.svg)](https://github.com/stevegrunwell/phpunit-markup-assertions/releases)\n\nThis library introduces the `MarkupAssertionsTrait` trait for use in [PHPUnit](https://phpunit.de) tests.\n\nThese assertions enable you to inspect generated markup without having to muddy tests with [`DOMDocument`](http://php.net/manual/en/class.domdocument.php) or nasty regular expressions. If you're generating markup at all with PHP, the PHPUnit Markup Assertions trait aims to make the output testable without making your tests fragile.\n\n## Example\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse SteveGrunwell\\PHPUnit_Markup_Assertions\\MarkupAssertionsTrait;\n\nclass MyUnitTest extends TestCase\n{\n    use MarkupAssertionsTrait;\n\n    /**\n     * Ensure the #first-name and #last-name selectors are present in the form.\n     */\n    public function testRenderFormContainsInputs()\n    {\n        $markup = render_form();\n\n        $this-\u003eassertContainsSelector('#first-name', $markup);\n        $this-\u003eassertContainsSelector('#last-name', $markup);\n    }\n}\n```\n\n## Installation\n\nTo add PHPUnit Markup Assertions to your project, first install the library via Composer:\n\n```sh\n$ composer require --dev stevegrunwell/phpunit-markup-assertions\n```\n\nNext, import the `SteveGrunwell\\PHPUnit_Markup_Assertions\\MarkupAssertionsTrait` trait into each test case that will leverage the assertions:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse SteveGrunwell\\PHPUnit_Markup_Assertions\\MarkupAssertionsTrait;\n\nclass MyTestCase extends TestCase\n{\n    use MarkupAssertionsTrait;\n}\n```\n\n### Making PHPUnit Markup Assertions available globally\n\nIf you'd like the methods to be available across your entire test suite, you might consider [sub-classing the PHPUnit test case and applying the trait there](https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestCase):\n\n```php\n# tests/TestCase.php\n\nnamespace Tests;\n\nuse PHPUnit\\Framework\\TestCase as BaseTestCase;\nuse SteveGrunwell\\PHPUnit_Markup_Assertions\\MarkupAssertionsTrait;\n\nclass TestCase extends BaseTestCase\n{\n    use MarkupAssertionsTrait;\n}\n```\n\nThen update your other test cases to use your new base:\n\n```php\n# tests/Unit/ExampleTest.php\n\nnamespace Tests/Unit;\n\nuse Tests\\TestCase;\n\nclass MyUnitTest extends TestCase\n{\n    // This class now automatically has markup assertions.\n}\n```\n\n## Available methods\n\nThese are the assertions made available to PHPUnit via the `MarkupAssertionsTrait`.\n\n* [`assertContainsSelector()`](#assertcontainsselector)\n* [`assertNotContainsSelector()`](#assertnotcontainsselector)\n* [`assertSelectorCount()`](#assertselectorcount)\n* [`assertHasElementWithAttributes()`](#asserthaselementwithattributes)\n* [`assertNotHasElementWithAttributes()`](#assertnothaselementwithattributes)\n* [`assertElementContains()`](#assertelementcontains)\n* [`assertElementNotContains()`](#assertelementnotcontains)\n* [`assertElementRegExp()`](#assertelementregexp)\n* [`assertElementNotRegExp()`](#assertelementnotregexp)\n\n### assertContainsSelector()\n\nAssert that the given string contains an element matching the given selector.\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\npublic function testBodyContainsImage()\n{\n    $body = getPageBody();\n\n    $this-\u003eassertContainsSelector('img', $body, 'Did not find an image in the page body.');\n}\n```\n\n### assertNotContainsSelector()\n\nAssert that the given string does not contain an element matching the given selector.\n\nThis method is the inverse of [`assertContainsSelector()`](#assertcontainsselector).\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should not contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n### assertSelectorCount()\n\nAssert the number of times an element matching the given selector is found.\n\n\u003cdl\u003e\n    \u003cdt\u003e(int) $count\u003c/dt\u003e\n    \u003cdd\u003eThe number of matching elements expected.\u003c/dd\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup to run the assertion against.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\npublic function testPostList()\n{\n    factory(Post::class, 10)-\u003ecreate();\n\n    $response = $this-\u003eget('/posts');\n\n    $this-\u003eassertSelectorCount(10, 'li.post-item', $response-\u003egetBody());\n}\n```\n\n### assertHasElementWithAttributes()\n\nAssert that an element with the given attributes exists in the given markup.\n\n\u003cdl\u003e\n    \u003cdt\u003e(array) $attributes\u003c/dt\u003e\n    \u003cdd\u003eAn array of HTML attributes that should be found on the element.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain an element with the provided \u003ccode\u003e$attributes\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\npublic function testExpectedInputsArePresent()\n{\n    $user = getUser();\n    $form = getFormMarkup();\n\n    $this-\u003eassertHasElementWithAttributes(\n        [\n            'name' =\u003e 'first-name',\n            'value' =\u003e $user-\u003efirst_name,\n        ],\n        $form,\n        'Did not find the expected input for the user first name.'\n    );\n}\n```\n\n### assertNotHasElementWithAttributes()\n\nAssert that an element with the given attributes does not exist in the given markup.\n\n\u003cdl\u003e\n    \u003cdt\u003e(array) $attributes\u003c/dt\u003e\n    \u003cdd\u003eAn array of HTML attributes that should not be found on the element.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should not contain an element with the provided \u003ccode\u003e$attributes\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n### assertElementContains()\n\nAssert that the element with the given selector contains a string.\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $contents\u003c/dt\u003e\n    \u003cdd\u003eThe string to look for within the DOM node's contents.\u003c/dd\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\npublic function testColumnShowsUserEmail()\n{\n    $user = getUser();\n    $table = getTableMarkup();\n\n    $this-\u003eassertElementContains(\n        $user-\u003eemail,\n        'td.email',\n        $table,\n        'The \u003ctd class=\"email\"\u003e should contain the user\\'s email address.'\n    );\n}\n```\n\n### assertElementNotContains()\n\nAssert that the element with the given selector does not contain a string.\n\nThis method is the inverse of [`assertElementContains()`](#assertelementcontains).\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $contents\u003c/dt\u003e\n    \u003cdd\u003eThe string to look for within the DOM node's contents.\u003c/dd\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n### assertElementRegExp()\n\nAssert that the element with the given selector contains a string.\n\nThis method works just like [`assertElementContains()`](#assertelementcontains), but uses regular expressions instead of simple string matching.\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $regexp\u003c/dt\u003e\n    \u003cdd\u003eThe regular expression pattern to look for within the DOM node.\u003c/dd\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n\n### assertElementNotRegExp()\n\nAssert that the element with the given selector does not contain a string.\n\nThis method is the inverse of [`assertElementRegExp()`](#assertelementregexp) and behaves like [`assertElementNotContains()`](#assertelementnotcontains) except with regular expressions instead of simple string matching.\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $regexp\u003c/dt\u003e\n    \u003cdd\u003eThe regular expression pattern to look for within the DOM node.\u003c/dd\u003e\n    \u003cdt\u003e(string) $selector\u003c/dt\u003e\n    \u003cdd\u003eA query selector for the element to find.\u003c/dd\u003e\n    \u003cdt\u003e(string) $markup\u003c/dt\u003e\n    \u003cdd\u003eThe markup that should contain the \u003ccode\u003e$selector\u003c/code\u003e.\u003c/dd\u003e\n    \u003cdt\u003e(string) $message\u003c/dt\u003e\n    \u003cdd\u003eA message to display if the assertion fails.\u003c/dd\u003e\n\u003c/dl\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegrunwell%2Fphpunit-markup-assertions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevegrunwell%2Fphpunit-markup-assertions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegrunwell%2Fphpunit-markup-assertions/lists"}