Skip to content

Commit 48a71c8

Browse files
committed
Have PHPUnit display better test failures
1 parent 5ae701f commit 48a71c8

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

tests/Mf2/MicroformatsTestSuiteTest.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,29 @@ class MicroformatsTestSuiteTest extends \PHPUnit_Framework_TestCase
1010
public function testFromTestSuite($input, $expectedOutput)
1111
{
1212
$parser = new \Mf2\Parser($input);
13-
$this->compareJson(
14-
json_decode($expectedOutput, true),
15-
$parser->parse()
13+
$this->assertEquals(
14+
$this->makeComparible(json_decode($expectedOutput, true)),
15+
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
1616
);
1717
}
1818

1919
/**
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.
20+
* To make arrays coming from JSON more easily comparible by PHPUnit:
21+
* * We sort arrays by key, normalising them, because JSON objects are unordered.
22+
* * We json_encode strings, and cut the starting and ending ", so PHPUnit better
23+
* shows whitespace characters like tabs and newlines.
2324
**/
24-
public function compareJson($correct, $test)
25+
public function makeComparible($array)
2526
{
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]);
27+
ksort($array);
28+
foreach ($array as $key => $value) {
29+
if (gettype($value) === 'array') {
30+
$array[$key] = $this->makeComparible($value);
31+
} else if (gettype($value) === 'string') {
32+
$array[$key] = substr(json_encode($value), 1, -1);
3033
}
31-
foreach (array_diff(array_keys($test), array_keys($correct)) as $fault) {
32-
// This will always fail, but we want to know in which tests this happens!
33-
$this->assertArrayHasKey(
34-
$fault,
35-
$correct,
36-
'The parser output included an extra property compared.'
37-
);
38-
}
39-
} else {
40-
$this->assertEquals($correct, $test);
4134
}
42-
}
43-
44-
/**
45-
* Check if the encountered array is an associative array (has string keys).
46-
* @see https://stackoverflow.com/a/173479
47-
**/
48-
public function isAssoc($array)
49-
{
50-
return array() !== $array && array_keys($array) !== range(0, count($array) - 1);
35+
return $array;
5136
}
5237

5338
/**

0 commit comments

Comments
 (0)