Skip to content

Commit

Permalink
refactor: Create classes for matchers and generators
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Nov 30, 2023
1 parent 2d358b3 commit 86e85f5
Show file tree
Hide file tree
Showing 96 changed files with 2,458 additions and 1,165 deletions.
4 changes: 2 additions & 2 deletions example/generators/consumer/tests/Service/GeneratorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function setUp(): void
$this->matcher = new Matcher();
}

public function testGetMatchers()
public function testGetGenerators(): void
{
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/generators')
->addHeader('Accept', 'application/json')
->setBody([
'id' => $this->matcher->fromProviderState($this->matcher->integer(), '${id}')
'id' => $this->matcher->fromProviderState($this->matcher->integerV3(), '${id}')
]);

$response = new ProviderResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"request": {
"body": {
"content": {
"id": 13
"id": null
},
"contentType": "application/json",
"encoded": false
Expand All @@ -41,7 +41,7 @@
"combine": "AND",
"matchers": [
{
"match": "type"
"match": "integer"
}
]
}
Expand Down Expand Up @@ -114,6 +114,7 @@
"type": "Time"
},
"$.uuid": {
"format": "lower-case-hyphenated",
"type": "Uuid"
}
},
Expand Down
2 changes: 1 addition & 1 deletion example/matchers/consumer/tests/Service/MatchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void
$this->matcher = new Matcher();
}

public function testGetMatchers()
public function testGetMatchers(): void
{
$request = new ConsumerRequest();
$request
Expand Down
8 changes: 6 additions & 2 deletions example/message/consumer/src/ExampleMessageConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ public function processMessage(string $message): void
{
$obj = \json_decode($message);
print " [x] Processing \n";
print ' [x] Text: ' . \print_r($obj->contents->text, true) . "\n";
print ' [x] Number: ' . \print_r($obj->contents->number, true) . "\n";
print " [x] Contents: \n";
print ' [x] Text: ' . \print_r($obj->contents->text, true) . "\n";
print ' [x] Number: ' . \print_r($obj->contents->number, true) . "\n";
print " [x] Metadata: \n";
print ' [x] Queue: ' . \print_r($obj->metadata->queue, true) . "\n";
print ' [x] Routing Key: ' . \print_r($obj->metadata->routing_key, true) . "\n";
print " [x] Processed \n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

use Exception;

class GeneratorRequiredException extends Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

use Exception;

class InvalidHttpStatusException extends Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

use Exception;

class InvalidRegexException extends Exception
{
}
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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

use Exception;

class MatcherNotSupportedException 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',
];
}
}
Loading

0 comments on commit 86e85f5

Please sign in to comment.