Skip to content

Commit

Permalink
refactor: Add Type 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 b276a14
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/Type.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;

/**
* This executes a type based match against the values, that is, they are equal if they are the same type.
*/
class Type implements MatcherInterface
{
/**
* @param object|array<mixed>|string|float|int|bool|null $value
*/
public function __construct(private object|array|string|float|int|bool|null $value)
{
}

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

public function getType(): string
{
return 'type';
}
}
19 changes: 19 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/TypeTest.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\Type;
use PHPUnit\Framework\TestCase;

class TypeTest extends TestCase
{
public function testSerialize(): void
{
$value = (object) ['key' => 'value'];
$object = new Type($value);
$this->assertSame(
'{"pact:matcher:type":"type","value":{"key":"value"}}',
json_encode($object)
);
}
}

0 comments on commit b276a14

Please sign in to comment.