Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shulard/atoum-xml-extension

An atoum extension to perform tests on XML files
https://github.com/shulard/atoum-xml-extension

atoum atoum-extension dtd unit-testing xml-document xpath

Last synced: 3 months ago
JSON representation

An atoum extension to perform tests on XML files

Awesome Lists containing this project

README

        

# shulard/atoum-xml-extension [![Build Status](https://travis-ci.org/shulard/atoum-xml-extension.svg?branch=master)](https://travis-ci.org/shulard/atoum-xml-extension) [![Latest Stable Version](https://img.shields.io/packagist/v/shulard/atoum-xml-extension.svg)](https://packagist.org/packages/shulard/atoum-xml-extension)

This atoum extension allows you to test XML document using [atoum](https://github.com/atoum/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

1namespaced content2

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:

* 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](https://getcomposer.org):

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

Enable and configure the extension using atoum configuration file:

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

## Use it

```php


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 = <<

content

XML;

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

/**
* Validate namespace on nodes
*/
public function testNamespaces()
{
$xml = <<

1namespaced content2

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 = <<

1namespaced content2

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

* [atoum](http://atoum.org)
* [atoum's documentation](http://docs.atoum.org)

## Licence

atoum-xml-extension is released under the Apache2 License. See the bundled [LICENSE](https://github.com/shulard/atoum-xml-extension/blob/master/LICENSE) file for details.

![atoum](http://atoum.org/images/logo/atoum.png)