Open menu icon N-ARY CIRCLED PLUS OPERATOR

shulard/atoum-xml-extension Build Status Latest Stable Version

This atoum extension allows you to test XML document using atoum. It's possible to execute xpath against the document or to validate it using DTD, XSD or RelaxNG schema. You can use it to validate HTML documents too.

Example

<?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)
        ;
    }
}

When running this test, the XML document will be loaded and:

Install it

Install extension using composer:

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

Enable and configure the extension using atoum configuration file:

<?php

// .atoum.php

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

use mageekguy\atoum\xml;

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

Use it

<?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');
        ;
    }
}

Links

Licence

atoum-xml-extension is released under the Apache2 License. See the bundled LICENSE file for details.

atoum