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 Number matcher class #410

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
33 changes: 33 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PhpPact\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Generators\RandomInt;

/**
* This checks if the type of the value is a number.
*/
class Number extends GeneratorAwareMatcher
{
public function __construct(private int|float|null $value = null)
{
if ($value === null) {
$this->setGenerator(new RandomInt());
}
}

public function getType(): string
{
return 'number';
}

protected function getAttributesData(): array
{
return [];
}

protected function getValue(): int|float|null
{
return $this->value;
}
}
24 changes: 24 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/NumberTest.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\Number;

class NumberTest extends GeneratorAwareMatcherTestCase
{
protected function setUp(): void
{
$this->matcher = new Number();
}

/**
* @testWith [null, "{\"pact:matcher:type\":\"number\",\"pact:generator:type\":\"RandomInt\",\"min\":0,\"max\":10}"]
* [123, "{\"pact:matcher:type\":\"number\",\"value\":123}"]
* [12.3, "{\"pact:matcher:type\":\"number\",\"value\":12.3}"]
*/
public function testSerialize(int|float|null $value, string $json): void
{
$this->matcher = new Number($value);
$this->assertSame($json, json_encode($this->matcher));
}
}