From 3e75931eea14b9bdce968bf33d446317fd994f61 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 8 Dec 2023 21:18:34 +0700 Subject: [PATCH] refactor: Add generator classes --- .../Exception/InvalidUuidFormatException.php | 9 ++++ .../Matcher/Generators/AbstractDateTime.php | 32 ++++++++++++ .../Consumer/Matcher/Generators/Date.php | 23 +++++++++ .../Consumer/Matcher/Generators/DateTime.php | 23 +++++++++ .../Matcher/Generators/MockServerURL.php | 30 +++++++++++ .../Matcher/Generators/ProviderState.php | 28 +++++++++++ .../Matcher/Generators/RandomBoolean.php | 21 ++++++++ .../Matcher/Generators/RandomDecimal.php | 26 ++++++++++ .../Matcher/Generators/RandomHexadecimal.php | 26 ++++++++++ .../Consumer/Matcher/Generators/RandomInt.php | 27 ++++++++++ .../Matcher/Generators/RandomString.php | 26 ++++++++++ .../Consumer/Matcher/Generators/Regex.php | 26 ++++++++++ .../Consumer/Matcher/Generators/Time.php | 23 +++++++++ .../Consumer/Matcher/Generators/Uuid.php | 50 +++++++++++++++++++ .../Matcher/Model/GeneratorInterface.php | 9 ++++ .../Consumer/Matcher/Generators/DateTest.php | 21 ++++++++ .../Matcher/Generators/DateTimeTest.php | 21 ++++++++ .../Matcher/Generators/MockServerURLTest.php | 18 +++++++ .../Matcher/Generators/ProviderStateTest.php | 18 +++++++ .../Matcher/Generators/RandomBooleanTest.php | 18 +++++++ .../Matcher/Generators/RandomDecimalTest.php | 18 +++++++ .../Generators/RandomHexadecimalTest.php | 18 +++++++ .../Matcher/Generators/RandomIntTest.php | 18 +++++++ .../Matcher/Generators/RandomStringTest.php | 18 +++++++ .../Consumer/Matcher/Generators/RegexTest.php | 18 +++++++ .../Consumer/Matcher/Generators/TimeTest.php | 21 ++++++++ .../Consumer/Matcher/Generators/UuidTest.php | 28 +++++++++++ 27 files changed, 614 insertions(+) create mode 100644 src/PhpPact/Consumer/Matcher/Exception/InvalidUuidFormatException.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/AbstractDateTime.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/Date.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/DateTime.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/MockServerURL.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/ProviderState.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/RandomBoolean.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/RandomDecimal.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/RandomInt.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/RandomString.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/Regex.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/Time.php create mode 100644 src/PhpPact/Consumer/Matcher/Generators/Uuid.php create mode 100644 src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/DateTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/DateTimeTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/MockServerURLTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/ProviderStateTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RandomBooleanTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RandomDecimalTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RandomHexadecimalTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/RegexTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/TimeTest.php create mode 100644 tests/PhpPact/Consumer/Matcher/Generators/UuidTest.php diff --git a/src/PhpPact/Consumer/Matcher/Exception/InvalidUuidFormatException.php b/src/PhpPact/Consumer/Matcher/Exception/InvalidUuidFormatException.php new file mode 100644 index 00000000..04cd925d --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Exception/InvalidUuidFormatException.php @@ -0,0 +1,9 @@ + + */ + public function jsonSerialize(): array + { + $data = ['pact:generator:type' => $this->getType()]; + + if ($this->format !== null) { + $data['format'] = $this->format; + } + + if ($this->expression !== null) { + $data['expression'] = $this->expression; + } + + return $data; + } + + abstract protected function getType(): string; +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/Date.php b/src/PhpPact/Consumer/Matcher/Generators/Date.php new file mode 100644 index 00000000..9caefa82 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/Date.php @@ -0,0 +1,23 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'regex' => $this->regex, + 'example' => $this->example, + 'pact:generator:type' => 'MockServerURL', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/ProviderState.php b/src/PhpPact/Consumer/Matcher/Generators/ProviderState.php new file mode 100644 index 00000000..88b2d675 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/ProviderState.php @@ -0,0 +1,28 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'expression' => $this->expression, + 'pact:generator:type' => 'ProviderState', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/RandomBoolean.php b/src/PhpPact/Consumer/Matcher/Generators/RandomBoolean.php new file mode 100644 index 00000000..872ab29f --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/RandomBoolean.php @@ -0,0 +1,21 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'pact:generator:type' => 'RandomBoolean', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/RandomDecimal.php b/src/PhpPact/Consumer/Matcher/Generators/RandomDecimal.php new file mode 100644 index 00000000..62ed7a8a --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/RandomDecimal.php @@ -0,0 +1,26 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'digits' => $this->digits, + 'pact:generator:type' => 'RandomDecimal', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php b/src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php new file mode 100644 index 00000000..48ec6ed3 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php @@ -0,0 +1,26 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'digits' => $this->digits, + 'pact:generator:type' => 'RandomHexadecimal', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/RandomInt.php b/src/PhpPact/Consumer/Matcher/Generators/RandomInt.php new file mode 100644 index 00000000..df774c87 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/RandomInt.php @@ -0,0 +1,27 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'min' => $this->min, + 'max' => $this->max, + 'pact:generator:type' => 'RandomInt', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/RandomString.php b/src/PhpPact/Consumer/Matcher/Generators/RandomString.php new file mode 100644 index 00000000..f8be32a5 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/RandomString.php @@ -0,0 +1,26 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'size' => $this->size, + 'pact:generator:type' => 'RandomString', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/Regex.php b/src/PhpPact/Consumer/Matcher/Generators/Regex.php new file mode 100644 index 00000000..0625269a --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/Regex.php @@ -0,0 +1,26 @@ + + */ + public function jsonSerialize(): array + { + return [ + 'regex' => $this->regex, + 'pact:generator:type' => 'Regex', + ]; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Generators/Time.php b/src/PhpPact/Consumer/Matcher/Generators/Time.php new file mode 100644 index 00000000..a535ae0a --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Generators/Time.php @@ -0,0 +1,23 @@ + + */ + public function jsonSerialize(): array + { + $data = ['pact:generator:type' => 'Uuid']; + + if ($this->format !== null) { + $data['format'] = $this->format; + } + + return $data; + } +} diff --git a/src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php b/src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php new file mode 100644 index 00000000..7701bdb5 --- /dev/null +++ b/src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php @@ -0,0 +1,9 @@ +assertSame($json, json_encode($date)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/DateTimeTest.php b/tests/PhpPact/Consumer/Matcher/Generators/DateTimeTest.php new file mode 100644 index 00000000..9ae7d95f --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/DateTimeTest.php @@ -0,0 +1,21 @@ +assertSame($json, json_encode($dateTime)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/MockServerURLTest.php b/tests/PhpPact/Consumer/Matcher/Generators/MockServerURLTest.php new file mode 100644 index 00000000..4a7bb3b2 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/MockServerURLTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"regex":".*(\/path)$","example":"http:\/\/localhost:1234\/path","pact:generator:type":"MockServerURL"}', + json_encode($url) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/ProviderStateTest.php b/tests/PhpPact/Consumer/Matcher/Generators/ProviderStateTest.php new file mode 100644 index 00000000..a6a80566 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/ProviderStateTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"expression":"\/products\/${id}","pact:generator:type":"ProviderState"}', + json_encode($url) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RandomBooleanTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RandomBooleanTest.php new file mode 100644 index 00000000..c302f3c0 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RandomBooleanTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"pact:generator:type":"RandomBoolean"}', + json_encode($boolean) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RandomDecimalTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RandomDecimalTest.php new file mode 100644 index 00000000..22aac2a9 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RandomDecimalTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"digits":12,"pact:generator:type":"RandomDecimal"}', + json_encode($decimal) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RandomHexadecimalTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RandomHexadecimalTest.php new file mode 100644 index 00000000..be13ab06 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RandomHexadecimalTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"digits":8,"pact:generator:type":"RandomHexadecimal"}', + json_encode($hexaDecimal) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.php new file mode 100644 index 00000000..70d3ff9d --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RandomIntTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"min":5,"max":15,"pact:generator:type":"RandomInt"}', + json_encode($int) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.php new file mode 100644 index 00000000..2adf26bf --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RandomStringTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"size":11,"pact:generator:type":"RandomString"}', + json_encode($string) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/RegexTest.php b/tests/PhpPact/Consumer/Matcher/Generators/RegexTest.php new file mode 100644 index 00000000..00332a02 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/RegexTest.php @@ -0,0 +1,18 @@ +assertSame( + '{"regex":"[\\\\w\\\\d]+","pact:generator:type":"Regex"}', + json_encode($regex) + ); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/TimeTest.php b/tests/PhpPact/Consumer/Matcher/Generators/TimeTest.php new file mode 100644 index 00000000..d42d4e74 --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/TimeTest.php @@ -0,0 +1,21 @@ +assertSame($json, json_encode($time)); + } +} diff --git a/tests/PhpPact/Consumer/Matcher/Generators/UuidTest.php b/tests/PhpPact/Consumer/Matcher/Generators/UuidTest.php new file mode 100644 index 00000000..c5e08adb --- /dev/null +++ b/tests/PhpPact/Consumer/Matcher/Generators/UuidTest.php @@ -0,0 +1,28 @@ +expectException(InvalidUuidFormatException::class); + $this->expectExceptionMessage('Format invalid is not supported. Supported formats are: simple, lower-case-hyphenated, upper-case-hyphenated, URN'); + } + $uuid = new Uuid($format); + $this->assertSame($json, json_encode($uuid)); + } +}