diff --git a/src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php b/src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php index 4552bacf..a9ca62f9 100644 --- a/src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php +++ b/src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php @@ -28,9 +28,6 @@ public function format(MatcherInterface $matcher): string throw new GeneratorNotRequiredException('Generator is not support in plugin'); } - if ($matcher instanceof MatchingField) { - return $this->formatMatchingFieldMatcher($matcher); - } if ($matcher instanceof NotEmpty) { return $this->formatNotEmptyMatcher($matcher); } @@ -51,7 +48,7 @@ public function format(MatcherInterface $matcher): string throw new MatcherNotSupportedException(sprintf("Matcher '%s' is not supported by plugin", $matcher->getType())); } - private function formatMatchingFieldMatcher(MatchingField $matcher): string + public function formatMatchingFieldMatcher(MatchingField $matcher): string { return sprintf("matching($%s)", $this->normalize($matcher->getFieldName())); } diff --git a/src/PhpPact/Consumer/Matcher/Matchers/MatchingField.php b/src/PhpPact/Consumer/Matcher/Matchers/MatchingField.php index e2c11833..c3ebe669 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/MatchingField.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/MatchingField.php @@ -3,6 +3,7 @@ namespace PhpPact\Consumer\Matcher\Matchers; use PhpPact\Consumer\Matcher\Exception\MatcherNotSupportedException; +use PhpPact\Consumer\Matcher\Formatters\PluginFormatter; class MatchingField extends AbstractMatcher { @@ -34,11 +35,11 @@ protected function getAttributesData(): array public function jsonSerialize(): string { - $result = parent::jsonSerialize(); - if (is_array($result)) { + $formatter = $this->getFormatter(); + if (!$formatter instanceof PluginFormatter) { throw new MatcherNotSupportedException(self::MATCHER_NOT_SUPPORTED_EXCEPTION_MESSAGE); } - return $result; + return $formatter->formatMatchingFieldMatcher($this); } } diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php index bd4080b4..5bb15ca0 100644 --- a/tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php +++ b/tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php @@ -32,14 +32,13 @@ use PhpPact\Consumer\Matcher\Matchers\Time; use PhpPact\Consumer\Matcher\Matchers\Type; use PhpPact\Consumer\Matcher\Matchers\Values; -use PhpPact\Consumer\Matcher\Model\FormatterInterface; use PhpPact\Consumer\Matcher\Model\MatcherInterface; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; class PluginFormatterTest extends TestCase { - private FormatterInterface $formatter; + private PluginFormatter $formatter; protected function setUp(): void { @@ -78,6 +77,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void $this->formatter->format($matcher); } + #[TestWith([new MatchingField('product')])] #[TestWith([new Values([1, 2, 3])])] #[TestWith([new ArrayContains([new Equality(1)])])] #[TestWith([new StatusCode('clientError', 405)])] @@ -88,7 +88,6 @@ public function testNotSupportedMatcher(MatcherInterface $matcher): void $this->formatter->format($matcher); } - #[TestWith([new MatchingField('product'), '"matching($\'product\')"'])] #[TestWith([new NotEmpty('test'), '"notEmpty(\'test\')"'])] #[TestWith([new EachKey(["doesn't matter"], [new Regex('\$(\.\w+)+', '$.test.one')]), '"eachKey(matching(regex, \'\\\\$(\\\\.\\\\w+)+\', \'$.test.one\'))"'])] #[TestWith([new EachValue(["doesn't matter"], [new Type(100)]), '"eachValue(matching(type, 100))"'])] @@ -110,4 +109,12 @@ public function testFormat(MatcherInterface $matcher, string $json): void { $this->assertSame($json, json_encode($this->formatter->format($matcher))); } + + public function testFormatMatchingFieldMatcher(): void + { + $this->assertSame( + "matching($'product')", + $this->formatter->formatMatchingFieldMatcher(new MatchingField('product')) + ); + } }