Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #20 Filter subpaths #78

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Filters/QueryMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Flow\JSONPath\Filters;

use Flow\JSONPath\AccessHelper;
use Flow\JSONPath\JSONPath;
use RuntimeException;

class QueryMatchFilter extends AbstractFilter
Expand Down Expand Up @@ -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;
}
Expand Down
31 changes: 20 additions & 11 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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
}
Expand Down
33 changes: 33 additions & 0 deletions tests/data/baselineFailedQueries.txt
Original file line number Diff line number Diff line change
@@ -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