From 17ff6fcf2ef6a9358588a74570cc88fd77cafee1 Mon Sep 17 00:00:00 2001 From: Nick Brink Date: Thu, 1 Mar 2018 08:58:54 -0600 Subject: [PATCH] Add pact folder to git ignore. --- .php_cs | 1 - .../Exception/MissingEnvVariableException.php | 17 +++++++++ .../Standalone/Installer/InstallManager.php | 2 +- .../MockService/MockServerConfig.php | 2 +- .../MockService/MockServerConfigInterface.php | 4 +- .../MockService/MockServerEnvConfig.php | 38 ++++++++++++++++--- 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/PhpPact/Standalone/Exception/MissingEnvVariableException.php diff --git a/.php_cs b/.php_cs index e7591cd4..4b762263 100644 --- a/.php_cs +++ b/.php_cs @@ -120,7 +120,6 @@ return PhpCsFixer\Config::create() 'return_type_declaration' => ['space_before' => 'none'], 'self_accessor' => true, 'short_scalar_cast' => true, - 'simplified_null_return' => true, 'single_blank_line_at_eof' => true, 'single_import_per_statement' => true, 'single_line_after_imports' => true, diff --git a/src/PhpPact/Standalone/Exception/MissingEnvVariableException.php b/src/PhpPact/Standalone/Exception/MissingEnvVariableException.php new file mode 100644 index 00000000..7e617191 --- /dev/null +++ b/src/PhpPact/Standalone/Exception/MissingEnvVariableException.php @@ -0,0 +1,17 @@ +destinationDir = __DIR__.'/../../../../'; + $this->destinationDir = __DIR__ . '/../../../..'; $this ->registerInstaller(new InstallerWindows()) ->registerInstaller(new InstallerMac()) diff --git a/src/PhpPact/Standalone/MockService/MockServerConfig.php b/src/PhpPact/Standalone/MockService/MockServerConfig.php index f0757446..82046d3f 100644 --- a/src/PhpPact/Standalone/MockService/MockServerConfig.php +++ b/src/PhpPact/Standalone/MockService/MockServerConfig.php @@ -186,7 +186,7 @@ public function getPactDir() /** * @inheritDoc */ - public function setPactDir(string $pactDir): MockServerConfigInterface + public function setPactDir($pactDir): MockServerConfigInterface { $this->pactDir = $pactDir; diff --git a/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php b/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php index 7b6ddef4..b01037a8 100644 --- a/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php +++ b/src/PhpPact/Standalone/MockService/MockServerConfigInterface.php @@ -81,11 +81,11 @@ public function setProvider(string $provider): self; public function getPactDir(); /** - * @param string $pactDir url to place the pact files when written to disk + * @param null|string $pactDir url to place the pact files when written to disk * * @return MockServerConfigInterface */ - public function setPactDir(string $pactDir): self; + public function setPactDir($pactDir): self; /** * @return string 'merge' or 'overwrite' merge means that interactions are added and overwrite means that the entire file is overwritten diff --git a/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php b/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php index b6f59676..da0fcec2 100644 --- a/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php +++ b/src/PhpPact/Standalone/MockService/MockServerEnvConfig.php @@ -2,15 +2,43 @@ namespace PhpPact\Standalone\MockService; +use PhpPact\Consumer\Exception\MissingEnvVariableException; + +/** + * An environment variable based mock server configuration. + * Class MockServerEnvConfig + */ class MockServerEnvConfig extends MockServerConfig { public function __construct() { $this - ->setHost(\getenv('PACT_MOCK_SERVER_HOST')) - ->setPort(\getenv('PACT_MOCK_SERVER_PORT')) - ->setConsumer(\getenv('PACT_CONSUMER_NAME')) - ->setProvider(\getenv('PACT_PROVIDER_NAME')) - ->setPactDir(\getenv('PACT_OUTPUT_DIR')); + ->setHost($this->parseEnv('PACT_MOCK_SERVER_HOST')) + ->setPort($this->parseEnv('PACT_MOCK_SERVER_PORT')) + ->setConsumer($this->parseEnv('PACT_CONSUMER_NAME')) + ->setProvider($this->parseEnv('PACT_PROVIDER_NAME')) + ->setPactDir($this->parseEnv('PACT_OUTPUT_DIR', false)); + } + + /** + * Parse environmental variables to be either null if not required or throw an error if required. + * + * @param string $variableName + * @param bool $required + * + * @throws MissingEnvVariableException + * + * @return null|string + */ + private function parseEnv(string $variableName, bool $required = true) + { + if (\getenv($variableName) !== false) { + return \getenv($variableName); + } + if ($required === true) { + throw new MissingEnvVariableException($variableName); + } + + return null; } }