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

refactor: Apply Visitor pattern for NotEmpty matcher #590

Merged
merged 1 commit into from
May 2, 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: 1 addition & 4 deletions src/PhpPact/Consumer/Matcher/Formatters/PluginFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function format(MatcherInterface $matcher): string
throw new GeneratorNotRequiredException('Generator is not support in plugin');
}

if ($matcher instanceof NotEmpty) {
return $this->formatNotEmptyMatcher($matcher);
}
if ($matcher instanceof EachKey || $matcher instanceof EachValue) {
return $this->formatEachKeyAndEachValueMatchers($matcher);
}
Expand Down Expand Up @@ -71,7 +68,7 @@ private function formatMatchersWithConfig(AbstractDateTime|Regex|ContentType $ma
return sprintf("matching(%s, %s, %s)", $matcher->getType(), $this->normalize($config), $this->normalize($matcher->getValue()));
}

private function formatNotEmptyMatcher(NotEmpty $matcher): string
public function formatNotEmptyMatcher(NotEmpty $matcher): string
{
return sprintf('notEmpty(%s)', $this->normalize($matcher->getValue()));
}
Expand Down
15 changes: 15 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Formatters\PluginFormatter;

/**
* Value must be present and not empty (not null or the empty string)
*/
Expand Down Expand Up @@ -32,4 +34,17 @@ public function getType(): string
{
return 'notEmpty';
}

/**
* @return string|array<string, mixed>
*/
public function jsonSerialize(): string|array
{
$formatter = $this->getFormatter();
if ($formatter instanceof PluginFormatter) {
return $formatter->formatNotEmptyMatcher($this);
}

return parent::jsonSerialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function testInvalidValue(MatcherInterface $matcher, string $type): void
}

#[TestWith([new MatchingField('product')])]
#[TestWith([new NotEmpty('test')])]
#[TestWith([new Values([1, 2, 3])])]
#[TestWith([new ArrayContains([new Equality(1)])])]
#[TestWith([new StatusCode('clientError', 405)])]
Expand All @@ -88,7 +89,6 @@ public function testNotSupportedMatcher(MatcherInterface $matcher): void
$this->formatter->format($matcher);
}

#[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))"'])]
#[TestWith([new Equality('Example value'), '"matching(equalTo, \'Example value\')"'])]
Expand Down Expand Up @@ -117,4 +117,12 @@ public function testFormatMatchingFieldMatcher(): void
$this->formatter->formatMatchingFieldMatcher(new MatchingField('product'))
);
}

public function testFormatNotEmptyMatcher(): void
{
$this->assertSame(
"notEmpty('simple text')",
$this->formatter->formatNotEmptyMatcher(new NotEmpty('simple text'))
);
}
}
16 changes: 14 additions & 2 deletions tests/PhpPact/Consumer/Matcher/Matchers/NotEmptyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@

namespace PhpPactTest\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Formatters\PluginFormatter;
use PhpPact\Consumer\Matcher\Matchers\NotEmpty;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class NotEmptyTest extends TestCase
{
public function testSerialize(): void
{
$array = new NotEmpty(['some text']);
$matcher = new NotEmpty(['some text']);
$this->assertSame(
'{"pact:matcher:type":"notEmpty","value":["some text"]}',
json_encode($array)
json_encode($matcher)
);
}

public function testSerializeIntoExpression(): void
{
$matcher = new NotEmpty('some text');
$matcher->setFormatter(new PluginFormatter());
$this->assertSame(
"\"notEmpty('some text')\"",
json_encode($matcher)
);
}
}