shulard/atoum-xml-extension

atoum xml 扩展允许您对 XML 文件进行断言

v1.0.0 2017-02-27 13:59 UTC

This package is auto-updated.

Last update: 2024-09-06 09:48:07 UTC


README

这个 atoum 扩展允许您使用 atoum 测试 XML 文档。您可以对文档执行 xpath 操作,或者使用 DTD、XSD 或 RelaxNG 架构进行验证。您也可以用它来验证 HTML 文档。

示例

<?php
namespace shulard\example\xml;

use atoum;

class foo extends atoum\test
{
    public function testXMLDocument()
    {
        $xml = <<<XML
<?xml version="1.0" ?>
<root xmlns:atom="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <atom:feed>1<dc:node>namespaced content</dc:node>2</atom:feed>
    <node attribute="value" />
    <node m:attribute="namespaced value" />
</root>
XML;

        $this
            ->then
                ->xml($xml)
                    ->isValidAgainstSchema
                        ->dtd('file://path/to.dtd', 'root')
                ->node
                    ->hasNamespace('atom', 'http://purl.org/atom/ns#')
                    ->isUsedNamespace('dc', 'http://purl.org/dc/elements/1.1/')
                    ->withNamespace('m', 'http://purl.org/atom/ns#')
                        ->xpath('//m:feed')
                            ->hasSize(1)
        ;
    }
}

当运行此测试时,将加载 XML 文档并

  • 使用 DTD 验证文档;
  • 检查文档声明中是否存在 atom 命名空间;
  • 检查文档内部是否使用了 dc 命名空间;
  • 对命名空间中的一个节点执行 xpath 操作,并检查返回的节点集合。

安装

使用 composer 安装扩展

composer require --dev shulard/atoum-xml-extension

使用 atoum 配置文件启用并配置扩展

<?php

// .atoum.php

require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use mageekguy\atoum\xml;

$runner->addExtension(new xml\extension($script));

使用它

<?php
namespace shulard\example\xml;

use atoum;

class foo extends atoum\test
{
    /**
     * Test attribute on nodes
     */
    public function testAttributes()
    {
        $xml = <<<XML
<?xml version="1.0" ?>
<root>
    <node attribute="value" />
    <node m:attribute="namespaced value" />
</root>
XML;

        $node = $this->xml($xml)
            ->children
            ->item(0);
        $node
            ->attributes()
                ->hasSize(1)
                ->string['attribute']->isEqualTo('value')
        ;
        $node
            ->attributes('m')
                ->hasSize(1)
                ->string['attribute']->isEqualTo('namespaced value')
        ;
    }

    /**
     * Test node content using phpString asserter
     */
    public function testXpathAndNodeContent()
    {
        $xml = <<<XML
<?xml version="1.0" ?>
<root>
    <node attribute="value">content</node>
</root>
XML;

        $this
            ->then
                ->xml($xml)
                ->xpath('//node')
                    ->hasSize(1)
                    ->item(0)
                        ->nodeValue->isEqualTo('content')
        ;
    }

    /**
     * Validate namespace on nodes
     */
    public function testNamespaces()
    {
        $xml = <<<XML
<?xml version="1.0" ?>
<root xmlns:atom="http://purl.org/atom/ns#" xmlns:toto="http://example.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <atom:feed>1<dc:node>namespaced content</dc:node>2</atom:feed>
</root>
XML;

        $this
            ->then
                ->xml($xml)
                ->hasNamespace('atom', 'http://purl.org/atom/ns#')
                ->isUsedNamespace('dc', 'http://purl.org/dc/elements/1.1/')
                    ->withNamespace('m', 'http://purl.org/atom/ns#')
                    ->xpath('//m:feed')
                        ->hasSize(1)
                        ->item(0)
                            ->xpath('./dc:node')
                                ->hasSize(1)
                            ->parent
                                ->xpath('//atom:feed')
                                    ->hasSize(1)
                                    ->item(0)
                                        ->nodeValue->isEqualTo("12")
        ;
    }

    /**
     * Validate document through schema (DTD, XSD, RNG)
     */
    public function testSchemaValidation()
    {
        $xml = <<<XML
<?xml version="1.0" ?>
<root>
    <atom:feed>1<dc:node>namespaced content</dc:node>2</atom:feed>
</root>
XML;

        $this
            ->then
                ->xml($xml)
                    ->isValidAgainstSchema
                        ->dtd('file://path/to.dtd', 'root')
                ->node
                    ->isValidAgainstSchema
                        ->schema('/path/to/schema.xsd')
                ->node
                    ->isValidAgainstSchema
                        ->relaxNg('/path/to/file.rng')
        ;
    }

    /**
     * You can also make tests on HTML Document
     */
    public function testOnHtmlDocument()
    {
        $this
            ->then
                ->html(file_get_contents('http://example.com'))
                ->xpath('//title')
                    ->item(0)
                        ->nodevalue
                            ->isEqualTo('My awesome title')
                ->xpath('//body/script')
                    ->last()
                        ->nodevalue
                            ->contains('GMTXXXXXX');
        ;
    }
}

链接

许可

atoum-xml-extension 在 Apache2 许可证下发布。有关详细信息,请参阅附带 LICENSE 文件。

atoum