1010
1111namespace Flow \JSONPath \Test ;
1212
13- use Exception ;
14- use Flow \JSONPath \JSONPath ;
13+ use Flow \JSONPath \{JSONPath , JSONPathException };
1514use PHPUnit \Framework \{ExpectationFailedException , TestCase };
1615
1716use function fwrite ;
@@ -33,44 +32,62 @@ class QueryTest extends TestCase
3332 * printed to the console (using STDERR), so we know, what's going on.
3433 *
3534 * @see https://cburgmer.github.io/json-path-comparison
36- *
37- * @small
3835 * @dataProvider queryDataProvider
39- *
4036 * @param string $query
4137 * @param string $selector
4238 * @param string $data
4339 * @param string $consensus
4440 * @param bool $allowFail
45- * @throws Exception
41+ * @param bool $skip
42+ * @throws JSONPathException
4643 */
4744 public function testQueries (
4845 string $ query ,
4946 string $ selector ,
5047 string $ data ,
5148 string $ consensus ,
52- bool $ allowFail = false
49+ bool $ allowFail = false ,
50+ bool $ skip = false
5351 ): void {
52+ if ($ skip ) {
53+ // Avoid "This test did not perform any assertions"
54+ // but do not use markTestSkipped, to prevent unnecessary
55+ // console outputs
56+ self ::assertTrue (true );
57+
58+ return ;
59+ }
60+
61+ $ results = json_encode ((new JSONPath (json_decode ($ data , true )))->find ($ selector ));
62+
5463 if ($ allowFail ) {
5564 try {
56- self ::assertEquals (
57- $ consensus ,
58- json_encode ((new JSONPath (json_decode ($ data , true )))->find ($ selector ))
59- );
65+ self ::assertEquals ($ consensus , $ results );
6066 } catch (ExpectationFailedException $ e ) {
6167 $ comparisonFailure = $ e ->getComparisonFailure ();
6268
6369 fwrite (STDERR , "Query: {$ query }\n{$ comparisonFailure ->toString ()}" );
6470 }
6571 } else {
66- self ::assertEquals (
67- $ consensus ,
68- json_encode ((new JSONPath (json_decode ($ data , true )))->find ($ selector ))
69- );
72+ self ::assertEquals ($ consensus , $ results );
7073 }
7174 }
7275
7376 /**
77+ * Returns a list of queries, test data and expected results.
78+ *
79+ * A hand full of queries may run forever, thus they should
80+ * be skipped.
81+ *
82+ * Queries that are currently known as "problematic" are:
83+ *
84+ * - array_slice_with_negative_step_and_start_greater_than_end
85+ * - array_slice_with_open_end_and_negative_step
86+ * - array_slice_with_large_number_for_start
87+ * - array_slice_with_large_number_for_end
88+ * - array_slice_with_open_start_and_negative_step
89+ * - array_slice_with_negative_step_only
90+ *
7491 * @return string[]
7592 * @todo Finish this list
7693 */
@@ -113,13 +130,14 @@ public function queryDataProvider(): array
113130 '["first", "second", "third", "forth", "fifth"] ' ,
114131 '[] ' , // Unknown consensus, might be ["third","second","first"]
115132 ],
116- //[
117- // 'Array slice with large number for start',
118- // '$[-113667776004:2]',
119- // '["first","second","third","forth","fifth"]',
120- // '[]', // Unknown consensus, might be ["first","second"],
121- // true
122- //]
133+ [
134+ 'Array slice with large number for start ' ,
135+ '$[-113667776004:2] ' ,
136+ '["first","second","third","forth","fifth"] ' ,
137+ '[] ' , // Unknown consensus, might be ["first","second"],
138+ true , // Allow fail
139+ true // Skip
140+ ]
123141 ];
124142 }
125143}
0 commit comments