diff --git a/src/Filters/QueryMatchFilter.php b/src/Filters/QueryMatchFilter.php index 13b8ee4..59caa72 100644 --- a/src/Filters/QueryMatchFilter.php +++ b/src/Filters/QueryMatchFilter.php @@ -9,6 +9,7 @@ namespace Flow\JSONPath\Filters; use Flow\JSONPath\AccessHelper; +use Flow\JSONPath\JSONPath; use RuntimeException; class QueryMatchFilter extends AbstractFilter @@ -59,9 +60,13 @@ public function filter($collection): array $return = []; foreach ($collection as $value) { + $value1 = null; if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) { $value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed); - + } elseif (\str_contains($key, '.')) { + $value1 = (new JSONPath($value))->find($key)->getData()[0] ?? ''; + } + if ($value1) { if ($operator === null && $value1) { $return[] = $value; } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 9c70b83..942460a 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -17,10 +17,16 @@ use PHPUnit\Framework\TestCase; use RuntimeException; -use const STDERR; - class QueryTest extends TestCase { + public static array $baselineFailedQueries; + + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + self::$baselineFailedQueries = \array_map('trim', \file(__DIR__ . '/data/baselineFailedQueries.txt')); + } + /** * This method aims to test the current implementation against * all queries listed on https://cburgmer.github.io/json-path-comparison/ @@ -70,21 +76,24 @@ public function testQueries( // assert in these cases. There might be still some false positives // (e.g. multidimensional comparisons), but that's okay, I guess. Maybe, // we can also find a way around that in the future. + $message = "==========================\n"; + $message .= "Query: {$query}\n\nMore information: {$url}\n"; + $message .= "==========================\n\n"; self::assertEqualsCanonicalizing( \json_decode($consensus, true), - \json_decode($results, true) + \json_decode($results, true), + $message ); } catch (ExpectationFailedException) { - $e = $e->getComparisonFailure(); - - \fwrite(STDERR, "==========================\n"); - \fwrite(STDERR, "Query: {$query}\n\n{$e->toString()}\nMore information: {$url}\n"); - \fwrite(STDERR, "==========================\n\n"); + if (!\in_array($id, self::$baselineFailedQueries, true)) { + throw new ExpectationFailedException( + $e->getMessage() . "\nQuery: {$query}\n\nMore information: {$url}", + $e->getComparisonFailure() + ); + } } } catch (JSONPathException $e) { - \fwrite(STDERR, "==========================\n"); - \fwrite(STDERR, "Query: {$query}\n\n{$e->getMessage()}\n"); - \fwrite(STDERR, "==========================\n\n"); + // ignore } catch (RuntimeException) { // ignore } diff --git a/tests/data/baselineFailedQueries.txt b/tests/data/baselineFailedQueries.txt new file mode 100644 index 0000000..75ab837 --- /dev/null +++ b/tests/data/baselineFailedQueries.txt @@ -0,0 +1,33 @@ +array_slice_with_large_number_for_end_and_negative_step +array_slice_with_large_number_for_start_end_negative_step +array_slice_with_negative_step +array_slice_with_negative_step_on_partially_overlapping_array +bracket_notation_with_negative_number_on_short_array +bracket_notation_with_number_on_object +bracket_notation_with_quoted_escaped_backslash +bracket_notation_with_quoted_escaped_single_quote +bracket_notation_with_quoted_special_characters_combined +bracket_notation_with_quoted_wildcard_literal_on_object_without_key +bracket_notation_with_wildcard_after_recursive_descent +dot_notation_with_number +dot_notation_with_number_-1 +dot_notation_with_wildcard_after_recursive_descent +filter_expression_with_boolean_and_operator +filter_expression_with_boolean_or_operator +filter_expression_with_bracket_notation_with_-1 +filter_expression_with_equals +filter_expression_with_equals_false +filter_expression_with_equals_null +filter_expression_with_equals_number_with_fraction +filter_expression_with_equals_true +filter_expression_with_equals_with_root_reference +filter_expression_with_greater_than +filter_expression_with_greater_than_or_equal +filter_expression_with_less_than +filter_expression_with_less_than_or_equal +filter_expression_with_not_equals +filter_expression_with_value +script_expression +union_with_filter +union_with_repeated_matches_after_dot_notation_with_wildcard +union_with_slice_and_number