-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/PhpPact/Consumer/Matcher/Exception/InvalidUuidFormatException.php
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,9 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Exception; | ||
|
||
use Exception; | ||
|
||
class InvalidUuidFormatException extends Exception | ||
{ | ||
} |
32 changes: 32 additions & 0 deletions
32
src/PhpPact/Consumer/Matcher/Generators/AbstractDateTime.php
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,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; | ||
} |
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,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'; | ||
} | ||
} |
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,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'; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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
26
src/PhpPact/Consumer/Matcher/Generators/RandomHexadecimal.php
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,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', | ||
]; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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', | ||
]; | ||
} | ||
} |
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,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'; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Model; | ||
|
||
use JsonSerializable; | ||
|
||
interface GeneratorInterface extends JsonSerializable | ||
{ | ||
} |
Oops, something went wrong.