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

Revert "refactor: Apply Visitor pattern for MatchingField matcher" #595

Merged
merged 1 commit into from
May 3, 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
5 changes: 4 additions & 1 deletion src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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);
}
Expand All @@ -48,7 +51,7 @@ public function format(MatcherInterface $matcher): string
throw new MatcherNotSupportedException(sprintf("Matcher '%s' is not supported by plugin", $matcher->getType()));
}

public function formatMatchingFieldMatcher(MatchingField $matcher): string
private function formatMatchingFieldMatcher(MatchingField $matcher): string
{
return sprintf("matching($%s)", $this->normalize($matcher->getFieldName()));
}
Expand Down
7 changes: 3 additions & 4 deletions src/PhpPact/Consumer/Matcher/Matchers/MatchingField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Exception\MatcherNotSupportedException;
use PhpPact\Consumer\Matcher\Formatters\PluginFormatter;

class MatchingField extends AbstractMatcher
{
Expand Down Expand Up @@ -35,11 +34,11 @@ protected function getAttributesData(): array

public function jsonSerialize(): string
{
$formatter = $this->getFormatter();
if (!$formatter instanceof PluginFormatter) {
$result = parent::jsonSerialize();
if (is_array($result)) {
throw new MatcherNotSupportedException(self::MATCHER_NOT_SUPPORTED_EXCEPTION_MESSAGE);
}

return $formatter->formatMatchingFieldMatcher($this);
return $result;
}
}
13 changes: 3 additions & 10 deletions tests/PhpPact/Consumer/Matcher/Formatters/PluginFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
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 PluginFormatter $formatter;
private FormatterInterface $formatter;

protected function setUp(): void
{
Expand Down Expand Up @@ -77,7 +78,6 @@ 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)])]
Expand All @@ -88,6 +88,7 @@ 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))"'])]
Expand All @@ -109,12 +110,4 @@ 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'))
);
}
}