Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add Type matcher class #414

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}
}