Skip to content

Commit

Permalink
refactor: Add generator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Dec 8, 2023
1 parent d6dbaf9 commit 3e75931
Show file tree
Hide file tree
Showing 27 changed files with 614 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

use Exception;

class InvalidUuidFormatException extends Exception
{
}
32 changes: 32 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/AbstractDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

abstract class AbstractDateTime implements GeneratorInterface
{
public function __construct(private ?string $format = null, private ?string $expression = null)
{
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
$data = ['pact:generator:type' => $this->getType()];

if ($this->format !== null) {
$data['format'] = $this->format;
}

if ($this->expression !== null) {
$data['expression'] = $this->expression;
}

return $data;
}

abstract protected function getType(): string;
}
23 changes: 23 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

/**
* Generates a date value for the provided format.
* If no format is provided, ISO date format is used.
* If an expression is given, it will be evaluated to generate the date, otherwise 'today' will be used
*
* Example format: yyyy-MM-dd
* Example expression: +1 day
*
* NOTE: Java's datetime format is used, not PHP's datetime format
* For Java one, see https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format#patterns
* For PHP one, see https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
*/
class Date extends AbstractDateTime
{
protected function getType(): string
{
return 'Date';
}
}
23 changes: 23 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

/**
* Generates a datetime value for the provided format.
* If no format is provided, ISO format is used.
* If an expression is given, it will be evaluated to generate the datetime, otherwise 'now' will be used
*
* Example format: yyyy-MM-dd'T'HH:mm:ss
* Example expression: +1 day
*
* NOTE: Java's datetime format is used, not PHP's datetime format
* For Java one, see https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format#patterns
* For PHP one, see https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
*/
class DateTime extends AbstractDateTime
{
protected function getType(): string
{
return 'DateTime';
}
}
30 changes: 30 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/MockServerURL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a URL with the mock server as the base URL.
*
* Example regex: .*(/path)$
* Example example: http://localhost:1234/path
*/
class MockServerURL implements GeneratorInterface
{
public function __construct(private string $regex, private string $example)
{
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return [
'regex' => $this->regex,
'example' => $this->example,
'pact:generator:type' => 'MockServerURL',
];
}
}
28 changes: 28 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/ProviderState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a value that is looked up from the provider state context using the given expression
*
* Example expression: /products/${id}
*/
class ProviderState implements GeneratorInterface
{
public function __construct(private string $expression)
{
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return [
'expression' => $this->expression,
'pact:generator:type' => 'ProviderState',
];
}
}
21 changes: 21 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomBoolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random boolean value
*/
class RandomBoolean implements GeneratorInterface
{
/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return [
'pact:generator:type' => 'RandomBoolean',
];
}
}
26 changes: 26 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomDecimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random big decimal value with the provided number of digits
*/
class RandomDecimal implements GeneratorInterface
{
public function __construct(private int $digits = 10)
{
}

/**
* @return array<string, string|int>
*/
public function jsonSerialize(): array
{
return [
'digits' => $this->digits,
'pact:generator:type' => 'RandomDecimal',
];
}
}
26 changes: 26 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random hexadecimal value of the given number of digits
*/
class RandomHexadecimal implements GeneratorInterface
{
public function __construct(private int $digits = 10)
{
}

/**
* @return array<string, string|int>
*/
public function jsonSerialize(): array
{
return [
'digits' => $this->digits,
'pact:generator:type' => 'RandomHexadecimal',
];
}
}
27 changes: 27 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random integer between the min and max values (inclusive)
*/
class RandomInt implements GeneratorInterface
{
public function __construct(private int $min = 0, private int $max = 10)
{
}

/**
* @return array<string, string|int>
*/
public function jsonSerialize(): array
{
return [
'min' => $this->min,
'max' => $this->max,
'pact:generator:type' => 'RandomInt',
];
}
}
26 changes: 26 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/RandomString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random alphanumeric string of the provided length
*/
class RandomString implements GeneratorInterface
{
public function __construct(private int $size = 10)
{
}

/**
* @return array<string, string|int>
*/
public function jsonSerialize(): array
{
return [
'size' => $this->size,
'pact:generator:type' => 'RandomString',
];
}
}
26 changes: 26 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/Regex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;

/**
* Generates a random string from the provided regular expression
*/
class Regex implements GeneratorInterface
{
public function __construct(private string $regex)
{
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return [
'regex' => $this->regex,
'pact:generator:type' => 'Regex',
];
}
}
23 changes: 23 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/Time.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

/**
* Generates a time value for the provided format.
* If no format is provided, ISO time format is used.
* If an expression is given, it will be evaluated to generate the time, otherwise 'now' will be used
*
* Example format: HH:mm:ss
* Example expression: +1 hour
*
* NOTE: Java's datetime format is used, not PHP's datetime format
* For Java one, see https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format#patterns
* For PHP one, see https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
*/
class Time extends AbstractDateTime
{
protected function getType(): string
{
return 'Time';
}
}
50 changes: 50 additions & 0 deletions src/PhpPact/Consumer/Matcher/Generators/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace PhpPact\Consumer\Matcher\Generators;

use PhpPact\Consumer\Matcher\Model\GeneratorInterface;
use PhpPact\Consumer\Matcher\Exception\InvalidUuidFormatException;

/**
* Generates a random UUID.
* V4 supports specifying the format:
* - simple (e.g 936DA01f9abd4d9d80c702af85c822a8)
* - lower-case-hyphenated (e.g 936da01f-9abd-4d9d-80c7-02af85c822a8)
* - upper-case-hyphenated (e.g 936DA01F-9ABD-4D9D-80C7-02AF85C822A8)
* - URN (e.g. urn:uuid:936da01f-9abd-4d9d-80c7-02af85c822a8)
*/
class Uuid implements GeneratorInterface
{
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';

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)
{
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)));
}
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
$data = ['pact:generator:type' => 'Uuid'];

if ($this->format !== null) {
$data['format'] = $this->format;
}

return $data;
}
}
9 changes: 9 additions & 0 deletions src/PhpPact/Consumer/Matcher/Model/GeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Model;

use JsonSerializable;

interface GeneratorInterface extends JsonSerializable
{
}
Loading

0 comments on commit 3e75931

Please sign in to comment.