-
Notifications
You must be signed in to change notification settings - Fork 91
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 #597 from tienvx/combined-matchers
feat: Allow combining matchers
- Loading branch information
Showing
15 changed files
with
330 additions
and
10 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
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
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
24 changes: 24 additions & 0 deletions
24
src/PhpPact/Consumer/Matcher/Formatters/CombinedMatchersFormatter.php
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,24 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Model\CombinedMatchersInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
class CombinedMatchersFormatter extends ValueOptionalFormatter | ||
{ | ||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function format(MatcherInterface $matcher): array | ||
{ | ||
if ($matcher instanceof CombinedMatchersInterface) { | ||
return [ | ||
'pact:matcher:type' => $matcher->getMatchers(), | ||
'value' => $matcher->getValue(), | ||
]; | ||
} | ||
|
||
return parent::format($matcher); | ||
} | ||
} |
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
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
42 changes: 42 additions & 0 deletions
42
src/PhpPact/Consumer/Matcher/Matchers/CombinedMatchers.php
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,42 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\MatcherNotSupportedException; | ||
use PhpPact\Consumer\Matcher\Formatters\CombinedMatchersFormatter; | ||
use PhpPact\Consumer\Matcher\Model\CombinedMatchersInterface; | ||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
use PhpPact\Consumer\Matcher\Trait\MatchersTrait; | ||
|
||
abstract class CombinedMatchers extends AbstractMatcher implements CombinedMatchersInterface | ||
{ | ||
use MatchersTrait; | ||
|
||
/** | ||
* @param array<mixed>|object $value | ||
* @param MatcherInterface[] $matchers | ||
*/ | ||
public function __construct(private object|array $value, array $matchers) | ||
{ | ||
foreach ($matchers as $matcher) { | ||
if ($matcher instanceof CombinedMatchersInterface) { | ||
throw new MatcherNotSupportedException('Nested combined matchers are not supported'); | ||
} | ||
$this->addMatcher($matcher); | ||
} | ||
$this->setFormatter(new CombinedMatchersFormatter()); | ||
} | ||
|
||
protected function getAttributesData(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @return array<mixed>|object | ||
*/ | ||
public function getValue(): object|array | ||
{ | ||
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,11 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Matchers; | ||
|
||
class MatchAll extends CombinedMatchers | ||
{ | ||
public function getType(): string | ||
{ | ||
return 'matchAll'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PhpPact/Consumer/Matcher/Model/CombinedMatchersInterface.php
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,11 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Model; | ||
|
||
interface CombinedMatchersInterface extends MatcherInterface | ||
{ | ||
/** | ||
* @return MatcherInterface[] | ||
*/ | ||
public function getMatchers(): array; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Trait; | ||
|
||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
trait MatchersTrait | ||
{ | ||
/** | ||
* @var MatcherInterface[] | ||
*/ | ||
private array $matchers = []; | ||
|
||
public function addMatcher(MatcherInterface $matcher): void | ||
{ | ||
$this->matchers[] = $matcher; | ||
} | ||
|
||
/** | ||
* @return MatcherInterface[] | ||
*/ | ||
public function getMatchers(): array | ||
{ | ||
return $this->matchers; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/PhpPact/Consumer/Matcher/Formatters/CombinedMatchersFormatterTest.php
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,30 @@ | ||
<?php | ||
|
||
namespace PhpPactTest\Consumer\Matcher\Formatters; | ||
|
||
use PhpPact\Consumer\Matcher\Formatters\CombinedMatchersFormatter; | ||
use PhpPact\Consumer\Matcher\Matchers\EachKey; | ||
use PhpPact\Consumer\Matcher\Matchers\EachValue; | ||
use PhpPact\Consumer\Matcher\Matchers\Includes; | ||
use PhpPact\Consumer\Matcher\Matchers\Integer; | ||
use PhpPact\Consumer\Matcher\Matchers\MatchAll; | ||
use PhpPact\Consumer\Matcher\Matchers\MaxType; | ||
use PhpPact\Consumer\Matcher\Matchers\MinType; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CombinedMatchersFormatterTest extends TestCase | ||
{ | ||
public function testFormat(): void | ||
{ | ||
$matcher = new MatchAll(['test 123' => 123], [ | ||
new MinType([], 1), | ||
new MaxType([], 2), | ||
new EachKey([], [new Includes('test')]), | ||
new EachValue([], [new Integer(123)]), | ||
]); | ||
$formatter = new CombinedMatchersFormatter(); | ||
$jsonEncoded = json_encode($formatter->format($matcher)); | ||
$this->assertIsString($jsonEncoded); | ||
$this->assertJsonStringEqualsJsonString('{"pact:matcher:type":[{"pact:matcher:type":"type","min":1,"value":[]},{"pact:matcher:type":"type","max":2,"value":[]},{"pact:matcher:type":"eachKey","rules":[{"pact:matcher:type":"include","value":"test"}],"value":[]},{"pact:matcher:type":"eachValue","rules":[{"pact:matcher:type":"integer","value":123}],"value":[]}],"value":{"test 123":123}}', $jsonEncoded); | ||
} | ||
} |
Oops, something went wrong.