thomasweinert / phpunit-xpath-assertions
PHPUnit 的 Xpath 断言和约束
Requires
- php: ^7.4 || ~8.0.0
- ext-dom: *
- ext-json: *
- phpunit/phpunit: ^8.0 || ^9.0
This package is auto-updated.
Last update: 2024-08-31 00:25:50 UTC
README
用于与 PHPUnit 一起使用的 Xpath 断言和约束。
示例
use PHPUnit\Framework\TestCase; use PHPUnit\Xpath\Assert as XpathAssertions; class MyProjectExampleTest extends TestCase { use XpathAssertions; public function testChildElementExistsInDocument() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertXpathMatch('//child', $document); } public function testCompareChildElementFromDocument() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertXpathEquals('<child>TEXT</child>', '//child', $document); } }
安装
Phar
如果您使用 PHAR 作为 PHPUnit,您也可以将此扩展作为 PHAR 下载。它们可以在 发布页面 上找到。将扩展的 *.phar
文件下载到目录中,并在 PHPUnit 配置文件中提供该目录。
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" extensionsDirectory="tools/phpunit.d"> </phpunit>
Composer
如果您使用 Composer 来管理项目的依赖关系,那么您可以将 PHPUnit 示例扩展作为开发时依赖添加到项目中
$ composer require --dev thomasweinert/phpunit-xpath-assertions
使用方法
该库提供了一些特质,您可以使用它们将断言添加到您的 TestCase 中。
use PHPUnit\Xpath\Assert as XpathAssertions; use PHPUnit\Xpath\Constraint as XpathConstraints; class MyProjectExampleTest extends \PHPUnit\Framework\TestCase { use XpathAssertions; use XpathConstraints; }
约束
使用特质 PHPUnit\Xpath\Constraint
。它们可以与 assertThat()
或 Mocks 一起使用。
self::matchesXpathExpression()
function matchesXpathExpression(string $expression, array|\ArrayAccess $namespaces = [])
验证提供的 Xpath 表达式是否与某个为 TRUE 且非空的对象匹配。如果表达式返回一个空节点列表、空字符串或 FALSE,则会失败。
public function testChildElementExistsInDocument() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertThat( $document, self::matchesXpathExpression('//child') ); }
self::matchesXpathResultCount()
function matchesXpathResultCount( int $expectedCount, string $expression, array|\ArrayAccess $namespaces = array() )
如果提供的 Xpath 表达式与期望的节点数完全匹配,则返回 true。
public function testChildElementExistsOnTimeInDocument() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertThat( $document, self::matchesXpathResultCount(1, '//child') ); }
self::equalToXpathResult()
function equalToXpathResult( mixed $expected, string $expression, array|\ArrayAccess, $namespaces = array() )
如果表达式返回一个节点列表,则比较匹配的节点序列化的 XML 与提供的 XML 字符串或 DOM。如果表达式返回一个标量,则使用根据类型决定的约束。
public function testCompareChildElementFromDocument() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertThat( $document, self::equalToXpathResult( '<child>TEXT</child>', '//child' ) ); }
public function testCompareChildElementFromDocumentAsString() { $document = new \DOMDocument(); $document->loadXML('<root><child>TEXT</child></root>'); self::assertThat( $document, self::equalToXpathResult( 'TEXT', 'string(//child)' ) ); }
断言
使用特质 PHPUnit\Xpath\Assert
。这些断言是 assertThat()
的快捷方式。
- self::assertXpathMatch()
- self::assertXpathCount()
- self::assertXpathEquals()
命名空间
所有方法都有一个可选参数,允许您提供命名空间定义。
public function testChildWithNamespaceElementExistsTwoTimesInDocument() { $document = new \DOMDocument(); $document->loadXML( '<example:root xmlns:example="urn:example"> <example:child>TEXT</example:child> <example:child>TEXT</example:child> </example:root>' ); self::assertThat( $document, self::matchesXpathResultCount(2, '//e:child', ['e' => 'urn:example']) ); }
JSON (>= 1.2.0)
断言可以与 JsonSerializable 对象/数组一起使用。它们将在内部转换为 DOM 表示形式。
public function testHomePhoneNumbersEqualsExpected() { self::assertXpathEquals( [ [ 'type' => 'home', 'number' => '212 555-1234' ] ], 'phoneNumbers/*[type="home"]', json_decode($wikipediaJsonExample) ); }
贡献
欢迎贡献,请使用问题跟踪器来报告错误和功能想法。
构建修改后的 phar
此项目包括用于创建自己的 phar 文件的构建脚本。要创建 phar,请调用 ./build/build-phar
。
构建已签名的 phar
要创建已签名的 phar,请将 dist.build.properties
复制到 build.properties
并设置 gpg.user
。然后调用 ./build/build-phar package
。