@@ -10,44 +10,29 @@ class MicroformatsTestSuiteTest extends \PHPUnit_Framework_TestCase
10
10
public function testFromTestSuite ($ input , $ expectedOutput )
11
11
{
12
12
$ 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 ) )
16
16
);
17
17
}
18
18
19
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.
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.
23
24
**/
24
- public function compareJson ( $ correct , $ test )
25
+ public function makeComparible ( $ array )
25
26
{
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 );
30
33
}
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 );
41
34
}
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 ;
51
36
}
52
37
53
38
/**
0 commit comments