diff --git a/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php b/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php new file mode 100644 index 00000000..6cbc4bc2 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Matchers/StringValue.php @@ -0,0 +1,50 @@ +setGenerator(new RandomString()); + } + } + + public function getType(): string + { + return 'type'; + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + $data = [ + 'pact:matcher:type' => $this->getType(), + 'value' => $this->getValue() ?? 'some string', + ]; + + if ($this->getGenerator()) { + return $data + ['pact:generator:type' => $this->getGenerator()->getType()] + $this->getMergedAttributes()->getData(); + } + + return $data; + } + + protected function getAttributesData(): array + { + return []; + } + + protected function getValue(): ?string + { + return $this->value; + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php b/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php new file mode 100644 index 00000000..602bead2 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php @@ -0,0 +1,26 @@ +matcher = new StringValue(); + } + + /** + * @testWith [null, "{\"pact:matcher:type\":\"type\",\"value\":\"some string\",\"pact:generator:type\":\"RandomString\",\"size\":10}"] + * ["test", "{\"pact:matcher:type\":\"type\",\"value\":\"test\"}"] + */ + public function testSerialize(?string $value, string $json): void + { + $this->matcher = new StringValue($value); + $this->assertSame($json, json_encode($this->matcher)); + } +}