Skip to content

Commit

Permalink
Merge pull request #397 from tienvx/add-array-contains-matcher-class
Browse files Browse the repository at this point in the history
refactor: Add ArrayContains matcher class
  • Loading branch information
tienvx committed Dec 17, 2023
2 parents 2fee20c + 25ab971 commit a8673fd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/ArrayContains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Model\MatcherInterface;

/**
* Checks if all the variants are present in an array.
*/
class ArrayContains implements MatcherInterface
{
/**
* @param array<mixed> $variants
*/
public function __construct(private array $variants)
{
}

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

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

namespace PhpPactTest\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Matchers\ArrayContains;
use PhpPact\Consumer\Matcher\Matchers\Integer;
use PhpPact\Consumer\Matcher\Matchers\Type;
use PHPUnit\Framework\TestCase;

class ArrayContainsTest extends TestCase
{
public function testSerialize(): void
{
$variants = [
new Type('string'),
new Integer(),
];
$array = new ArrayContains($variants);
$this->assertSame(
'{"pact:matcher:type":"arrayContains","variants":[{"pact:matcher:type":"type","value":"string"},{"pact:matcher:type":"integer","pact:generator:type":"RandomInt","min":0,"max":10}]}',
json_encode($array)
);
}
}

0 comments on commit a8673fd

Please sign in to comment.