diff --git a/src/PhpPact/Consumer/Matcher/Formatters/MinimalFormatter.php b/src/PhpPact/Consumer/Matcher/Formatters/MinimalFormatter.php new file mode 100644 index 00000000..61eb095d --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Formatters/MinimalFormatter.php @@ -0,0 +1,20 @@ + + */ + public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array + { + return [ + 'pact:matcher:type' => $matcher->getType(), + ] + $matcher->getAttributes()->getData(); + } +} diff --git a/src/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatter.php b/src/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatter.php new file mode 100644 index 00000000..30aea26d --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatter.php @@ -0,0 +1,26 @@ + + */ + public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array + { + $data = [ + 'pact:matcher:type' => $matcher->getType(), + ]; + + if ($generator) { + return $data + ['pact:generator:type' => $generator->getType()] + $matcher->getAttributes()->merge($generator->getAttributes())->getData(); + } + + return $data + $matcher->getAttributes()->getData() + ['value' => $value]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatter.php b/src/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatter.php new file mode 100644 index 00000000..a1285a6e --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatter.php @@ -0,0 +1,17 @@ + + */ + public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array + { + return parent::format($matcher, $generator, $value) + ['value' => $value]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Matchers/AbstractDateTime.php b/src/PhpPact/Consumer/Matcher/Matchers/AbstractDateTime.php index 47c4ec28..c11dbad9 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/AbstractDateTime.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/AbstractDateTime.php @@ -6,6 +6,7 @@ abstract class AbstractDateTime extends GeneratorAwareMatcher { public function __construct(protected string $format, private ?string $value = null) { + parent::__construct(); } /** diff --git a/src/PhpPact/Consumer/Matcher/Matchers/AbstractMatcher.php b/src/PhpPact/Consumer/Matcher/Matchers/AbstractMatcher.php new file mode 100644 index 00000000..021ae779 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Matchers/AbstractMatcher.php @@ -0,0 +1,49 @@ +formatter = new ValueOptionalFormatter(); + } + + public function setFormatter(FormatterInterface $formatter): void + { + $this->formatter = $formatter; + } + + public function getFormatter(): FormatterInterface + { + return $this->formatter; + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->getFormatter()->format($this, null, $this->getValue()); + } + + public function getAttributes(): Attributes + { + return new Attributes($this, $this->getAttributesData()); + } + + /** + * @return array + */ + abstract protected function getAttributesData(): array; + + abstract protected function getValue(): mixed; +} diff --git a/src/PhpPact/Consumer/Matcher/Matchers/ArrayContains.php b/src/PhpPact/Consumer/Matcher/Matchers/ArrayContains.php index da9368df..f5f04df2 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/ArrayContains.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/ArrayContains.php @@ -2,29 +2,35 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; +use PhpPact\Consumer\Matcher\Formatters\MinimalFormatter; /** * Checks if all the variants are present in an array. */ -class ArrayContains implements MatcherInterface +class ArrayContains extends AbstractMatcher { /** * @param array $variants */ public function __construct(private array $variants) { + $this->setFormatter(new MinimalFormatter()); } /** * @return array */ - public function jsonSerialize(): array + protected function getAttributesData(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'variants' => array_values($this->variants), - ]; + return ['variants' => array_values($this->variants)]; + } + + /** + * @todo Change return type to `null` + */ + protected function getValue(): mixed + { + return null; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Boolean.php b/src/PhpPact/Consumer/Matcher/Matchers/Boolean.php index 0965e378..08e3e398 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Boolean.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Boolean.php @@ -14,6 +14,7 @@ public function __construct(private ?bool $value = null) if ($value === null) { $this->setGenerator(new RandomBoolean()); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/ContentType.php b/src/PhpPact/Consumer/Matcher/Matchers/ContentType.php index 052e6e41..a8d8635d 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/ContentType.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/ContentType.php @@ -2,26 +2,24 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * Match binary data by its content type (magic file check) */ -class ContentType implements MatcherInterface +class ContentType extends AbstractMatcher { public function __construct(private string $contentType) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } - /** - * @return array - */ - public function jsonSerialize(): array + protected function getValue(): string { - return [ - 'value' => $this->contentType, - 'pact:matcher:type' => $this->getType(), - ]; + return $this->contentType; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Decimal.php b/src/PhpPact/Consumer/Matcher/Matchers/Decimal.php index 69d2e54a..de994b2b 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Decimal.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Decimal.php @@ -14,6 +14,7 @@ public function __construct(private ?float $value = null) if ($value === null) { $this->setGenerator(new RandomDecimal()); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/EachKey.php b/src/PhpPact/Consumer/Matcher/Matchers/EachKey.php index d0b67067..8a941234 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/EachKey.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/EachKey.php @@ -7,7 +7,7 @@ /** * Allows defining matching rules to apply to the keys in a map */ -class EachKey implements MatcherInterface +class EachKey extends AbstractMatcher { /** * @param array|object $value @@ -15,18 +15,23 @@ class EachKey implements MatcherInterface */ public function __construct(private object|array $value, private array $rules) { + parent::__construct(); } /** - * @return array + * @return array */ - public function jsonSerialize(): array + protected function getAttributesData(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value, - 'rules' => array_map(fn (MatcherInterface $rule) => $rule, $this->rules), - ]; + return ['rules' => array_map(fn (MatcherInterface $rule) => $rule, $this->rules)]; + } + + /** + * @return array|object + */ + protected function getValue(): object|array + { + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/EachValue.php b/src/PhpPact/Consumer/Matcher/Matchers/EachValue.php index 1533fba1..3b0c8bbd 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/EachValue.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/EachValue.php @@ -7,7 +7,7 @@ /** * Allows defining matching rules to apply to the values in a collection. For maps, delgates to the Values matcher. */ -class EachValue implements MatcherInterface +class EachValue extends AbstractMatcher { /** * @param array|object $value @@ -15,18 +15,23 @@ class EachValue implements MatcherInterface */ public function __construct(private object|array $value, private array $rules) { + parent::__construct(); } /** - * @return array + * @return array */ - public function jsonSerialize(): array + protected function getAttributesData(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value, - 'rules' => array_map(fn (MatcherInterface $rule) => $rule, $this->rules), - ]; + return ['rules' => array_map(fn (MatcherInterface $rule) => $rule, $this->rules)]; + } + + /** + * @return array|object + */ + protected function getValue(): object|array + { + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Equality.php b/src/PhpPact/Consumer/Matcher/Matchers/Equality.php index bbf779ee..e6d12c42 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Equality.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Equality.php @@ -2,29 +2,30 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This is the default matcher, and relies on the equals operator */ -class Equality implements MatcherInterface +class Equality extends AbstractMatcher { /** * @param object|array|string|float|int|bool|null $value */ public function __construct(private object|array|string|float|int|bool|null $value) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } /** - * @return array + * @return object|array|string|float|int|bool|null */ - public function jsonSerialize(): array + protected function getValue(): object|array|string|float|int|bool|null { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value - ]; + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/GeneratorAwareMatcher.php b/src/PhpPact/Consumer/Matcher/Matchers/GeneratorAwareMatcher.php index a194742d..e3cd0174 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/GeneratorAwareMatcher.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/GeneratorAwareMatcher.php @@ -4,12 +4,10 @@ use PhpPact\Consumer\Matcher\Exception\GeneratorNotRequiredException; use PhpPact\Consumer\Matcher\Exception\GeneratorRequiredException; -use PhpPact\Consumer\Matcher\Model\Attributes; use PhpPact\Consumer\Matcher\Model\GeneratorAwareInterface; use PhpPact\Consumer\Matcher\Model\GeneratorInterface; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; -abstract class GeneratorAwareMatcher implements MatcherInterface, GeneratorAwareInterface +abstract class GeneratorAwareMatcher extends AbstractMatcher implements GeneratorAwareInterface { private ?GeneratorInterface $generator = null; @@ -28,37 +26,16 @@ public function getGenerator(): ?GeneratorInterface */ public function jsonSerialize(): array { - $data = [ - 'pact:matcher:type' => $this->getType(), - ]; - if (null === $this->getValue()) { if (!$this->generator) { throw new GeneratorRequiredException(sprintf("Generator is required for matcher '%s' when example value is not set", $this->getType())); } - return $data + ['pact:generator:type' => $this->generator->getType()] + $this->getMergedAttributes()->getData(); + return $this->getFormatter()->format($this, $this->generator, null); } elseif ($this->generator) { throw new GeneratorNotRequiredException(sprintf("Generator '%s' is not required for matcher '%s' when example value is set", $this->generator->getType(), $this->getType())); } - return $data + $this->getAttributes()->getData() + ['value' => $this->getValue()]; - } - - protected function getAttributes(): Attributes - { - return new Attributes($this, $this->getAttributesData()); - } - - protected function getMergedAttributes(): Attributes - { - return $this->getAttributes()->merge($this->generator->getAttributes()); + return $this->getFormatter()->format($this, $this->generator, $this->getValue()); } - - /** - * @return array - */ - abstract protected function getAttributesData(): array; - - abstract protected function getValue(): mixed; } diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Includes.php b/src/PhpPact/Consumer/Matcher/Matchers/Includes.php index 7435d919..094d94a7 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Includes.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Includes.php @@ -2,26 +2,24 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This checks if the string representation of a value contains the substring. */ -class Includes implements MatcherInterface +class Includes extends AbstractMatcher { public function __construct(private string $value) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } - /** - * @return array - */ - public function jsonSerialize(): array + protected function getValue(): string { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value, - ]; + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Integer.php b/src/PhpPact/Consumer/Matcher/Matchers/Integer.php index bacc17a8..3919854c 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Integer.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Integer.php @@ -14,6 +14,7 @@ public function __construct(private ?int $value = null) if ($value === null) { $this->setGenerator(new RandomInt()); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/MaxType.php b/src/PhpPact/Consumer/Matcher/Matchers/MaxType.php index 098c80b9..50a925dc 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/MaxType.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/MaxType.php @@ -2,33 +2,36 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This executes a type based match against the values, that is, they are equal if they are the same type. * In addition, if the values represent a collection, the length of the actual value is compared against the maximum. */ -class MaxType implements MatcherInterface +class MaxType extends AbstractMatcher { /** - * @param array$values + * @param array $values */ public function __construct( private array $values, private int $max, ) { + parent::__construct(); + } + + /** + * @return array + */ + protected function getAttributesData(): array + { + return ['max' => $this->max]; } /** - * @return array + * @return array */ - public function jsonSerialize(): array + protected function getValue(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'max' => $this->max, - 'value' => array_values($this->values), - ]; + return array_values($this->values); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/MinMaxType.php b/src/PhpPact/Consumer/Matcher/Matchers/MinMaxType.php index 35586b24..f87fb753 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/MinMaxType.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/MinMaxType.php @@ -2,13 +2,11 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This executes a type based match against the values, that is, they are equal if they are the same type. * In addition, if the values represent a collection, the length of the actual value is compared against the minimum and maximum. */ -class MinMaxType implements MatcherInterface +class MinMaxType extends AbstractMatcher { /** * @param array $values @@ -18,21 +16,28 @@ public function __construct( private int $min, private int $max, ) { + parent::__construct(); } /** - * @return array + * @return array */ - public function jsonSerialize(): array + protected function getAttributesData(): array { return [ - 'pact:matcher:type' => $this->getType(), - 'min' => $this->min, - 'max' => $this->max, - 'value' => array_values($this->values), + 'min' => $this->min, + 'max' => $this->max, ]; } + /** + * @return array + */ + protected function getValue(): array + { + return array_values($this->values); + } + public function getType(): string { return 'type'; diff --git a/src/PhpPact/Consumer/Matcher/Matchers/MinType.php b/src/PhpPact/Consumer/Matcher/Matchers/MinType.php index cc4673b9..4aabf493 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/MinType.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/MinType.php @@ -2,13 +2,11 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This executes a type based match against the values, that is, they are equal if they are the same type. * In addition, if the values represent a collection, the length of the actual value is compared against the minimum. */ -class MinType implements MatcherInterface +class MinType extends AbstractMatcher { /** * @param array $values @@ -17,6 +15,7 @@ public function __construct( private array $values, private int $min, ) { + parent::__construct(); } /** @@ -24,15 +23,27 @@ public function __construct( */ public function jsonSerialize(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'min' => $this->min, - 'value' => array_values($this->values), - ]; + return $this->getFormatter()->format($this, null, array_values($this->values)); } public function getType(): string { return 'type'; } + + /** + * @return array + */ + protected function getAttributesData(): array + { + return ['min' => $this->min]; + } + + /** + * @return array + */ + protected function getValue(): array + { + return array_values($this->values); + } } diff --git a/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php b/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php index b87ad6d9..85aaf0eb 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/NotEmpty.php @@ -2,29 +2,30 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * Value must be present and not empty (not null or the empty string) */ -class NotEmpty implements MatcherInterface +class NotEmpty extends AbstractMatcher { /** * @param object|array|string|float|int|bool $value */ public function __construct(private object|array|string|float|int|bool $value) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } /** - * @return array + * @return object|array|string|float|int|bool */ - public function jsonSerialize(): array + protected function getValue(): object|array|string|float|int|bool { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value, - ]; + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/NullValue.php b/src/PhpPact/Consumer/Matcher/Matchers/NullValue.php index 5c00f7e7..2aa4cf8d 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/NullValue.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/NullValue.php @@ -2,25 +2,33 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; +use PhpPact\Consumer\Matcher\Formatters\MinimalFormatter; /** * Match if the value is a null value (this is content specific, for JSON will match a JSON null) */ -class NullValue implements MatcherInterface +class NullValue extends AbstractMatcher { - /** - * @return array - */ - public function jsonSerialize(): array + public function __construct() { - return [ - 'pact:matcher:type' => $this->getType(), - ]; + $this->setFormatter(new MinimalFormatter()); } public function getType(): string { return 'null'; } + + protected function getAttributesData(): array + { + return []; + } + + /** + * @todo Change return type to `null` + */ + protected function getValue(): mixed + { + return null; + } } diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Number.php b/src/PhpPact/Consumer/Matcher/Matchers/Number.php index d98b9694..9f45fb4f 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Number.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Number.php @@ -14,6 +14,7 @@ public function __construct(private int|float|null $value = null) if ($value === null) { $this->setGenerator(new RandomInt()); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Regex.php b/src/PhpPact/Consumer/Matcher/Matchers/Regex.php index 2d5fa859..d16582ee 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Regex.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Regex.php @@ -20,6 +20,7 @@ public function __construct( if ($values === null) { $this->setGenerator(new RegexGenerator($this->regex)); } + parent::__construct(); } /** diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Semver.php b/src/PhpPact/Consumer/Matcher/Matchers/Semver.php index 11c63673..231c7f21 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Semver.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Semver.php @@ -14,6 +14,7 @@ public function __construct(private ?string $value = null) if ($value === null) { $this->setGenerator(new Regex('\d+\.\d+\.\d+')); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/StatusCode.php b/src/PhpPact/Consumer/Matcher/Matchers/StatusCode.php index 94690d96..e1132733 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/StatusCode.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/StatusCode.php @@ -31,6 +31,7 @@ public function __construct(private string $status, private ?int $value = null) $this->setGenerator(new RandomInt($min, $max)); } + parent::__construct(); } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php b/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php index 1459d9b0..da0fcc15 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php @@ -2,6 +2,7 @@ namespace PhpPact\Consumer\Matcher\Matchers; +use PhpPact\Consumer\Matcher\Formatters\ValueRequiredFormatter; use PhpPact\Consumer\Matcher\Generators\RandomString; /** @@ -16,6 +17,7 @@ public function __construct(private ?string $value = null) if ($value === null) { $this->setGenerator(new RandomString()); } + $this->setFormatter(new ValueRequiredFormatter()); } public function getType(): string @@ -28,16 +30,7 @@ public function getType(): string */ public function jsonSerialize(): array { - $data = [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->getValue() ?? self::DEFAULT_VALUE, - ]; - - if ($this->getGenerator()) { - return $data + ['pact:generator:type' => $this->getGenerator()->getType()] + $this->getMergedAttributes()->getData(); - } - - return $data; + return $this->getFormatter()->format($this, $this->getGenerator(), $this->getValue()); } protected function getAttributesData(): array @@ -45,8 +38,8 @@ protected function getAttributesData(): array return []; } - protected function getValue(): ?string + protected function getValue(): string { - return $this->value; + return $this->value ?? self::DEFAULT_VALUE; } } diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Type.php b/src/PhpPact/Consumer/Matcher/Matchers/Type.php index 912561fa..b8595330 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Type.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Type.php @@ -2,29 +2,30 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * This executes a type based match against the values, that is, they are equal if they are the same type. */ -class Type implements MatcherInterface +class Type extends AbstractMatcher { /** * @param object|array|string|float|int|bool|null $value */ public function __construct(private object|array|string|float|int|bool|null $value) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } /** - * @return array + * @return object|array|string|float|int|bool|null */ - public function jsonSerialize(): array + protected function getValue(): object|array|string|float|int|bool|null { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->value, - ]; + return $this->value; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Matchers/Values.php b/src/PhpPact/Consumer/Matcher/Matchers/Values.php index 789c4643..11feaf12 100644 --- a/src/PhpPact/Consumer/Matcher/Matchers/Values.php +++ b/src/PhpPact/Consumer/Matcher/Matchers/Values.php @@ -2,29 +2,30 @@ namespace PhpPact\Consumer\Matcher\Matchers; -use PhpPact\Consumer\Matcher\Model\MatcherInterface; - /** * Match the values in a map, ignoring the keys */ -class Values implements MatcherInterface +class Values extends AbstractMatcher { /** * @param array $values */ public function __construct(private array $values) { + parent::__construct(); + } + + protected function getAttributesData(): array + { + return []; } /** - * @return array + * @return array */ - public function jsonSerialize(): array + protected function getValue(): array { - return [ - 'pact:matcher:type' => $this->getType(), - 'value' => $this->values, - ]; + return $this->values; } public function getType(): string diff --git a/src/PhpPact/Consumer/Matcher/Model/FormatterAwareInterface.php b/src/PhpPact/Consumer/Matcher/Model/FormatterAwareInterface.php new file mode 100644 index 00000000..d757cb21 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Model/FormatterAwareInterface.php @@ -0,0 +1,10 @@ + + */ + public function format(MatcherInterface $matcher, ?GeneratorInterface $generator, mixed $value): array; +} diff --git a/src/PhpPact/Consumer/Matcher/Model/MatcherInterface.php b/src/PhpPact/Consumer/Matcher/Model/MatcherInterface.php index df200149..1e4fb48f 100644 --- a/src/PhpPact/Consumer/Matcher/Model/MatcherInterface.php +++ b/src/PhpPact/Consumer/Matcher/Model/MatcherInterface.php @@ -7,4 +7,6 @@ interface MatcherInterface extends JsonSerializable { public function getType(): string; + + public function getAttributes(): Attributes; } diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/MinimalFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/MinimalFormatterTest.php new file mode 100644 index 00000000..3cbcc2c9 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Formatters/MinimalFormatterTest.php @@ -0,0 +1,28 @@ +assertSame([ + 'pact:matcher:type' => 'date', + 'format' => 'yyyy-MM-dd', + ], $formatter->format($matcher, $generator, $value)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatterTest.php new file mode 100644 index 00000000..bb1eaefc --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Formatters/ValueOptionalFormatterTest.php @@ -0,0 +1,25 @@ +assertSame($result, $formatter->format($matcher, $generator, $value)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatterTest.php b/tests/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatterTest.php new file mode 100644 index 00000000..c8dbd387 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Formatters/ValueRequiredFormatterTest.php @@ -0,0 +1,25 @@ +assertSame($result, $formatter->format($matcher, $generator, $value)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/ContentTypeTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/ContentTypeTest.php index 4b837779..4670af8c 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/ContentTypeTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/ContentTypeTest.php @@ -10,7 +10,7 @@ class ContentTypeTest extends TestCase public function testSerialize(): void { $contentType = new ContentType('text/csv'); - $this->assertSame( + $this->assertJsonStringEqualsJsonString( '{"value":"text\/csv","pact:matcher:type":"contentType"}', json_encode($contentType) ); diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/EachKeyTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/EachKeyTest.php index 537d554a..d068e4ab 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/EachKeyTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/EachKeyTest.php @@ -23,7 +23,7 @@ public function testSerialize(): void new Regex('\w{3}'), ]; $eachKey = new EachKey($value, $rules); - $this->assertSame( + $this->assertJsonStringEqualsJsonString( '{"pact:matcher:type":"eachKey","value":{"abc":123,"def":111,"ghi":{"test":"value"}},"rules":[{"pact:matcher:type":"type","value":"string"},{"pact:matcher:type":"regex","pact:generator:type":"Regex","regex":"\\\\w{3}"}]}', json_encode($eachKey) ); diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/EachValueTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/EachValueTest.php index 9356292b..95dc7fb0 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/EachValueTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/EachValueTest.php @@ -21,7 +21,7 @@ public function testSerialize(): void new Regex('\w{2}\d'), ]; $eachValue = new EachValue($value, $rules); - $this->assertSame( + $this->assertJsonStringEqualsJsonString( '{"pact:matcher:type":"eachValue","value":["ab1","cd2","ef9"],"rules":[{"pact:matcher:type":"type","value":"string"},{"pact:matcher:type":"regex","pact:generator:type":"Regex","regex":"\\\\w{2}\\\\d"}]}', json_encode($eachValue) ); diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php index 602bead2..b9ca2805 100644 --- a/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php +++ b/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php @@ -21,6 +21,6 @@ protected function setUp(): void public function testSerialize(?string $value, string $json): void { $this->matcher = new StringValue($value); - $this->assertSame($json, json_encode($this->matcher)); + $this->assertJsonStringEqualsJsonString($json, json_encode($this->matcher)); } }