From abf6559aa4a678818f9c775bd9bb51a780f16760 Mon Sep 17 00:00:00 2001 From: tienvx Date: Fri, 12 May 2023 20:41:12 +0700 Subject: [PATCH] Remove duplicated code in config --- .../ExampleMessageConsumerTest.php | 2 +- src/PhpPact/Config/PactConfig.php | 195 ++++++++++++++++++ src/PhpPact/Config/PactConfigInterface.php | 79 +++++++ src/PhpPact/Consumer/MessageBuilder.php | 2 +- .../MockService/MockServerConfig.php | 192 +---------------- .../MockService/MockServerConfigInterface.php | 10 - .../MockService/MockServerEnvConfig.php | 2 - .../PactMessage/PactMessageConfig.php | 150 +------------- tests/PhpPact/Config/PactConfigTest.php | 66 ++++++ .../MockServer/MockServerConfigTest.php | 8 +- .../PactMessage/PactMessageConfigTest.php | 14 ++ 11 files changed, 368 insertions(+), 352 deletions(-) create mode 100644 src/PhpPact/Config/PactConfig.php create mode 100644 src/PhpPact/Config/PactConfigInterface.php create mode 100644 tests/PhpPact/Config/PactConfigTest.php create mode 100644 tests/PhpPact/Standalone/PactMessage/PactMessageConfigTest.php diff --git a/example/tests/MessageConsumer/ExampleMessageConsumerTest.php b/example/tests/MessageConsumer/ExampleMessageConsumerTest.php index 6c17c6ce..e9bda5d0 100644 --- a/example/tests/MessageConsumer/ExampleMessageConsumerTest.php +++ b/example/tests/MessageConsumer/ExampleMessageConsumerTest.php @@ -6,7 +6,7 @@ use Exception; use PhpPact\Consumer\MessageBuilder; -use PhpPact\Standalone\PactConfigInterface; +use PhpPact\Config\PactConfigInterface; use PhpPact\Standalone\PactMessage\PactMessageConfig; use PHPUnit\Framework\TestCase; use stdClass; diff --git a/src/PhpPact/Config/PactConfig.php b/src/PhpPact/Config/PactConfig.php new file mode 100644 index 00000000..a54893f5 --- /dev/null +++ b/src/PhpPact/Config/PactConfig.php @@ -0,0 +1,195 @@ +consumer; + } + + /** + * {@inheritdoc} + */ + public function setConsumer(string $consumer): self + { + $this->consumer = $consumer; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getProvider(): string + { + return $this->provider; + } + + /** + * {@inheritdoc} + */ + public function setProvider(string $provider): self + { + $this->provider = $provider; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPactDir(): string + { + if ($this->pactDir === null) { + return \sys_get_temp_dir(); + } + + return $this->pactDir; + } + + /** + * {@inheritdoc} + */ + public function setPactDir(?string $pactDir): self + { + if ($pactDir === null) { + return $this; + } + + if ('\\' !== \DIRECTORY_SEPARATOR) { + $pactDir = \str_replace('\\', \DIRECTORY_SEPARATOR, $pactDir); + } + + $this->pactDir = $pactDir; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPactFileWriteMode(): string + { + return $this->pactFileWriteMode; + } + + /** + * {@inheritdoc} + */ + public function setPactFileWriteMode(string $pactFileWriteMode): self + { + $options = [self::MODE_OVERWRITE, self::MODE_MERGE]; + + if (!\in_array($pactFileWriteMode, $options)) { + $implodedOptions = \implode(', ', $options); + + throw new \InvalidArgumentException("Invalid PhpPact File Write Mode, value must be one of the following: {$implodedOptions}."); + } + + $this->pactFileWriteMode = $pactFileWriteMode; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getPactSpecificationVersion(): string + { + return $this->pactSpecificationVersion; + } + + /** + * {@inheritdoc} + * + * @throws \UnexpectedValueException + */ + public function setPactSpecificationVersion(string $pactSpecificationVersion): self + { + /* + * Parse the version but do not assign it. If it is an invalid version, an exception is thrown + */ + $parser = new VersionParser(); + $parser->normalize($pactSpecificationVersion); + + $this->pactSpecificationVersion = $pactSpecificationVersion; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getLog(): ?string + { + return $this->log; + } + + /** + * {@inheritdoc} + */ + public function setLog(string $log): self + { + $this->log = $log; + + return $this; + } + + public function getLogLevel(): ?string + { + return $this->logLevel; + } + + public function setLogLevel(string $logLevel): PactConfigInterface + { + $logLevel = \strtoupper($logLevel); + if (!\in_array($logLevel, ['DEBUG', 'INFO', 'WARN', 'ERROR'])) { + throw new \InvalidArgumentException('LogLevel ' . $logLevel . ' not supported.'); + } + $this->logLevel = $logLevel; + + return $this; + } +} diff --git a/src/PhpPact/Config/PactConfigInterface.php b/src/PhpPact/Config/PactConfigInterface.php new file mode 100644 index 00000000..a1597a8c --- /dev/null +++ b/src/PhpPact/Config/PactConfigInterface.php @@ -0,0 +1,79 @@ +secure = $secure; @@ -135,156 +99,6 @@ public function getBaseUri(): UriInterface return new Uri("{$protocol}://{$this->getHost()}:{$this->getPort()}"); } - /** - * {@inheritdoc} - */ - public function getConsumer(): string - { - return $this->consumer; - } - - /** - * {@inheritdoc} - */ - public function setConsumer(string $consumer): self - { - $this->consumer = $consumer; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getProvider(): string - { - return $this->provider; - } - - /** - * {@inheritdoc} - */ - public function setProvider(string $provider): self - { - $this->provider = $provider; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPactDir(): string - { - if ($this->pactDir === null) { - return \sys_get_temp_dir(); - } - - return $this->pactDir; - } - - /** - * {@inheritdoc} - */ - public function setPactDir(?string $pactDir): self - { - if ($pactDir === null) { - return $this; - } - - if ('\\' !== \DIRECTORY_SEPARATOR) { - $pactDir = \str_replace('\\', \DIRECTORY_SEPARATOR, $pactDir); - } - - $this->pactDir = $pactDir; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPactFileWriteMode(): string - { - return $this->pactFileWriteMode; - } - - /** - * {@inheritdoc} - */ - public function setPactFileWriteMode(string $pactFileWriteMode): self - { - $options = ['overwrite', 'merge']; - - if (!\in_array($pactFileWriteMode, $options)) { - $implodedOptions = \implode(', ', $options); - - throw new \InvalidArgumentException("Invalid PhpPact File Write Mode, value must be one of the following: {$implodedOptions}."); - } - - $this->pactFileWriteMode = $pactFileWriteMode; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPactSpecificationVersion(): string - { - return $this->pactSpecificationVersion; - } - - /** - * {@inheritdoc} - */ - public function setPactSpecificationVersion(string $pactSpecificationVersion): self - { - /* - * Parse the version but do not assign it. If it is an invalid version, an exception is thrown - */ - $parser = new VersionParser(); - $parser->normalize($pactSpecificationVersion); - - $this->pactSpecificationVersion = $pactSpecificationVersion; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getLog(): ?string - { - return $this->log; - } - - /** - * {@inheritdoc} - */ - public function setLog(string $log): self - { - $this->log = $log; - - return $this; - } - - public function getLogLevel(): ?string - { - return $this->logLevel; - } - - public function setLogLevel(string $logLevel): PactConfigInterface - { - $logLevel = \strtoupper($logLevel); - if (!\in_array($logLevel, ['DEBUG', 'INFO', 'WARN', 'ERROR'])) { - throw new \InvalidArgumentException('LogLevel ' . $logLevel . ' not supported.'); - } - $this->logLevel = $logLevel; - - return $this; - } - public function hasCors(): bool { return $this->cors; diff --git a/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php b/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php index b61f8d1f..2682d875 100644 --- a/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php +++ b/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php @@ -41,16 +41,6 @@ public function setSecure(bool $secure): self; public function getBaseUri(): UriInterface; - /** - * @return string 'merge' or 'overwrite' merge means that interactions are added and overwrite means that the entire file is overwritten - */ - public function getPactFileWriteMode(): string; - - /** - * @param string $pactFileWriteMode 'merge' or 'overwrite' merge means that interactions are added and overwrite means that the entire file is overwritten - */ - public function setPactFileWriteMode(string $pactFileWriteMode): self; - public function hasCors(): bool; public function setCors(mixed $flag): self; diff --git a/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php b/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php index fae19e83..7a832881 100644 --- a/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php +++ b/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php @@ -9,8 +9,6 @@ */ class MockServerEnvConfig extends MockServerConfig { - public const DEFAULT_SPECIFICATION_VERSION = '2.0.0'; - /** * @throws MissingEnvVariableException */ diff --git a/src/PhpPact/Standalone/PactMessage/PactMessageConfig.php b/src/PhpPact/Standalone/PactMessage/PactMessageConfig.php index d386fe88..56314f6c 100644 --- a/src/PhpPact/Standalone/PactMessage/PactMessageConfig.php +++ b/src/PhpPact/Standalone/PactMessage/PactMessageConfig.php @@ -2,157 +2,11 @@ namespace PhpPact\Standalone\PactMessage; -use Composer\Semver\VersionParser; -use PhpPact\Standalone\PactConfigInterface; +use PhpPact\Config\PactConfig; /** * Configuration defining the default PhpPact Ruby Standalone server. - * Class MockServerConfig. */ -class PactMessageConfig implements PactConfigInterface +class PactMessageConfig extends PactConfig { - /** - * Consumer name. - */ - private string $consumer; - - /** - * Provider name. - */ - private string $provider; - - /** - * Directory to which the pacts will be written. - */ - private ?string $pactDir = null; - - /** - * The pact specification version to use when writing the pact. Note that only versions 1 and 2 are currently supported. - */ - private string $pactSpecificationVersion; - - /** - * File to which to log output. - */ - private string $log; - - private string $logLevel; - - /** - * {@inheritdoc} - */ - public function getConsumer(): string - { - return $this->consumer; - } - - /** - * {@inheritdoc} - */ - public function setConsumer(string $consumer): PactConfigInterface - { - $this->consumer = $consumer; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getProvider(): string - { - return $this->provider; - } - - /** - * {@inheritdoc} - */ - public function setProvider(string $provider): PactConfigInterface - { - $this->provider = $provider; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPactDir(): string - { - if ($this->pactDir === null) { - return \sys_get_temp_dir(); - } - - return $this->pactDir; - } - - /** - * {@inheritdoc} - */ - public function setPactDir($pactDir): PactConfigInterface - { - $this->pactDir = $pactDir; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getPactSpecificationVersion(): string - { - return $this->pactSpecificationVersion; - } - - /** - * {@inheritdoc} - * - * @throws \UnexpectedValueException - */ - public function setPactSpecificationVersion($pactSpecificationVersion): PactConfigInterface - { - /* - * Parse the version but do not assign it. If it is an invalid version, an exception is thrown - */ - $parser = new VersionParser(); - $parser->normalize($pactSpecificationVersion); - - $this->pactSpecificationVersion = $pactSpecificationVersion; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getLog(): string - { - return $this->log; - } - - /** - * {@inheritdoc} - */ - public function setLog(string $log): PactConfigInterface - { - $this->log = $log; - - return $this; - } - - public function getLogLevel(): string - { - return $this->logLevel; - } - - public function setLogLevel(string $logLevel): PactConfigInterface - { - $logLevel = \strtoupper($logLevel); - if (!\in_array($logLevel, ['DEBUG', 'INFO', 'WARN', 'ERROR'])) { - throw new \InvalidArgumentException('LogLevel ' . $logLevel . ' not supported.'); - } - $this->logLevel = $logLevel; - - return $this; - } } diff --git a/tests/PhpPact/Config/PactConfigTest.php b/tests/PhpPact/Config/PactConfigTest.php new file mode 100644 index 00000000..9b43621c --- /dev/null +++ b/tests/PhpPact/Config/PactConfigTest.php @@ -0,0 +1,66 @@ +config = new PactConfig(); + } + + public function testSetters(): void + { + $provider = 'test-provider'; + $consumer = 'test-consumer'; + $pactDir = 'test-pact-dir/'; + $pactSpecificationVersion = '2.0.0'; + $log = 'test-log-dir/'; + $logLevel = 'ERROR'; + $pactFileWriteMode = 'merge'; + + $this->config + ->setProvider($provider) + ->setConsumer($consumer) + ->setPactDir($pactDir) + ->setPactSpecificationVersion($pactSpecificationVersion) + ->setLog($log) + ->setLogLevel($logLevel) + ->setPactFileWriteMode($pactFileWriteMode); + + static::assertSame($provider, $this->config->getProvider()); + static::assertSame($consumer, $this->config->getConsumer()); + static::assertSame($pactDir, $this->config->getPactDir()); + static::assertSame($pactSpecificationVersion, $this->config->getPactSpecificationVersion()); + static::assertSame($log, $this->config->getLog()); + static::assertSame($logLevel, $this->config->getLogLevel()); + static::assertSame($pactFileWriteMode, $this->config->getPactFileWriteMode()); + } + + public function testInvalidPactSpecificationVersion(): void + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Invalid version string "invalid"'); + $this->config->setPactSpecificationVersion('invalid'); + } + + public function testInvalidLogLevel(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('LogLevel VERBOSE not supported.'); + $this->config->setLogLevel('VERBOSE'); + } + + public function testInvalidPactFileWriteMode(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid PhpPact File Write Mode, value must be one of the following: overwrite, merge."); + $this->config->setPactFileWriteMode('APPEND'); + } +} diff --git a/tests/PhpPact/Standalone/MockServer/MockServerConfigTest.php b/tests/PhpPact/Standalone/MockServer/MockServerConfigTest.php index 30a43b0b..2bca2957 100644 --- a/tests/PhpPact/Standalone/MockServer/MockServerConfigTest.php +++ b/tests/PhpPact/Standalone/MockServer/MockServerConfigTest.php @@ -17,6 +17,8 @@ public function testSetters() $pactFileWriteMode = 'merge'; $log = 'test-log-dir/'; $cors = true; + $healthCheckTimeout = 11; + $healthCheckRetrySec = 22; $pactSpecificationVersion = '2.0'; $subject = (new MockServerConfig()) @@ -28,7 +30,9 @@ public function testSetters() ->setPactFileWriteMode($pactFileWriteMode) ->setLog($log) ->setPactSpecificationVersion($pactSpecificationVersion) - ->setCors($cors); + ->setCors($cors) + ->setHealthCheckTimeout($healthCheckTimeout) + ->setHealthCheckRetrySec($healthCheckRetrySec); static::assertSame($host, $subject->getHost()); static::assertSame($port, $subject->getPort()); @@ -39,5 +43,7 @@ public function testSetters() static::assertSame($log, $subject->getLog()); static::assertSame($pactSpecificationVersion, $subject->getPactSpecificationVersion()); static::assertSame($cors, $subject->hasCors()); + static::assertSame($healthCheckTimeout, $subject->getHealthCheckTimeout()); + static::assertSame($healthCheckRetrySec, $subject->getHealthCheckRetrySec()); } } diff --git a/tests/PhpPact/Standalone/PactMessage/PactMessageConfigTest.php b/tests/PhpPact/Standalone/PactMessage/PactMessageConfigTest.php new file mode 100644 index 00000000..e03aca21 --- /dev/null +++ b/tests/PhpPact/Standalone/PactMessage/PactMessageConfigTest.php @@ -0,0 +1,14 @@ +config = new PactMessageConfig(); + } +}