Skip to content

Commit 1e2a2ee

Browse files
authored
Merge pull request #163 from Zegnat/central-tests
Run mf2/tests test suite with PHPUnit during default testing
2 parents 87da7b3 + 3b68bc5 commit 1e2a2ee

391 files changed

Lines changed: 187 additions & 4850 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Mf2/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public function __construct($input, $url = null, $jsonMode = false) {
362362
$doc = $doc->loadHTML($input);
363363
} else {
364364
$doc = new DOMDocument();
365-
@$doc->loadHTML(unicodeToHtmlEntities($input));
365+
@$doc->loadHTML(unicodeToHtmlEntities($input), \LIBXML_NOWARNING);
366366
}
367367
} elseif (is_a($input, 'DOMDocument')) {
368368
$doc = clone $input;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "4.8.*",
22-
"mf2/tests": "@dev",
22+
"mf2/tests": "dev-master#e9e2b905821ba0a5b59dab1a8eaf40634ce9cd49",
2323
"squizlabs/php_codesniffer": "^2.6 || ^3.1.0",
2424
"dealerdirect/phpcodesniffer-composer-installer": "^0.6",
2525
"phpcompatibility/php-compatibility": "^9.3"

composer.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
<directory suffix="Test.php">tests/Mf2</directory>
66
</testsuite>
77
</testsuites>
8+
<groups>
9+
<exclude>
10+
<group>microformats/tests/mf1</group>
11+
</exclude>
12+
</groups>
813
</phpunit>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Mf2\Parser\Test;
4+
5+
final class TestSuiteParser extends \Mf2\Parser
6+
{
7+
/** Actually textContent from before the whitespace normalisation merge (e8da04f93d548d26287a8980eca4216639cbc61d) */
8+
public function textContent(\DOMElement $el, $dummy=false) {
9+
$excludeTags = array('noframe', 'noscript', 'script', 'style', 'frames', 'frameset');
10+
11+
if (isset($el->tagName) and in_array(strtolower($el->tagName), $excludeTags)) {
12+
return '';
13+
}
14+
15+
$this->_resolveChildUrls($el);
16+
17+
$clonedEl = $el->cloneNode(true);
18+
19+
foreach ($this->xpath->query('.//img', $clonedEl) as $imgEl) {
20+
if ($imgEl->hasAttribute('alt')) {
21+
$replacement = $imgEl->getAttribute('alt');
22+
} else if ($imgEl->hasAttribute('src')) {
23+
$replacement = ' ' . $imgEl->getAttribute('src') . ' ';
24+
} else {
25+
$replacement = ''; // Bye bye IMG element.
26+
}
27+
$newNode = $this->doc->createTextNode($replacement);
28+
$imgEl->parentNode->replaceChild($newNode, $imgEl);
29+
}
30+
31+
foreach ($excludeTags as $tagName) {
32+
foreach ($this->xpath->query(".//{$tagName}", $clonedEl) as $elToRemove) {
33+
$elToRemove->parentNode->removeChild($elToRemove);
34+
}
35+
}
36+
37+
return \Mf2\unicodeTrim($clonedEl->textContent);
38+
}
39+
40+
// Hack. Old textContent requires "resolveChildUrls", but that method is private.
41+
private $__resolveChildUrls = null;
42+
private function _resolveChildUrls(\DOMElement $element) {
43+
if (null === $this->__resolveChildUrls) {
44+
$reflectUpon = new \ReflectionClass($this);
45+
$this->__resolveChildUrls = $reflectUpon->getMethod('resolveChildUrls');
46+
$this->__resolveChildUrls->setAccessible(true);
47+
}
48+
return $this->__resolveChildUrls->invoke($this, $element);
49+
}
50+
}
51+
52+
class MicroformatsTestSuiteTest extends \PHPUnit_Framework_TestCase
53+
{
54+
/**
55+
* @dataProvider mf1TestsProvider
56+
* @group microformats/tests/mf1
57+
*/
58+
public function testMf1FromTestSuite($input, $expectedOutput)
59+
{
60+
$parser = new TestSuiteParser($input, 'http://example.com/');
61+
$this->assertEquals(
62+
$this->makeComparible(json_decode($expectedOutput, true)),
63+
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
64+
);
65+
}
66+
67+
/**
68+
* @dataProvider mf2TestsProvider
69+
* @group microformats/tests/mf2
70+
*/
71+
public function testMf2FromTestSuite($input, $expectedOutput, $test)
72+
{
73+
if ($test === 'h-event/time') {
74+
$this->markTestIncomplete('This test does not match because we implement a proposed spec: https://github.com/microformats/microformats2-parsing/issues/4#issuecomment-373457720.');
75+
}
76+
77+
$parser = new TestSuiteParser($input, 'http://example.com/');
78+
$this->assertEquals(
79+
$this->makeComparible(json_decode($expectedOutput, true)),
80+
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
81+
);
82+
}
83+
84+
/**
85+
* @dataProvider mixedTestsProvider
86+
* @group microformats/tests/mixed
87+
*/
88+
public function testMixedFromTestSuite($input, $expectedOutput)
89+
{
90+
$parser = new TestSuiteParser($input, 'http://example.com/');
91+
$this->assertEquals(
92+
$this->makeComparible(json_decode($expectedOutput, true)),
93+
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
94+
);
95+
}
96+
97+
/**
98+
* To make arrays coming from JSON more easily comparible by PHPUnit:
99+
* * We sort arrays by key, normalising them, because JSON objects are unordered.
100+
* * We json_encode strings, and cut the starting and ending ", so PHPUnit better
101+
* shows whitespace characters like tabs and newlines.
102+
**/
103+
public function makeComparible($array)
104+
{
105+
ksort($array);
106+
foreach ($array as $key => $value) {
107+
if (gettype($value) === 'array') {
108+
$array[$key] = $this->makeComparible($value);
109+
} else if (gettype($value) === 'string') {
110+
$array[$key] = substr(json_encode($value), 1, -1);
111+
}
112+
}
113+
return $array;
114+
}
115+
116+
/**
117+
* Data provider lists all tests from a specific directory in mf2/tests.
118+
**/
119+
public function htmlAndJsonProvider($subFolder = '')
120+
{
121+
// Get the actual wanted subfolder.
122+
$subFolder = '/mf2/tests/tests' . $subFolder;
123+
// Ripped out of the test-suite.php code:
124+
$finder = new \RegexIterator(
125+
new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(
126+
dirname(__FILE__) . '/../../vendor/' . $subFolder,
127+
\RecursiveDirectoryIterator::SKIP_DOTS
128+
)),
129+
'/^.+\.html$/i',
130+
\RecursiveRegexIterator::GET_MATCH
131+
);
132+
// Build the array of separate tests:
133+
$tests = array();
134+
foreach ($finder as $key => $value) {
135+
$dir = realpath(pathinfo($key, PATHINFO_DIRNAME));
136+
$testname = substr($dir, strpos($dir, $subFolder) + strlen($subFolder) + 1) . '/' . pathinfo($key, PATHINFO_FILENAME);
137+
$test = pathinfo($key, PATHINFO_BASENAME);
138+
$result = pathinfo($key, PATHINFO_FILENAME) . '.json';
139+
if (is_file($dir . '/' . $result)) {
140+
$tests[$testname] = array(
141+
'input' => file_get_contents($dir . '/' . $test),
142+
'expectedOutput' => file_get_contents($dir . '/' . $result),
143+
'name' => $testname
144+
);
145+
}
146+
}
147+
return $tests;
148+
}
149+
150+
/**
151+
* Following three functions are the actual dataProviders used by the test methods.
152+
*/
153+
public function mf1TestsProvider()
154+
{
155+
return $this->htmlAndJsonProvider('/microformats-v1');
156+
}
157+
158+
public function mf2TestsProvider()
159+
{
160+
return $this->htmlAndJsonProvider('/microformats-v2');
161+
}
162+
163+
public function mixedTestsProvider()
164+
{
165+
return $this->htmlAndJsonProvider('/microformats-mixed');
166+
}
167+
}

tests/Mf2/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,9 @@ public function testNotMutatingPassedInDOM() {
817817
// Use same parsing as Parser::__construct(), twice to have a comparison object.
818818
libxml_use_internal_errors(true);
819819
$refDoc = new \DOMDocument();
820-
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
820+
@$refDoc->loadHTML(Mf2\unicodeToHtmlEntities($input), \LIBXML_NOWARNING);
821821
$inputDoc = new \DOMDocument();
822-
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input));
822+
@$inputDoc->loadHTML(Mf2\unicodeToHtmlEntities($input), \LIBXML_NOWARNING);
823823

824824
// For completion sake, test PHP itself.
825825
$this->assertEquals($refDoc, $inputDoc, 'PHP could not create identical DOMDocument instances.');

tests/test-suite/test-suite-data/adr/justaname/input.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test-suite/test-suite-data/adr/justaname/output.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/test-suite/test-suite-data/adr/justaname/test.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/test-suite/test-suite-data/adr/simpleproperties/input.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)