Skip to content

Commit

Permalink
Merge pull request #407 from tienvx/add-min-type-matcher-class
Browse files Browse the repository at this point in the history
refactor: Add MinType matcher class
  • Loading branch information
tienvx committed Dec 17, 2023
2 parents 6b895c0 + cde0e98 commit 420f8e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/MinType.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 minimum.
*/
class MinType implements MatcherInterface
{
/**
* @param array<mixed> $values
*/
public function __construct(
private array $values,
private int $min,
) {
}

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

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

namespace PhpPactTest\Consumer\Matcher\Matchers;

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

class MinTypeTest extends TestCase
{
public function testSerialize(): void
{
$values = [
123,
34,
5,
];
$array = new MinType($values, 3);
$this->assertSame(
'{"pact:matcher:type":"type","min":3,"value":[123,34,5]}',
json_encode($array)
);
}
}

0 comments on commit 420f8e2

Please sign in to comment.