Repository: hotmeteor/spectator (php)

Issue:
No response object matching returned status code [200] when 201 is expected
I'm being told that there's no 200 status code so a test is failing, but it actually is responding with a 200. It's meant to be 201, which is what my test is checking for (my controller was wrong).

```
  • Tests\Feature\OrdersCreateTest > it creates an order
   ErrorException

  No response object matching returned status code [200].
Failed asserting that true is false.

  at tests/Feature/OrdersCreateTest.php:39
     35▕         ->postJson('/orders/', [
     36▕             'orderedTrees' => 1,
     37▕         ])
     38▕         ->assertValidRequest()
  ➜  39▕         ->assertValidResponse(201);
     40▕ });
     41▕

  1   tests/Feature/OrdersCreateTest.php:39
      Illuminate\Testing\TestResponse::__call()
```


So Spectator is detecting the response doesn't match the expectated status code, its just reporting it wrong in the error message.

```

it('creates an order', function () {
    $this
        ->withToken($this->api_key)
        ->postJson('/orders/', [
            'orderedTrees' => 1,
        ])
        ->assertValidRequest()
        ->assertValidResponse(201);
});
```

Pull request:
Give Http Code assertion more priority in response assertion
Http code assertion has more priority than schema validation when given to `assertValidResponse` and `assertInvalidResponse`.

I also simplify the implementation using Laravel built-in assertion.

Fixes #121 

Patch:
diff --git a/src/Assertions.php b/src/Assertions.php
index 2f8d9f0..c5b7a96 100644
--- a/src/Assertions.php
+++ b/src/Assertions.php
@@ -64,6 +64,10 @@ class Assertions
     public function assertValidResponse()
     {
         return fn ($status = null) => $this->runAssertion(function () use ($status) {
+            if ($status) {
+                $this->assertStatus($status);
+            }
+
             $exception = app('spectator')->responseException;
 
             $this->expectsFalse($exception, [
@@ -75,15 +79,6 @@ class Assertions
                 UnresolvableReferenceException::class,
             ]);
 
-            if ($status) {
-                $actual = $this->getStatusCode();
-
-                PHPUnit::assertTrue(
-                    $actual === $status,
-                    "Expected status code {$status} but received {$actual}."
-                );
-            }
-
             return $this;
         });
     }
@@ -91,6 +86,10 @@ class Assertions
     public function assertInvalidResponse()
     {
         return fn ($status = null) => $this->runAssertion(function () use ($status) {
+            if ($status) {
+                $this->assertStatus($status);
+            }
+
             $exception = app('spectator')->responseException;
 
             $this->expectsFalse($exception, [
@@ -105,15 +104,6 @@ class Assertions
                 ResponseValidationException::class,
             ], 'Failed asserting that the response is invalid.');
 
-            if ($status) {
-                $actual = $this->getStatusCode();
-
-                PHPUnit::assertTrue(
-                    $actual === $status,
-                    "Expected status code {$status} but received {$actual}."
-                );
-            }
-
             return $this;
         });
     }


Test patch:
diff --git a/tests/AssertionsTest.php b/tests/AssertionsTest.php
index cd0efb0..88bce96 100644
--- a/tests/AssertionsTest.php
+++ b/tests/AssertionsTest.php
@@ -82,7 +82,7 @@ class AssertionsTest extends TestCase
 
         $this->expectException(ErrorException::class);
         $this->expectExceptionCode(0);
-        $this->expectExceptionMessage('No response object matching returned status code [500].');
+        $this->expectExceptionMessage('Expected response status code [200] but received 500.');
 
         Route::get('/users', function () {
             throw new \Exception('Explosion');
diff --git a/tests/ResponseValidatorTest.php b/tests/ResponseValidatorTest.php
index 8f6e810..dd3088b 100644
--- a/tests/ResponseValidatorTest.php
+++ b/tests/ResponseValidatorTest.php
@@ -136,6 +136,26 @@ class ResponseValidatorTest extends TestCase
             ->assertValidationMessage('Response body is expected to be empty.');
     }
 
+    public function test_validates_status_code(): void
+    {
+        $this->expectException(ErrorException::class);
+        $this->expectExceptionMessage('Expected response status code [200] but received 201.');
+
+        Route::get('/users', static function () {
+            return response([
+                [
+                    'id' => 1,
+                    'name' => 'Jim',
+                    'email' => 'test@test.test',
+                ],
+            ], 201);
+        })->middleware(Middleware::class);
+
+        $this->getJson('/users')
+            ->assertValidRequest()
+            ->assertValidResponse(200);
+    }
+
     public function test_validates_valid_problem_json_response()
     {
         Route::get('/users', function () {