2017 © Pedro PelĂĄez
 

library atoum-xml-extension

The atoum xml extension allows you to make assertions on XML files

image

shulard/atoum-xml-extension

The atoum xml extension allows you to make assertions on XML files

  • Wednesday, February 14, 2018
  • by shulard
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4,034 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 6 Versions
  • 4 % Grown

The README.md

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., (*1)

Example


<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:, (*2)

  • Validate the document using a DTD;
  • Check if atom namespace is present in document declaration;
  • Check that dc namespace is used inside the document;
  • Execute a xpath one namespaced node and check returning node collection.

Install it

Install extension using composer:, (*3)

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

Enable and configure the extension using atoum configuration file:, (*4)

addExtension(new xml\extension($script));
```

## Use it

```php

<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

<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

<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

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

Licence

atoum-xml-extension is released under the Apache2 License. See the bundled LICENSE file for details., (*5)

atoum, (*6)

The Versions

14/02 2018

dev-master

9999999-dev http://www.atoum.org

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache-2.0 Apache-2

The Requires

 

The Development Requires

by Stéphane HULARD

test extension xml unit testing atoum atoum-extension schema validation

27/02 2017

v1.0.0

1.0.0.0 http://www.atoum.org

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache-2

The Requires

 

The Development Requires

by Stéphane HULARD

test extension xml unit testing atoum atoum-extension schema validation

27/02 2017

v0.2.0

0.2.0.0 http://www.atoum.org

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache-2

The Requires

 

The Development Requires

by Stéphane HULARD

test extension xml unit testing atoum atoum-extension schema validation

31/10 2016

v0.1.0

0.1.0.0 http://www.atoum.org

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache-2

The Requires

 

The Development Requires

by Stéphane HULARD

test extension xml unit testing atoum atoum-extension schema validation

26/10 2016

v0.0.1

0.0.1.0

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Stéphane HULARD

06/09 2016

v0.0.0

0.0.0.0

The atoum xml extension allows you to make assertions on XML files

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Stéphane HULARD