-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from tienvx/add-array-contains-matcher-class
refactor: Add ArrayContains matcher class
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
tests/PhpPact/Consumer/Matcher/Matchers/ArrayContainsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |