-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMicroformatsTestSuiteTest.php
169 lines (150 loc) · 6.05 KB
/
MicroformatsTestSuiteTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Mf2\Parser\Test;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
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 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');
}
}