Skip to content

Commit

Permalink
Merge pull request #405 from tienvx/add-max-type-matcher-class
Browse files Browse the repository at this point in the history
refactor: Add MaxType matcher class
  • Loading branch information
tienvx authored Dec 17, 2023
2 parents a3d395e + 358fd29 commit 3a9db0a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/MaxType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Model\MatcherInterface;

/**
* This executes a type based match against the values, that is, they are equal if they are the same type.
* In addition, if the values represent a collection, the length of the actual value is compared against the maximum.
*/
class MaxType implements MatcherInterface
{
/**
* @param array<mixed>$values
*/
public function __construct(
private array $values,
private int $max,
) {
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'pact:matcher:type' => $this->getType(),
'max' => $this->max,
'value' => array_values($this->values),
];
}

public function getType(): string
{
return 'type';
}
}
21 changes: 21 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/MaxTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace PhpPactTest\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Matchers\MaxType;
use PHPUnit\Framework\TestCase;

class MaxTypeTest extends TestCase
{
public function testSerialize(): void
{
$values = [
'string value',
];
$array = new MaxType($values, 3);
$this->assertSame(
'{"pact:matcher:type":"type","max":3,"value":["string value"]}',
json_encode($array)
);
}
}

0 comments on commit 3a9db0a

Please sign in to comment.