{"id":18707155,"url":"https://github.com/thomasweinert/phpunit-xpath-assertions","last_synced_at":"2026-03-04T16:02:52.361Z","repository":{"id":25927928,"uuid":"106911705","full_name":"ThomasWeinert/phpunit-xpath-assertions","owner":"ThomasWeinert","description":"Xpath based assertions for PHPUnit","archived":false,"fork":false,"pushed_at":"2024-12-13T13:25:06.000Z","size":85,"stargazers_count":14,"open_issues_count":3,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T01:17:06.823Z","etag":null,"topics":["dom","php","phpunit","xml","xpath"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ThomasWeinert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2017-10-14T08:33:11.000Z","updated_at":"2022-11-28T22:47:33.000Z","dependencies_parsed_at":"2024-02-02T17:49:18.462Z","dependency_job_id":null,"html_url":"https://github.com/ThomasWeinert/phpunit-xpath-assertions","commit_stats":{"total_commits":46,"total_committers":4,"mean_commits":11.5,"dds":"0.26086956521739135","last_synced_commit":"37ac7dc02db270bd667c9a4cc6e0f93841918782"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThomasWeinert%2Fphpunit-xpath-assertions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThomasWeinert%2Fphpunit-xpath-assertions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThomasWeinert%2Fphpunit-xpath-assertions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThomasWeinert%2Fphpunit-xpath-assertions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ThomasWeinert","download_url":"https://codeload.github.com/ThomasWeinert/phpunit-xpath-assertions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248551150,"owners_count":21123042,"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":["dom","php","phpunit","xml","xpath"],"created_at":"2024-11-07T12:16:49.926Z","updated_at":"2026-03-04T16:02:52.345Z","avatar_url":"https://github.com/ThomasWeinert.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# phpunit-xpath-assertions\n\n[![CI](https://github.com/ThomasWeinert/phpunit-xpath-assertions/actions/workflows/ci.yml/badge.svg)](https://github.com/ThomasWeinert/phpunit-xpath-assertions/actions/workflows/ci.yml)\n[![License](https://img.shields.io/packagist/l/thomasweinert/phpunit-xpath-assertions.svg)](https://github.com/thomasweinert/phpunit-xpath-assertions/blob/master/LICENSE)\n[![Total Downloads](https://img.shields.io/packagist/dt/thomasweinert/phpunit-xpath-assertions.svg)](https://packagist.org/packages/thomasweinert/phpunit-xpath-assertions)\n[![Latest Stable Version](https://img.shields.io/packagist/v/thomasweinert/phpunit-xpath-assertions.svg)](https://packagist.org/packages/thomasweinert/phpunit-xpath-assertions)\n\nXpath assertions and constraints for use with PHPUnit.\n\n## Example\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse PHPUnit\\Xpath\\AssertTrait as XpathAssertions;\n\nclass MyProjectExampleTest extends TestCase\n{\n    use XpathAssertions;\n\n    public function testChildElementExistsInDocument()\n    {\n        $document = new \\DOMDocument();\n        $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n        self::assertXpathMatch('//child', $document);\n    }\n\n    public function testCompareChildElementFromDocument()\n    {\n        $document = new \\DOMDocument();\n        $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n        self::assertXpathEquals('\u003cchild\u003eTEXT\u003c/child\u003e', '//child', $document);\n    }\n}\n```\n\n## Installation\n\n### Composer\n\nIf you use [Composer](https://getcomposer.org/) to manage the dependencies of your project then you can add the PHPUnit example extension as a development-time dependency to your project:\n\n```\n$ composer require --dev thomasweinert/phpunit-xpath-assertions\n```\n\n## Usage\n\nThe library provides traits that you can use to add the assertions to your TestCase.\n\n```php\nuse PHPUnit\\Xpath\\AssertTrait as XpathAssertions;\nuse PHPUnit\\Xpath\\ConstraintTrait as XpathConstraints;\n\nclass MyProjectExampleTest extends \\PHPUnit\\Framework\\TestCase\n{\n    use XpathAssertions;\n    use XpathConstraints;\n}\n```\n\n### Constraints\n\nUse trait `PHPUnit\\Xpath\\ConstraintTrait`. They can be used with `assertThat()` or\nwith Mocks.\n\n#### self::matchesXpathExpression()\n\n```php\nfunction matchesXpathExpression(string $expression, array|\\ArrayAccess $namespaces = [])\n```\n\nValidate if the provided Xpath expression matches something that is TRUE and not empty.\nIt will fail if the expression returns an empty node list or an empty string or FALSE.\n\n```php\npublic function testChildElementExistsInDocument()\n{\n    $document = new \\DOMDocument();\n    $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n    self::assertThat(\n        $document,\n        self::matchesXpathExpression('//child')\n    );\n}\n```\n\n#### self::matchesXpathResultCount()\n\n```php\nfunction matchesXpathResultCount(\n    int $expectedCount, string $expression, array|\\ArrayAccess $namespaces = array()\n)\n```\n\nReturns true if the provided Xpath expression matches exactly the expected count of nodes.\n\n```php\npublic function testChildElementExistsOnTimeInDocument()\n{\n    $document = new \\DOMDocument();\n    $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n    self::assertThat(\n        $document,\n        self::matchesXpathResultCount(1, '//child')\n    );\n}\n```\n\n#### self::equalToXpathResult()\n\n```php\nfunction equalToXpathResult(\n    mixed $expected,\n    string $expression,\n    array|\\ArrayAccess,\n    $namespaces = array()\n)\n```\n\nIf the expressions return a node list it compares the serialized XML of the matched nodes with the provided XML string\nor DOM. If the expression return a scalar uses a constraint depending on the type.\n\n```php\npublic function testCompareChildElementFromDocument()\n{\n    $document = new \\DOMDocument();\n    $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n    self::assertThat(\n        $document,\n        self::equalToXpathResult(\n            '\u003cchild\u003eTEXT\u003c/child\u003e',\n            '//child'\n        )\n    );\n}\n```\n\n\n```php\npublic function testCompareChildElementFromDocumentAsString()\n{\n    $document = new \\DOMDocument();\n    $document-\u003eloadXML('\u003croot\u003e\u003cchild\u003eTEXT\u003c/child\u003e\u003c/root\u003e');\n\n    self::assertThat(\n        $document,\n        self::equalToXpathResult(\n            'TEXT',\n            'string(//child)'\n        )\n    );\n}\n```\n\n### Assertions\n\nUse trait `PHPUnit\\Xpath\\AssertTrait`. These assertions are shortcuts for\n`assertThat()`.\n\n* self::assertXpathMatch()\n* self::assertXpathCount()\n* self::assertXpathEquals()\n\n### Namespaces\n\nAll methods have an optional argument that allow to provide an namespace definition.\n\n```php\npublic function testChildWithNamespaceElementExistsTwoTimesInDocument()\n{\n    $document = new \\DOMDocument();\n    $document-\u003eloadXML(\n        '\u003cexample:root xmlns:example=\"urn:example\"\u003e\n        \u003cexample:child\u003eTEXT\u003c/example:child\u003e\n        \u003cexample:child\u003eTEXT\u003c/example:child\u003e\n        \u003c/example:root\u003e'\n    );\n\n    self::assertThat(\n        $document,\n        self::matchesXpathResultCount(2, '//e:child', ['e' =\u003e 'urn:example'])\n    );\n}\n```\n\n### JSON (\u003e= 1.2.0)\n\nThe assertions can be used with JsonSerializable objects/arrays. They will be\nconverted into a DOM representation internally.\n\n```php\npublic function testHomePhoneNumbersEqualsExpected()\n{\n    self::assertXpathEquals(\n        [\n            [ 'type' =\u003e 'home', 'number' =\u003e '212 555-1234' ]\n        ],\n        'phoneNumbers/*[type=\"home\"]',\n        json_decode($wikipediaJsonExample)\n    );\n}\n```\n\n# Contributing\n\nContributions are welcome, please use the issue tracker to report bug and feature ideas.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasweinert%2Fphpunit-xpath-assertions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasweinert%2Fphpunit-xpath-assertions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasweinert%2Fphpunit-xpath-assertions/lists"}