-
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 #412 from tienvx/add-status-code-matcher-class
refactor: Add StatusCode matcher class
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/PhpPact/Consumer/Matcher/Exception/InvalidHttpStatusException.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,7 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Exception; | ||
|
||
class InvalidHttpStatusException extends MatcherException | ||
{ | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace PhpPact\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidHttpStatusException; | ||
use PhpPact\Consumer\Matcher\Generators\RandomInt; | ||
use PhpPact\Consumer\Matcher\HttpStatus; | ||
|
||
/** | ||
* Matches the response status code. | ||
*/ | ||
class StatusCode extends GeneratorAwareMatcher | ||
{ | ||
public function __construct(private string $status, private ?int $value = null) | ||
{ | ||
if (!in_array($status, HttpStatus::all())) { | ||
throw new InvalidHttpStatusException(sprintf("Status '%s' is not supported. Supported status are: %s", $status, implode(', ', HttpStatus::all()))); | ||
} | ||
|
||
if ($value === null) { | ||
[$min, $max] = match($status) { | ||
HttpStatus::INFORMATION => [100, 199], | ||
HttpStatus::SUCCESS => [200, 299], | ||
HttpStatus::REDIRECT => [300, 399], | ||
HttpStatus::CLIENT_ERROR => [400, 499], | ||
HttpStatus::SERVER_ERROR => [500, 599], | ||
HttpStatus::NON_ERROR => [100, 399], | ||
HttpStatus::ERROR => [400, 599], | ||
default => [100, 199], // Can't happen, just to make PHPStan happy | ||
}; | ||
|
||
$this->setGenerator(new RandomInt($min, $max)); | ||
} | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'statusCode'; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
protected function getAttributesData(): array | ||
{ | ||
return ['status' => $this->status]; | ||
} | ||
|
||
protected function getValue(): ?int | ||
{ | ||
return $this->value; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.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,35 @@ | ||
<?php | ||
|
||
namespace PhpPactTest\Consumer\Matcher\Matchers; | ||
|
||
use PhpPact\Consumer\Matcher\Exception\InvalidHttpStatusException; | ||
use PhpPact\Consumer\Matcher\Matchers\StatusCode; | ||
|
||
class StatusCodeTest extends GeneratorAwareMatcherTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this->matcher = new StatusCode('info'); | ||
} | ||
|
||
/** | ||
* @testWith ["invalid", null, null] | ||
* ["info", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"info\",\"min\":100,\"max\":199}"] | ||
* ["success", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"success\",\"min\":200,\"max\":299}"] | ||
* ["redirect", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"redirect\",\"min\":300,\"max\":399}"] | ||
* ["clientError", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"clientError\",\"min\":400,\"max\":499}"] | ||
* ["serverError", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"serverError\",\"min\":500,\"max\":599}"] | ||
* ["nonError", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"nonError\",\"min\":100,\"max\":399}"] | ||
* ["error", null, "{\"pact:matcher:type\":\"statusCode\",\"pact:generator:type\":\"RandomInt\",\"status\":\"error\",\"min\":400,\"max\":599}"] | ||
* ["info", "123", "{\"pact:matcher:type\":\"statusCode\",\"status\":\"info\",\"value\":123}"] | ||
*/ | ||
public function testSerialize(string $status, ?string $value, ?string $json): void | ||
{ | ||
if (!$json) { | ||
$this->expectException(InvalidHttpStatusException::class); | ||
$this->expectExceptionMessage("Status 'invalid' is not supported. Supported status are: info, success, redirect, clientError, serverError, nonError, error"); | ||
} | ||
$this->matcher = new StatusCode($status, $value); | ||
$this->assertSame($json, json_encode($this->matcher)); | ||
} | ||
} |