BodyMatchers
1 year ago
CoreTestCase.php
1 year ago
HeadersMatcher.php
1 year ago
StatusCodeMatcher.php
1 year ago
TestParam.php
1 year ago
StatusCodeMatcher.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Core\TestCase; |
| 6 | |
| 7 | use PHPUnit\Framework\TestCase; |
| 8 | |
| 9 | class StatusCodeMatcher |
| 10 | { |
| 11 | /** |
| 12 | * @var int|null |
| 13 | */ |
| 14 | private $statusCode; |
| 15 | |
| 16 | /** |
| 17 | * @var int|null |
| 18 | */ |
| 19 | private $lowerStatusCode; |
| 20 | |
| 21 | /** |
| 22 | * @var int|null |
| 23 | */ |
| 24 | private $upperStatusCode; |
| 25 | private $assertStatusRange = false; |
| 26 | private $testCase; |
| 27 | |
| 28 | /** |
| 29 | * Creates a new StatusCodeMatcher object. |
| 30 | */ |
| 31 | public function __construct(TestCase $testCase) |
| 32 | { |
| 33 | $this->testCase = $testCase; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Sets statusCode of the object to the value provided. |
| 38 | */ |
| 39 | public function setStatusCode(int $statusCode): void |
| 40 | { |
| 41 | $this->statusCode = $statusCode; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Sets an expected status code range. Used in case the test case expects a status from a range of status codes. |
| 46 | */ |
| 47 | public function setStatusRange(int $lowerStatusCode, int $upperStatusCode): void |
| 48 | { |
| 49 | $this->assertStatusRange = true; |
| 50 | $this->lowerStatusCode = $lowerStatusCode; |
| 51 | $this->upperStatusCode = $upperStatusCode; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Assert required assertions according to the properties set within the object. |
| 56 | */ |
| 57 | public function assert(int $statusCode) |
| 58 | { |
| 59 | if (isset($this->statusCode)) { |
| 60 | $this->testCase->assertEquals($this->statusCode, $statusCode, "Status is not $this->statusCode"); |
| 61 | return; |
| 62 | } |
| 63 | if (!$this->assertStatusRange) { |
| 64 | return; |
| 65 | } |
| 66 | $message = "Status is not between $this->lowerStatusCode and $this->upperStatusCode"; |
| 67 | $this->testCase->assertGreaterThanOrEqual($this->lowerStatusCode, $statusCode, $message); |
| 68 | $this->testCase->assertLessThanOrEqual($this->upperStatusCode, $statusCode, $message); |
| 69 | } |
| 70 | } |
| 71 |