-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from tienvx/remove-duplicated-code-in-config
Remove duplicated code in config
- Loading branch information
Showing
11 changed files
with
368 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<?php | ||
|
||
namespace PhpPact\Config; | ||
|
||
use Composer\Semver\VersionParser; | ||
|
||
class PactConfig implements PactConfigInterface | ||
{ | ||
/** | ||
* Consumer name. | ||
*/ | ||
private string $consumer; | ||
|
||
/** | ||
* Provider name. | ||
*/ | ||
private string $provider; | ||
|
||
/** | ||
* Directory to which the pacts will be written. | ||
*/ | ||
private ?string $pactDir = null; | ||
|
||
/** | ||
* `overwrite` or `merge`. Use `merge` when running multiple mock service | ||
* instances in parallel for the same consumer/provider pair. Ensure the | ||
* pact file is deleted before running tests when using this option so that | ||
* interactions deleted from the code are not maintained in the file. | ||
*/ | ||
private string $pactFileWriteMode = self::MODE_OVERWRITE; | ||
|
||
/** | ||
* 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 = null; | ||
|
||
private ?string $logLevel = null; | ||
|
||
/** | ||
* {@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 = [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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace PhpPact\Config; | ||
|
||
use InvalidArgumentException; | ||
use UnexpectedValueException; | ||
|
||
/** | ||
* Mock Server configuration interface to allow for simple overrides that are reusable. | ||
*/ | ||
interface PactConfigInterface | ||
{ | ||
public const DEFAULT_SPECIFICATION_VERSION = '2.0.0'; | ||
|
||
public const MODE_OVERWRITE = 'overwrite'; | ||
public const MODE_MERGE = 'merge'; | ||
|
||
public function getConsumer(): string; | ||
|
||
/** | ||
* @param string $consumer consumers name | ||
*/ | ||
public function setConsumer(string $consumer): self; | ||
|
||
public function getProvider(): string; | ||
|
||
/** | ||
* @param string $provider providers name | ||
*/ | ||
public function setProvider(string $provider): self; | ||
|
||
/** | ||
* @return string url to place the pact files when written to disk | ||
*/ | ||
public function getPactDir(): string; | ||
|
||
/** | ||
* @param null|string $pactDir url to place the pact files when written to disk | ||
*/ | ||
public function setPactDir(?string $pactDir): self; | ||
|
||
/** | ||
* @return string pact version | ||
*/ | ||
public function getPactSpecificationVersion(): string; | ||
|
||
/** | ||
* @param string $pactSpecificationVersion pact semver version | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function setPactSpecificationVersion(string $pactSpecificationVersion): self; | ||
|
||
/** | ||
* @return null|string directory for log output | ||
*/ | ||
public function getLog(): ?string; | ||
|
||
/** | ||
* @param string $log directory for log output | ||
*/ | ||
public function setLog(string $log): self; | ||
|
||
public function getLogLevel(): ?string; | ||
|
||
public function setLogLevel(string $logLevel): self; | ||
|
||
/** | ||
* @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 | ||
* | ||
* @throws InvalidArgumentException If mode is incorrect. | ||
*/ | ||
public function setPactFileWriteMode(string $pactFileWriteMode): self; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.