Skip to content

Commit

Permalink
Register a new handler : html
Browse files Browse the repository at this point in the history
This allow to make assertions on HTML content file.
  • Loading branch information
shulard committed Feb 27, 2017
1 parent a140880 commit 6e9dbfc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
37 changes: 31 additions & 6 deletions classes/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class extension implements atoum\extension
public function __construct(atoum\configurator $configurator = null)
{
if ($configurator) {
$parser = $configurator->getScript()->getArgumentsParser();
$handler = function ($script, $argument, $values) {
$script->getRunner()->addTestsFromDirectory(dirname(__DIR__) . '/tests/units/classes');
};
$parser
->addHandler($handler, array('--test-ext'))
$configurator
->getScript()
->getArgumentsParser()
->addHandler(function ($script, $argument, $values) {
$script
->getRunner()
->addTestsFromDirectory(dirname(__DIR__) . '/tests/units/classes');
}, array('--test-ext'))
;
}
}
Expand Down Expand Up @@ -45,6 +47,29 @@ function ($xml = null, $depth = null, $options = null) use ($test, & $asserter)
->setWith($xml, $depth, $options);
}
)
->setHandler(
'html',
function ($html = null, $depth = null, $options = null) use ($test, & $asserter) {
if ($asserter === null) {
$asserter = new atoum\xml\asserters\node($test->getAsserterGenerator());
}
if (null === $html) {
throw new atoum\exceptions\logic("HTML is undefined");
}

$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->validateOnParse = true;
@$dom->loadHTML($html);
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);

return $asserter
->setWithTest($test)
->setWith($dom, $depth, $options);
}
)
;
return $this;
}
Expand Down
50 changes: 49 additions & 1 deletion tests/units/classes/asserters/node.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function test_xpath()
;
}

public function test_xpathwith_namespace()
public function test_xpath_with_namespace()
{
$xml = <<<XML
<?xml version="1.0"?>
Expand Down Expand Up @@ -329,4 +329,52 @@ public function test_nodes_asserter_on_property_access()
->isEqualTo(1)
;
}

public function test_asserter_on_html_document()
{
$node = $this
->html(file_get_contents(__DIR__.'/../../../resources/index.html'));

$node->nodename->isEqualTo('html');
$items = $node->body;

$this
->then
->object($items)
->isInstanceOf('mageekguy\atoum\xml\asserters\nodes')
;
$items
->size
->isEqualTo(1)
;
}

public function test_xpath_on_html()
{
$html = $this
->html(file_get_contents(__DIR__.'/../../../resources/index.html'));

$html
->xpath('//meta[@name="Robots"]')
->hasSize(1)
->item(0)
->attributes()
->string['content']
->isEqualTo('Index, Follow')
;
$html
->xpath('//meta[starts-with(@property, "og:")]')
->hasSize(6)
->item(5)
->attributes()
->string['content']
->isEqualTo('CH Studio');
;
$html
->xpath('//body/script')
->last()
->nodevalue
->contains('gapi.plusone.go();');
;
}
}
22 changes: 22 additions & 0 deletions tests/units/classes/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@ public function test_xml_asserter_without_value()
->hasMessage('XML is undefined')
;
}

public function test_html_asserter_with_value()
{
$asserter = $this->then->html('<html />');
$this
->then
->object($asserter)
->isInstanceOf('mageekguy\atoum\xml\asserters\node')
;
}

public function test_html_asserter_without_value()
{
$this
->then
->exception(function () {
$this->then->html();
})
->isInstanceOf('mageekguy\atoum\exceptions\logic')
->hasMessage('HTML is undefined')
;
}
}

0 comments on commit 6e9dbfc

Please sign in to comment.