Skip to content

Commit

Permalink
Merge pull request #5 from oqq/matchers-for-pact-file
Browse files Browse the repository at this point in the history
Add matchers for pact file
  • Loading branch information
oqq authored Jan 6, 2021
2 parents fe3a446 + 59ad099 commit 2ac1db3
Show file tree
Hide file tree
Showing 38 changed files with 1,039 additions and 233 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require-dev": {
"infection/infection": "^0.20.2",
"phpunit/phpunit": "^9.4",
"psalm/plugin-phpunit": "^0.13.0",
"psalm/plugin-phpunit": "^0.15.0",
"roave/security-advisories": "dev-latest",
"vimeo/psalm": "^4.2"
},
Expand Down
155 changes: 79 additions & 76 deletions composer.lock

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions src/Definition/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class Matcher
{
/** @var array<string, class-string<Matcher>> */
private const MATCHER_TYPES = [
Matcher\Collection::MATCH_TYPE => Matcher\Collection::class,
Matcher\Decimal::MATCH_TYPE => Matcher\Decimal::class,
Matcher\Equality::MATCH_TYPE => Matcher\Equality::class,
Matcher\Include_::MATCH_TYPE => Matcher\Include_::class,
Expand All @@ -21,17 +22,28 @@ abstract class Matcher

public static function create(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::oneOf($payload['type'], \array_keys(self::MATCHER_TYPES));
Assert::keyExists($payload, 'match');
Assert::oneOf($payload['match'], \array_keys(self::MATCHER_TYPES));

/** @var key-of<self::MATCHER_TYPES> $type */
$type = $payload['type'];
/** @var array&array{match: key-of<self::MATCHER_TYPES>} $payload */
$type = $payload['match'];
$matcher = self::MATCHER_TYPES[$type];

return $matcher::fromArray($payload);
}

/**
* @param array&array{match: key-of<self::MATCHER_TYPES>} $payload
*/
abstract public static function fromArray(array $payload): self;

/**
* @return array&array{match: key-of<self::MATCHER_TYPES>}
*/
abstract public function toArray(): array;

/**
* @return key-of<self::MATCHER_TYPES>
*/
abstract public function match(): string;
}
49 changes: 49 additions & 0 deletions src/Definition/Matcher/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Oqq\Pact\Definition\Matcher;

use Oqq\Pact\Definition\Matcher;
use Oqq\Pact\Util\Assert;

final class Collection extends Matcher
{
public const MATCH_TYPE = 'collection';
private int $min;

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

Assert::keyExists($payload, 'min');
Assert::integer($payload['min']);
Assert::greaterThanEq($payload['min'], 1);

return new self($payload['min']);
}

public function toArray(): array
{
return [
'match' => self::MATCH_TYPE,
'min' => $this->min,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

public function min(): int
{
return $this->min;
}

private function __construct(int $min)
{
$this->min = $min;
}
}
11 changes: 8 additions & 3 deletions src/Definition/Matcher/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ final class Decimal extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

return new self();
}

public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

private function __construct()
{
}
Expand Down
11 changes: 8 additions & 3 deletions src/Definition/Matcher/Equality.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ final class Equality extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

return new self();
}

public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

private function __construct()
{
}
Expand Down
16 changes: 13 additions & 3 deletions src/Definition/Matcher/Include_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class Include_ extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

Assert::keyExists($payload, 'value');
Assert::string($payload['value']);
Expand All @@ -27,11 +27,21 @@ public static function fromArray(array $payload): self
public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
'value' => $this->value,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

public function value(): string
{
return $this->value;
}

private function __construct(string $value)
{
$this->value = $value;
Expand Down
11 changes: 8 additions & 3 deletions src/Definition/Matcher/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ final class Integer extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

return new self();
}

public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

private function __construct()
{
}
Expand Down
11 changes: 8 additions & 3 deletions src/Definition/Matcher/Null_.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ final class Null_ extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

return new self();
}

public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

private function __construct()
{
}
Expand Down
16 changes: 13 additions & 3 deletions src/Definition/Matcher/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class Regex extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

Assert::keyExists($payload, 'pattern');
Assert::string($payload['pattern']);
Expand All @@ -27,11 +27,21 @@ public static function fromArray(array $payload): self
public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
'pattern' => $this->pattern,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

public function pattern(): string
{
return $this->pattern;
}

private function __construct(string $pattern)
{
$this->pattern = $pattern;
Expand Down
11 changes: 8 additions & 3 deletions src/Definition/Matcher/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ final class Type extends Matcher

public static function fromArray(array $payload): self
{
Assert::keyExists($payload, 'type');
Assert::same($payload['type'], self::MATCH_TYPE);
Assert::keyExists($payload, 'match');
Assert::same($payload['match'], self::MATCH_TYPE);

return new self();
}

public function toArray(): array
{
return [
'type' => self::MATCH_TYPE,
'match' => self::MATCH_TYPE,
];
}

public function match(): string
{
return self::MATCH_TYPE;
}

private function __construct()
{
}
Expand Down
13 changes: 13 additions & 0 deletions src/Definition/MatchingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function toArray(): array
];
}

public function combine(): string
{
return $this->combine;
}

/**
* @return list<Matcher>
*/
public function matchers(): array
{
return $this->matchers;
}

/**
* @param self::COMBINE_TYPE_* $combine
*/
Expand Down
10 changes: 9 additions & 1 deletion src/Definition/MatchingRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Oqq\Pact\Util\Assert;

final class MatchingRules
/**
* @implements \IteratorAggregate<string, MatchingRule>
*/
final class MatchingRules implements \IteratorAggregate
{
/** @var array<string, MatchingRule> */
private array $matchingRules;
Expand All @@ -32,6 +35,11 @@ public function toArray(): array
);
}

public function getIterator(): \Traversable
{
yield from $this->matchingRules;
}

/**
* @param array<string, MatchingRule> $matchingRules
*/
Expand Down
Loading

0 comments on commit 2ac1db3

Please sign in to comment.