From 6e9dbfc0d521a0c62994388eef39c89247ffd475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20HULARD?= Date: Mon, 27 Feb 2017 13:15:10 +0100 Subject: [PATCH] Register a new handler : html This allow to make assertions on HTML content file. --- classes/extension.php | 37 +++++++++++++++---- tests/units/classes/asserters/node.php | 50 +++++++++++++++++++++++++- tests/units/classes/extension.php | 22 ++++++++++++ 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/classes/extension.php b/classes/extension.php index a1cb48f..f99d9b8 100644 --- a/classes/extension.php +++ b/classes/extension.php @@ -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')) ; } } @@ -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; } diff --git a/tests/units/classes/asserters/node.php b/tests/units/classes/asserters/node.php index ee99d6b..c86385b 100644 --- a/tests/units/classes/asserters/node.php +++ b/tests/units/classes/asserters/node.php @@ -189,7 +189,7 @@ public function test_xpath() ; } - public function test_xpathwith_namespace() + public function test_xpath_with_namespace() { $xml = << @@ -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();'); + ; + } } diff --git a/tests/units/classes/extension.php b/tests/units/classes/extension.php index 0c75925..4ac54d2 100644 --- a/tests/units/classes/extension.php +++ b/tests/units/classes/extension.php @@ -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(''); + $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') + ; + } }