Skip to content

Commit 16c2595

Browse files
committed
Let PHPUnit validate the parser against the mf2/tests suite
1 parent 87da7b3 commit 16c2595

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Mf2\Parser\Test;
4+
5+
class MicroformatsTestSuiteTest extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @dataProvider htmlAndJsonProvider
9+
*/
10+
public function testFromTestSuite($input, $expectedOutput)
11+
{
12+
$parser = new \Mf2\Parser($input);
13+
$this->compareJson(
14+
json_decode($expectedOutput, true),
15+
$parser->parse()
16+
);
17+
}
18+
19+
/**
20+
* Objects within JSON are unordered.
21+
* Check if all keys from the correct one are present (in any order) in our output.
22+
* Then recursively check the contents of those properties.
23+
**/
24+
public function compareJson($correct, $test)
25+
{
26+
if (gettype($correct) === 'array' && $this->isAssoc($correct)) {
27+
foreach ($correct as $key => $value) {
28+
$this->assertArrayHasKey($key, $test);
29+
$this->compareJson($value, $test[$key]);
30+
}
31+
} else {
32+
$this->assertEquals($correct, $test);
33+
}
34+
}
35+
36+
/**
37+
* Check if the encountered array is an associative array (has string keys).
38+
* @see https://stackoverflow.com/a/173479
39+
**/
40+
public function isAssoc($array)
41+
{
42+
return array() !== $array && array_keys($array) !== range(0, count($array) - 1);
43+
}
44+
45+
/**
46+
* Data provider lists all tests from mf2/tests.
47+
**/
48+
public function htmlAndJsonProvider()
49+
{
50+
// Ripped out of the test-suite.php code:
51+
$finder = new \RegexIterator(
52+
new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(
53+
dirname(__FILE__) . '/../../vendor/mf2/tests/tests',
54+
\RecursiveDirectoryIterator::SKIP_DOTS
55+
)),
56+
'/^.+\.html$/i',
57+
\RecursiveRegexIterator::GET_MATCH
58+
);
59+
// Build the array of separate tests:
60+
$tests = array();
61+
foreach ($finder as $key => $value) {
62+
$dir = realpath(pathinfo($key, PATHINFO_DIRNAME));
63+
$test = pathinfo($key, PATHINFO_BASENAME);
64+
$result = pathinfo($key, PATHINFO_FILENAME) . '.json';
65+
if (is_file($dir . '/' . $result)) {
66+
$tests[pathinfo($key, PATHINFO_FILENAME)] = array(
67+
'input' => file_get_contents($dir . '/' . $test),
68+
'expectedOutput' => file_get_contents($dir . '/' . $result)
69+
);
70+
}
71+
}
72+
return $tests;
73+
}
74+
}

0 commit comments

Comments
 (0)