Skip to content

Commit

Permalink
Merge pull request #412 from tienvx/add-status-code-matcher-class
Browse files Browse the repository at this point in the history
refactor: Add StatusCode matcher class
  • Loading branch information
tienvx committed Dec 17, 2023
2 parents 6f0c0a0 + 3da9618 commit eb1ce56
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpPact\Consumer\Matcher\Exception;

class InvalidHttpStatusException extends MatcherException
{
}
53 changes: 53 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matchers/StatusCode.php
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 tests/PhpPact/Consumer/Matcher/Matchers/StatusCodeTest.php
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));
}
}

0 comments on commit eb1ce56

Please sign in to comment.