Skip to content

Commit

Permalink
refactor: Define enums
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Oct 2, 2024
1 parent cb1b59a commit 9bf8c8c
Show file tree
Hide file tree
Showing 28 changed files with 193 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Exception;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\MessageBuilder;
use PhpPact\Standalone\PactMessage\PactMessageConfig;
use PhpPactTest\CompatibilitySuite\Constant\Path;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function __construct(
->setProvider(PactPath::PROVIDER)
->setPactDir(Path::PACTS_PATH)
->setPactSpecificationVersion($specificationVersion)
->setPactFileWriteMode(PactConfigInterface::MODE_OVERWRITE);
->setPactFileWriteMode(WriteMode::OVERWRITE);
$this->builder = new MessageBuilder($config);
}

Expand Down
6 changes: 3 additions & 3 deletions compatibility-suite/tests/Context/V4/CombinedContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PhpPactTest\CompatibilitySuite\Context\V4;

use Behat\Behat\Context\Context;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Model\Message;
use PhpPactTest\CompatibilitySuite\Model\PactPath;
use PhpPactTest\CompatibilitySuite\Service\InteractionBuilderInterface;
Expand Down Expand Up @@ -51,10 +51,10 @@ public function aMessageInteractionIsBeingDefinedForAConsumerTest(): void
*/
public function thePactFileForTheTestIsGenerated(): void
{
$this->pactWriter->write($this->id, $this->pactPath, PactConfigInterface::MODE_MERGE);
$this->pactWriter->write($this->id, $this->pactPath, WriteMode::MERGE);
$message = new Message();
$message->setDescription('message interaction');
$this->messagePactWriter->write($message, $this->pactPath, PactConfigInterface::MODE_MERGE);
$this->messagePactWriter->write($message, $this->pactPath, WriteMode::MERGE);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions compatibility-suite/tests/Service/MessagePactWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Factory\MessageDriverFactory;
use PhpPact\Consumer\Model\Message;
use PhpPact\Standalone\MockService\MockServerConfig;
Expand All @@ -17,7 +17,7 @@ public function __construct(
) {
}

public function write(Message $message, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void
public function write(Message $message, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void
{
$config = new MockServerConfig();
$config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Model\Message;
use PhpPactTest\CompatibilitySuite\Model\PactPath;

interface MessagePactWriterInterface
{
public function write(Message $message, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void;
public function write(Message $message, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void;
}
4 changes: 2 additions & 2 deletions compatibility-suite/tests/Service/PactWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Factory\InteractionDriverFactory;
use PhpPact\Standalone\MockService\MockServerConfig;
use PhpPactTest\CompatibilitySuite\Constant\Path;
Expand All @@ -16,7 +16,7 @@ public function __construct(
) {
}

public function write(int $id, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void
public function write(int $id, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void
{
$config = new MockServerConfig();
$config
Expand Down
4 changes: 2 additions & 2 deletions compatibility-suite/tests/Service/PactWriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPactTest\CompatibilitySuite\Model\PactPath;

interface PactWriterInterface
{
public function write(int $id, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void;
public function write(int $id, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void;
}
4 changes: 2 additions & 2 deletions compatibility-suite/tests/Service/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Driver\Interaction\InteractionDriverInterface;
use PhpPact\Consumer\Factory\InteractionDriverFactory;
use PhpPact\Standalone\MockService\MockServerConfig;
Expand Down Expand Up @@ -30,7 +30,7 @@ public function __construct(
->setProvider(PactPath::PROVIDER)
->setPactDir(Path::PACTS_PATH)
->setPactSpecificationVersion($specificationVersion)
->setPactFileWriteMode(PactConfigInterface::MODE_OVERWRITE);
->setPactFileWriteMode(WriteMode::OVERWRITE);

if ($level = \getenv('PACT_LOGLEVEL')) {
$this->config->setLogLevel($level);
Expand Down
4 changes: 2 additions & 2 deletions compatibility-suite/tests/Service/SyncMessagePactWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Model\Message;
use PhpPact\Standalone\MockService\MockServerConfig;
use PhpPact\SyncMessage\Factory\SyncMessageDriverFactory;
Expand All @@ -16,7 +16,7 @@ public function __construct(
) {
}

public function write(Message $message, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void
public function write(Message $message, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void
{
$config = new MockServerConfig();
$config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace PhpPactTest\CompatibilitySuite\Service;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Consumer\Model\Message;
use PhpPactTest\CompatibilitySuite\Model\PactPath;

interface SyncMessagePactWriterInterface
{
public function write(Message $message, PactPath $pactPath, string $mode = PactConfigInterface::MODE_OVERWRITE): void;
public function write(Message $message, PactPath $pactPath, WriteMode $mode = WriteMode::OVERWRITE): void;
}
15 changes: 15 additions & 0 deletions src/PhpPact/Config/Enum/WriteMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PhpPact\Config\Enum;

enum WriteMode: string
{
/**
* The entire file is overwritten
*/
case OVERWRITE = 'overwrite';
/**
* Interactions are added
*/
case MERGE = 'merge';
}
9 changes: 9 additions & 0 deletions src/PhpPact/Config/Exception/ConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Config\Exception;

use PhpPact\Exception\BaseException;

class ConfigException extends BaseException
{
}
7 changes: 7 additions & 0 deletions src/PhpPact/Config/Exception/InvalidWriteModeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpPact\Config\Exception;

class InvalidWriteModeException extends ConfigException
{
}
25 changes: 15 additions & 10 deletions src/PhpPact/Config/PactConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpPact\Config;

use Composer\Semver\VersionParser;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Config\Exception\InvalidWriteModeException;

class PactConfig implements PactConfigInterface
{
Expand Down Expand Up @@ -40,7 +42,7 @@ class PactConfig implements PactConfigInterface
* 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_MERGE;
private WriteMode $pactFileWriteMode = WriteMode::MERGE;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -155,24 +157,27 @@ public function setLog(string $log): self
/**
* {@inheritdoc}
*/
public function getPactFileWriteMode(): string
public function getPactFileWriteMode(): WriteMode
{
return $this->pactFileWriteMode;
}

/**
* {@inheritdoc}
*/
public function setPactFileWriteMode(string $pactFileWriteMode): self
public function setPactFileWriteMode(string|WriteMode $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}.");
if (is_string($pactFileWriteMode)) {
try {
$pactFileWriteMode = WriteMode::from($pactFileWriteMode);
} catch (\Throwable $th) {
$all = implode(', ', array_map(
fn (WriteMode $mode) => $mode->value,
WriteMode::cases()
));
throw new InvalidWriteModeException(sprintf("Mode '%s' is not supported. Supported modes are: %s", $pactFileWriteMode, $all));
}
}

$this->pactFileWriteMode = $pactFileWriteMode;

return $this;
Expand Down
16 changes: 5 additions & 11 deletions src/PhpPact/Config/PactConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace PhpPact\Config;

use InvalidArgumentException;
use PhpPact\Config\Exception\InvalidWriteModeException;
use PhpPact\Config\Enum\WriteMode;
use UnexpectedValueException;

/**
Expand All @@ -12,8 +13,6 @@ interface PactConfigInterface
{
public const DEFAULT_SPECIFICATION_VERSION = '3.0.0';

public const MODE_OVERWRITE = 'overwrite';
public const MODE_MERGE = 'merge';

public function getConsumer(): string;

Expand Down Expand Up @@ -65,15 +64,10 @@ 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;
public function getPactFileWriteMode(): WriteMode;

/**
* @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.
* @throws InvalidWriteModeException If mode is incorrect.
*/
public function setPactFileWriteMode(string $pactFileWriteMode): self;
public function setPactFileWriteMode(string|WriteMode $pactFileWriteMode): self;
}
3 changes: 2 additions & 1 deletion src/PhpPact/Consumer/Driver/Pact/PactDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpPact\Consumer\Driver\Pact;

use Composer\Semver\Comparator;
use PhpPact\Config\Enum\WriteMode;
use PhpPact\Config\PactConfigInterface;
use PhpPact\Consumer\Driver\Exception\MissingPactException;
use PhpPact\Consumer\Driver\Exception\PactFileNotWrittenException;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function writePact(): void
$error = $this->client->pactHandleWriteFile(
$this->getPact()->handle,
$this->config->getPactDir(),
$this->config->getPactFileWriteMode() === PactConfigInterface::MODE_OVERWRITE
$this->config->getPactFileWriteMode() === WriteMode::OVERWRITE
);
if ($error) {
throw new PactFileNotWrittenException($error);
Expand Down
29 changes: 29 additions & 0 deletions src/PhpPact/Consumer/Matcher/Enum/HttpStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PhpPact\Consumer\Matcher\Enum;

use PhpPact\Consumer\Matcher\Model\Range;

enum HttpStatus: string
{
case INFORMATION = 'info';
case SUCCESS = 'success';
case REDIRECT = 'redirect';
case CLIENT_ERROR = 'clientError';
case SERVER_ERROR = 'serverError';
case NON_ERROR = 'nonError';
case ERROR = 'error';

public function range(): Range
{
return match($this) {
self::INFORMATION => new Range(100, 199),
self::SUCCESS => new Range(200, 299),
self::REDIRECT => new Range(300, 399),
self::CLIENT_ERROR => new Range(400, 499),
self::SERVER_ERROR => new Range(500, 599),
self::NON_ERROR => new Range(100, 399),
self::ERROR => new Range(400, 599),
};
}
}
11 changes: 11 additions & 0 deletions src/PhpPact/Consumer/Matcher/Enum/UuidFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PhpPact\Consumer\Matcher\Enum;

enum UuidFormat: string
{
case SIMPLE_FORMAT = 'simple';
case LOWER_CASE_HYPHENATED_FORMAT = 'lower-case-hyphenated';
case UPPER_CASE_HYPHENATED_FORMAT = 'upper-case-hyphenated';
case URN_FORMAT = 'URN';
}
30 changes: 15 additions & 15 deletions src/PhpPact/Consumer/Matcher/Generators/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Enum\UuidFormat;
use PhpPact\Consumer\Matcher\Exception\InvalidUuidFormatException;

/**
Expand All @@ -14,23 +15,22 @@
*/
class Uuid extends AbstractGenerator
{
public const SIMPLE_FORMAT = 'simple';
public const LOWER_CASE_HYPHENATED_FORMAT = 'lower-case-hyphenated';
public const UPPER_CASE_HYPHENATED_FORMAT = 'upper-case-hyphenated';
public const URN_FORMAT = 'URN';
private null|UuidFormat $format;

public const FORMATS = [
self::SIMPLE_FORMAT,
self::LOWER_CASE_HYPHENATED_FORMAT,
self::UPPER_CASE_HYPHENATED_FORMAT,
self::URN_FORMAT,
];

public function __construct(private ?string $format = null)
public function __construct(null|string|UuidFormat $format = null)
{
if ($format && !in_array($format, self::FORMATS, true)) {
throw new InvalidUuidFormatException(sprintf('Format %s is not supported. Supported formats are: %s', $format, implode(', ', self::FORMATS)));
if (is_string($format)) {
try {
$format = UuidFormat::from($format);
} catch (\Throwable $th) {
$all = implode(', ', array_map(
fn (UuidFormat $status) => $status->value,
UuidFormat::cases()
));
throw new InvalidUuidFormatException(sprintf('Format %s is not supported. Supported formats are: %s', $format, $all));
}
}
$this->format = $format;
}

public function getType(): string
Expand All @@ -44,7 +44,7 @@ public function getType(): string
protected function getAttributesData(): array
{
return $this->format !== null ? [
'format' => $this->format,
'format' => $this->format->value,
] : [];
}
}
Loading

0 comments on commit 9bf8c8c

Please sign in to comment.