Skip to content

Commit

Permalink
refactor: Apply Visitor pattern for NotEmpty matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 2, 2024
1 parent 52ab37a commit e07fb4e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
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)
);
}
}

0 comments on commit e07fb4e

Please sign in to comment.