-
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 #399 from tienvx/add-content-type-matcher-class
refactor: Add ContentType matcher class
- Loading branch information
Showing
2 changed files
with
49 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,31 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Model\MatcherInterface; | ||
|
||
/** | ||
* Match binary data by its content type (magic file check) | ||
*/ | ||
class ContentType implements MatcherInterface | ||
{ | ||
public function __construct(private string $contentType) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'value' => $this->contentType, | ||
'pact:matcher:type' => $this->getType(), | ||
]; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'contentType'; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/PhpPact/Consumer/Matcher/Matchers/ContentTypeTest.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,18 @@ | ||
<?php | ||
|
||
namespace PhpPactTest\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Matchers\ContentType; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ContentTypeTest extends TestCase | ||
{ | ||
public function testSerialize(): void | ||
{ | ||
$contentType = new ContentType('text/csv'); | ||
$this->assertSame( | ||
'{"value":"text\/csv","pact:matcher:type":"contentType"}', | ||
json_encode($contentType) | ||
); | ||
} | ||
} |