Skip to content

Commit

Permalink
refactor: Add Values matcher class
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Dec 15, 2023
1 parent 5a59f42 commit ba11d77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/Values.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;

/**
* Match the values in a map, ignoring the keys
*/
class Values implements MatcherInterface
{
/**
* @param array<mixed> $values
*/
public function __construct(private array $values)
{
}

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

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

namespace PhpPactTest\Consumer\Matcher\Matchers;

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

class ValuesTest extends TestCase
{
/**
* @testWith [["value 1", "value 2"], "{\"pact:matcher:type\":\"values\",\"value\":[\"value 1\",\"value 2\"]}"]
* [{"key 1": "value 1", "key 2": "value 2"}, "{\"pact:matcher:type\":\"values\",\"value\":{\"key 1\":\"value 1\",\"key 2\":\"value 2\"}}"]
*/
public function testSerialize(array $values, string $json): void
{
$array = new Values($values);
$this->assertSame($json, json_encode($array));
}
}

0 comments on commit ba11d77

Please sign in to comment.