Skip to content

Run mf2/tests test suite with PHPUnit during default testing #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function __construct($input, $url = null, $jsonMode = false) {
$doc = $doc->loadHTML($input);
} else {
$doc = new DOMDocument();
@$doc->loadHTML(unicodeToHtmlEntities($input));
@$doc->loadHTML(unicodeToHtmlEntities($input), \LIBXML_NOWARNING);
}
} elseif (is_a($input, 'DOMDocument')) {
$doc = clone $input;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require-dev": {
"phpunit/phpunit": "4.8.*",
"mf2/tests": "@dev",
"mf2/tests": "dev-master#e9e2b905821ba0a5b59dab1a8eaf40634ce9cd49",
"squizlabs/php_codesniffer": "^2.6 || ^3.1.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.6",
"phpcompatibility/php-compatibility": "^9.3"
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
<directory suffix="Test.php">tests/Mf2</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>microformats/tests/mf1</group>
</exclude>
</groups>
</phpunit>
167 changes: 167 additions & 0 deletions tests/Mf2/MicroformatsTestSuiteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

namespace Mf2\Parser\Test;

final class TestSuiteParser extends \Mf2\Parser
{
/** Actually textContent from before the whitespace normalisation merge (e8da04f93d548d26287a8980eca4216639cbc61d) */
public function textContent(\DOMElement $el, $dummy=false) {
$excludeTags = array('noframe', 'noscript', 'script', 'style', 'frames', 'frameset');

if (isset($el->tagName) and in_array(strtolower($el->tagName), $excludeTags)) {
return '';
}

$this->_resolveChildUrls($el);

$clonedEl = $el->cloneNode(true);

foreach ($this->xpath->query('.//img', $clonedEl) as $imgEl) {
if ($imgEl->hasAttribute('alt')) {
$replacement = $imgEl->getAttribute('alt');
} else if ($imgEl->hasAttribute('src')) {
$replacement = ' ' . $imgEl->getAttribute('src') . ' ';
} else {
$replacement = ''; // Bye bye IMG element.
}
$newNode = $this->doc->createTextNode($replacement);
$imgEl->parentNode->replaceChild($newNode, $imgEl);
}

foreach ($excludeTags as $tagName) {
foreach ($this->xpath->query(".//{$tagName}", $clonedEl) as $elToRemove) {
$elToRemove->parentNode->removeChild($elToRemove);
}
}

return \Mf2\unicodeTrim($clonedEl->textContent);
}

// Hack. Old textContent requires "resolveChildUrls", but that method is private.
private $__resolveChildUrls = null;
private function _resolveChildUrls(\DOMElement $element) {
if (null === $this->__resolveChildUrls) {
$reflectUpon = new \ReflectionClass($this);
$this->__resolveChildUrls = $reflectUpon->getMethod('resolveChildUrls');
$this->__resolveChildUrls->setAccessible(true);
}
return $this->__resolveChildUrls->invoke($this, $element);
}
}

class MicroformatsTestSuiteTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider mf1TestsProvider
* @group microformats/tests/mf1
*/
public function testMf1FromTestSuite($input, $expectedOutput)
{
$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}

/**
* @dataProvider mf2TestsProvider
* @group microformats/tests/mf2
*/
public function testMf2FromTestSuite($input, $expectedOutput, $test)
{
if ($test === 'h-event/time') {
$this->markTestIncomplete('This test does not match because we implement a proposed spec: https://github.com/microformats/microformats2-parsing/issues/4#issuecomment-373457720.');
}

$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}

/**
* @dataProvider mixedTestsProvider
* @group microformats/tests/mixed
*/
public function testMixedFromTestSuite($input, $expectedOutput)
{
$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}

/**
* To make arrays coming from JSON more easily comparible by PHPUnit:
* * We sort arrays by key, normalising them, because JSON objects are unordered.
* * We json_encode strings, and cut the starting and ending ", so PHPUnit better
* shows whitespace characters like tabs and newlines.
**/
public function makeComparible($array)
{
ksort($array);
foreach ($array as $key => $value) {
if (gettype($value) === 'array') {
$array[$key] = $this->makeComparible($value);
} else if (gettype($value) === 'string') {
$array[$key] = substr(json_encode($value), 1, -1);
}
}
return $array;
}

/**
* Data provider lists all tests from a specific directory in mf2/tests.
**/
public function htmlAndJsonProvider($subFolder = '')
{
// Get the actual wanted subfolder.
$subFolder = '/mf2/tests/tests' . $subFolder;
// Ripped out of the test-suite.php code:
$finder = new \RegexIterator(
new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(
dirname(__FILE__) . '/../../vendor/' . $subFolder,
\RecursiveDirectoryIterator::SKIP_DOTS
)),
'/^.+\.html$/i',
\RecursiveRegexIterator::GET_MATCH
);
// Build the array of separate tests:
$tests = array();
foreach ($finder as $key => $value) {
$dir = realpath(pathinfo($key, PATHINFO_DIRNAME));
$testname = substr($dir, strpos($dir, $subFolder) + strlen($subFolder) + 1) . '/' . pathinfo($key, PATHINFO_FILENAME);
$test = pathinfo($key, PATHINFO_BASENAME);
$result = pathinfo($key, PATHINFO_FILENAME) . '.json';
if (is_file($dir . '/' . $result)) {
$tests[$testname] = array(
'input' => file_get_contents($dir . '/' . $test),
'expectedOutput' => file_get_contents($dir . '/' . $result),
'name' => $testname
);
}
}
return $tests;
}

/**
* Following three functions are the actual dataProviders used by the test methods.
*/
public function mf1TestsProvider()
{
return $this->htmlAndJsonProvider('/microformats-v1');
}

public function mf2TestsProvider()
{
return $this->htmlAndJsonProvider('/microformats-v2');
}

public function mixedTestsProvider()
{
return $this->htmlAndJsonProvider('/microformats-mixed');
}
}
4 changes: 2 additions & 2 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,9 @@ public function testNotMutatingPassedInDOM() {
// Use same parsing as Parser::__construct(), twice to have a comparison object.
libxml_use_internal_errors(true);
$refDoc = new \DOMDocument();
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input), \LIBXML_NOWARNING);
$inputDoc = new \DOMDocument();
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input), \LIBXML_NOWARNING);

// For completion sake, test PHP itself.
$this->assertEquals($refDoc, $inputDoc, 'PHP could not create identical DOMDocument instances.');
Expand Down
1 change: 0 additions & 1 deletion tests/test-suite/test-suite-data/adr/justaname/input.html

This file was deleted.

8 changes: 0 additions & 8 deletions tests/test-suite/test-suite-data/adr/justaname/output.json

This file was deleted.

5 changes: 0 additions & 5 deletions tests/test-suite/test-suite-data/adr/justaname/test.json

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions tests/test-suite/test-suite-data/adr/simpleproperties/output.json

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions tests/test-suite/test-suite-data/adr/suite.json

This file was deleted.

4 changes: 0 additions & 4 deletions tests/test-suite/test-suite-data/geo/abbrpattern/input.html

This file was deleted.

10 changes: 0 additions & 10 deletions tests/test-suite/test-suite-data/geo/abbrpattern/output.json

This file was deleted.

5 changes: 0 additions & 5 deletions tests/test-suite/test-suite-data/geo/abbrpattern/test.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/test-suite/test-suite-data/geo/hidden/input.html

This file was deleted.

10 changes: 0 additions & 10 deletions tests/test-suite/test-suite-data/geo/hidden/output.json

This file was deleted.

5 changes: 0 additions & 5 deletions tests/test-suite/test-suite-data/geo/hidden/test.json

This file was deleted.

Loading