From 7be55cc00420492a55ce3898a6375e99adb6a13c Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 15 Dec 2023 10:59:56 +0700 Subject: [PATCH] refactor: Add StringValue matcher class --- .../Consumer/Matcher/Matchers/StringValue.php | 50 +++++++++++++++++++ .../Matcher/Matchers/StringValueTest.php | 26 ++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/PhpPact/Consumer/Matcher/Matchers/StringValue.php create mode 100644 tests/PhpPact/Consumer/Matcher/Matchers/StringValueTest.php 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)); + } +}