From 5c3f3cbefa23a0aa3d42bbb506d4191813c63e64 Mon Sep 17 00:00:00 2001 From: mtomala Date: Fri, 9 Oct 2020 15:29:23 +0200 Subject: [PATCH] include ArrayMatcher in OrMatcher to fix issues with `@null@||@array@` pattern (#214) Co-authored-by: michal --- src/Factory/MatcherFactory.php | 17 +++++++++++++++-- tests/OrMatcherTest.php | 8 +++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Factory/MatcherFactory.php b/src/Factory/MatcherFactory.php index bc65b75b..a047a6b7 100644 --- a/src/Factory/MatcherFactory.php +++ b/src/Factory/MatcherFactory.php @@ -23,6 +23,7 @@ protected function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher { $scalarMatchers = $this->buildScalarMatchers($parser, $backtrace); $arrayMatcher = $this->buildArrayMatcher($scalarMatchers, $parser, $backtrace); + $orMatcher = $this->buildOrMatcher([$scalarMatchers, $arrayMatcher], $backtrace); // Matchers are registered in order of matching // 1) all scalars @@ -39,7 +40,7 @@ protected function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher new Matcher\JsonMatcher($arrayMatcher, $backtrace), new Matcher\XmlMatcher($arrayMatcher, $backtrace), $arrayMatcher, - new Matcher\OrMatcher($backtrace, $scalarMatchers), + $orMatcher, new Matcher\TextMatcher($scalarMatchers, $backtrace, $parser), ] ); @@ -88,7 +89,19 @@ protected function buildScalarMatchers(Parser $parser, Backtrace $backtrace) : M ); } - protected function buildParser(Backtrace $backtrace) : Parser + private function buildOrMatcher(array $orMatchers, Backtrace $backtrace) : Matcher\OrMatcher + { + return new Matcher\OrMatcher( + $backtrace, + new Matcher\ChainMatcher( + 'or', + $backtrace, + $orMatchers + ) + ); + } + + private function buildParser(Backtrace $backtrace) : Parser { return new Parser(new Lexer(), new Parser\ExpanderInitializer($backtrace)); } diff --git a/tests/OrMatcherTest.php b/tests/OrMatcherTest.php index 7dec1e5b..1ec5d5bc 100644 --- a/tests/OrMatcherTest.php +++ b/tests/OrMatcherTest.php @@ -34,10 +34,16 @@ public static function orExamples() ['ipsum lorem', '@string@.startsWith("lorem")||@string@.contains("lorem")', true], ['norbert@coduo.pl', '@string@.isEmail()||@null@', true], [null, '@string@.isEmail()||@null@', true], - [null, '@string@.isEmail()||@null@', true], ['2014-08-19', '@string@.isDateTime()||@integer@', true], [null, '@integer@||@string@', false], [1, '@integer@.greaterThan(10)||@string@.contains("10")', false], + [[], '@array@||@null@', true], + [[1, 2, 3], '@array@.count(3)||@null@', true], + [null, '@array@||@null@', true], + [null, '@array@.count(3)||@null@', true], + ['ipsum', '@array@||@string@', true], + ['ipsum', '@array@||@null@', false], + [1, '@array@||@null@', false], ]; } }