File tree Expand file tree Collapse file tree 4 files changed +30
-4
lines changed Expand file tree Collapse file tree 4 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,16 @@ public function resolve($definition)
81
81
$ path = realpath ($ this ->getIterator ()->getRootDefinition ()->getBasepath () . \DIRECTORY_SEPARATOR . $ path );
82
82
}
83
83
84
- return file_get_contents ($ path );
84
+ $ content = file_get_contents ($ path );
85
+
86
+ if (pathinfo ($ path , PATHINFO_EXTENSION ) == 'json ' ) {
87
+ $ content = json_decode ($ content , true );
88
+
89
+ if (json_last_error () !== JSON_ERROR_NONE ) {
90
+ throw new \RuntimeException ('Failed to parse ' . basename ($ path ) . ': ' . json_last_error_msg ());
91
+ }
92
+ }
93
+
94
+ return $ content ;
85
95
}
86
96
}
Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ protected function setUp(): void
42
42
->andReturnUsing (function (string $ key , Definition $ definition ) {
43
43
return $ definition ->get ($ key );
44
44
});
45
+
46
+ $ this ->mockIterator ->shouldReceive ('getRootDefinition->getBasePath ' )
47
+ ->andReturn (__DIR__ );
45
48
}
46
49
47
50
public function testIndicator (): void
@@ -86,14 +89,25 @@ public function testIsValid(): void
86
89
verify ($ this ->resolver ->isValid ($ invalidParse ))->is ()->false ();
87
90
}
88
91
92
+ public function testJsonException (): void
93
+ {
94
+ $ this ->expectException (\RuntimeException::class);
95
+ // Omit portion of the error message that comes from buitlin PHP value
96
+ $ this ->expectExceptionMessage ('Failed to parse invalid.json: ' );
97
+
98
+ $ this ->resolver ->resolve ('./_data/invalid.json ' );
99
+ }
100
+
89
101
public function testResolve (): void
90
102
{
91
103
$ definition = new Definition (['file ' => './_data/sample.txt ' ]);
92
104
93
- $ this ->mockIterator ->shouldReceive ('getRootDefinition->getBasePath ' )
94
- ->andReturn (__DIR__ );
95
-
96
105
verify ($ this ->resolver ->resolve ('./_data/sample.txt ' ))->is ()->sameAs ("This is a sample file. \n" );
97
106
verify ($ this ->resolver ->resolve ($ definition ))->is ()->sameAs ("This is a sample file. \n" );
98
107
}
108
+
109
+ public function testResolveJson (): void
110
+ {
111
+ verify ($ this ->resolver ->resolve ('./_data/sample.json ' ))->is ()->sameAs (['json ' => true ]);
112
+ }
99
113
}
Original file line number Diff line number Diff line change
1
+ { 'json': false
Original file line number Diff line number Diff line change
1
+ {"json" : true }
You can’t perform that action at this time.
0 commit comments