Skip to content

Commit

Permalink
Merge pull request #411 from tienvx/add-semver-matcher-class
Browse files Browse the repository at this point in the history
refactor: Add Semver matcher class
  • Loading branch information
tienvx committed Dec 17, 2023
2 parents 2402b71 + 20a104e commit 6f0c0a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/Semver.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\Regex;

/**
* Value must be valid based on the semver specification
*/
class Semver extends GeneratorAwareMatcher
{
public function __construct(private ?string $value = null)
{
if ($value === null) {
$this->setGenerator(new Regex('\d+\.\d+\.\d+'));
}
}

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

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

protected function getValue(): ?string
{
return $this->value;
}
}
23 changes: 23 additions & 0 deletions tests/PhpPact/Consumer/Matcher/Matchers/SemverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PhpPactTest\Consumer\Matcher\Matchers;

use PhpPact\Consumer\Matcher\Matchers\Semver;

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

/**
* @testWith [null, "{\"pact:matcher:type\":\"semver\",\"pact:generator:type\":\"Regex\",\"regex\":\"\\\\d+\\\\.\\\\d+\\\\.\\\\d+\"}"]
* ["1.2.3", "{\"pact:matcher:type\":\"semver\",\"value\":\"1.2.3\"}"]
*/
public function testSerialize(?string $value, string $json): void
{
$this->matcher = new Semver($value);
$this->assertSame($json, json_encode($this->matcher));
}
}

0 comments on commit 6f0c0a0

Please sign in to comment.