Skip to content

Commit

Permalink
Remove duplicated code in config
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed May 12, 2023
1 parent f69b41b commit abf6559
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
195 changes: 195 additions & 0 deletions src/PhpPact/Config/PactConfig.php
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;
}
}
79 changes: 79 additions & 0 deletions src/PhpPact/Config/PactConfigInterface.php
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;
}
2 changes: 1 addition & 1 deletion src/PhpPact/Consumer/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PhpPact\Consumer;

use PhpPact\Consumer\Model\Message;
use PhpPact\Standalone\PactConfigInterface;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Standalone\PactMessage\PactMessage;

/**
Expand Down
Loading

0 comments on commit abf6559

Please sign in to comment.