-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from tienvx/add-semver-matcher-class
refactor: Add Semver matcher class
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |