jroszkiewicz / phpunit-xpath-assertions
PHPUnit 的 XPath 断言和约束
3.1.0
2023-03-02 10:29 UTC
Requires
- php: ^7.3|^8.0
- ext-dom: *
- ext-json: *
- phpunit/phpunit: ^8.0 || ^9.0 || ^10.0
This package is auto-updated.
Last update: 2024-08-30 01:06:13 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); } }
安装
使用 Composer 管理项目的依赖关系,然后可以将 PHPUnit 示例扩展添加为开发时依赖项
$ composer require --dev jroszkiewicz/phpunit-xpath-assertions
用法
该库提供了可以用于将断言添加到 TestCase 中的 traits。
use PHPUnit\Xpath\Assert as XpathAssertions; use PHPUnit\Xpath\Constraint as XpathConstraints; class MyProjectExampleTest extends \PHPUnit\Framework\TestCase { use XpathAssertions; use XpathConstraints; }
约束
使用 trait 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)' ) ); }
断言
使用 trait 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) ); }
贡献
欢迎贡献,请使用问题跟踪器报告错误和功能想法。