Skip to content

Commit

Permalink
Merge pull request #50 from pact-foundation/change-default-bin
Browse files Browse the repository at this point in the history
Fixed some issues with default pact dir and required envs
  • Loading branch information
mattermack authored Mar 1, 2018
2 parents d1a784b + 17ff6fc commit e83f0d3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,5 @@ vendor/*
.settings/
.buildpath

test_results
test_results
pact
1 change: 0 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/PhpPact/Standalone/Exception/MissingEnvVariableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace PhpPact\Consumer\Exception;

use Exception;

/**
* Exception for a required environmental variable.
* Class MissingEnvVariableException
*/
class MissingEnvVariableException extends Exception
{
public function __construct(string $variableName)
{
parent::__construct("Please provide required environmental variable {$variableName}!", 0, null);
}
}
2 changes: 1 addition & 1 deletion src/PhpPact/Standalone/Installer/InstallManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class InstallManager

public function __construct()
{
$this->destinationDir = \sys_get_temp_dir();
$this->destinationDir = __DIR__ . '/../../../..';
$this
->registerInstaller(new InstallerWindows())
->registerInstaller(new InstallerMac())
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPact/Standalone/MockService/MockServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function getPactDir()
/**
* @inheritDoc
*/
public function setPactDir(string $pactDir): MockServerConfigInterface
public function setPactDir($pactDir): MockServerConfigInterface
{
$this->pactDir = $pactDir;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions src/PhpPact/Standalone/MockService/MockServerEnvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e83f0d3

Please sign in to comment.