Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expects true fails if the throwable is null #180

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
path: src/Assertions.php

-
message: "#^Method Spectator\\\\Assertions\\:\\:expectsTrue\\(\\) invoked with 2 parameters, 0 required\\.$#"
message: "#^Method Spectator\\\\Assertions\\:\\:expectsTrue\\(\\) invoked with 3 parameters, 0 required\\.$#"
count: 2
path: src/Assertions.php

Expand Down
4 changes: 2 additions & 2 deletions src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function assertInvalidRequest()
$this->expectsTrue($exception, [
InvalidPathException::class,
RequestValidationException::class,
]);
], 'Failed asserting that the request is invalid.');

return $this;
});
Expand Down Expand Up @@ -103,7 +103,7 @@ public function assertInvalidResponse()
$this->expectsTrue($exception, [
InvalidPathException::class,
ResponseValidationException::class,
]);
], 'Failed asserting that the response is invalid.');

if ($status) {
$actual = $this->getStatusCode();
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/HasExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function expectsTrue()
* @param array $exceptions
* @return void
*/
return function (?Throwable $throwable = null, array $exceptions = []) {
return function (?Throwable $throwable = null, array $exceptions = [], string $message = '') {
if ($throwable) {
$class = get_class($throwable);

Expand All @@ -44,7 +44,7 @@ public function expectsTrue()
$throwable->getMessage(),
);
} else {
PHPUnit::assertTrue(true);
PHPUnit::assertTrue(false, $message);
}
};
}
Expand Down
19 changes: 14 additions & 5 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,27 @@ protected function validateParameters()
if (isset($expectedParameterSchema->type) && gettype($parameterValue) !== $expectedParameterSchema->type) {
$expectedType = $expectedParameterSchema->type;

if ($expectedType === 'number') {
$expectedType = is_float($parameterValue) ? 'float' : 'int';
$expectedType = match ($expectedType) {
'integer' => 'int',
'number' => 'float',
default => $expectedType,
};

if (is_numeric($parameterValue)) {
$parameterValue = match ($expectedType) {
'int' => (int) $parameterValue,
'float' => (float) $parameterValue,
default => $parameterValue,
};
}

settype($parameterValue, $expectedType);
}

$result = $validator->validate($parameterValue, $expectedParameterSchema);

// If the result is not valid, then display failure reason.
if ($result->isValid() === false) {
$message = RequestValidationException::validationErrorMessage($expectedParameterSchema, $result->error());
$message = RequestValidationException::validationErrorMessage($expectedParameterSchema,
$result->error());

throw RequestValidationException::withError($message, $result->error());
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/AllOf.v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ paths:
owner:
type: string
nullable: true
required:
- id
- owner
- $ref: '#/components/schemas/Dog'

components:
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Enum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ paths:
name: type
required: true
schema:
$refs: '#/components/schemas/TestEnum'
$ref: '#/components/schemas/TestEnum'
get:
summary: Request with enum in path via reference
tags: []
Expand Down
8 changes: 2 additions & 6 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -793,8 +792,6 @@ public function test_enum_in_path(string $type, bool $isValid): void
{
Spectator::using('Enum.yml');

$this->withoutExceptionHandling([BackedEnumCaseNotFoundException::class]);

Route::get('/enum-in-path/{type}', function (TestEnum $type) {
return response()->noContent();
})->middleware([SubstituteBindings::class, Middleware::class]);
Expand All @@ -815,8 +812,6 @@ public function test_enum_in_path_via_reference(string $type, bool $isValid): vo
{
Spectator::using('Enum.yml');

$this->withoutExceptionHandling([BackedEnumCaseNotFoundException::class]);

Route::get('/enum-in-path-via-reference/{type}', function (TestEnum $type) {
return response()->noContent();
})->middleware([SubstituteBindings::class, Middleware::class]);
Expand All @@ -838,7 +833,7 @@ public static function enumProvider(): array
true,
],
'invalid enum' => [
'foo',
'invalid',
false,
],
];
Expand All @@ -853,4 +848,5 @@ enum TestEnum: string
{
case name = 'name';
case email = 'email';
case invalid = 'invalid';
}
32 changes: 24 additions & 8 deletions tests/ResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spectator\Tests;

use ErrorException;
use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -69,6 +70,26 @@ public function test_validates_invalid_json_response(): void
->assertValidationMessage('All array items must match schema');
}

public function test_fails_to_invalidate_valid_json_response(): void
{
Route::get('/users', static function () {
return [
[
'id' => 1,
'name' => 'Jim',
'email' => '[email protected]',
],
];
})->middleware(Middleware::class);

$this->expectException(ErrorException::class);
$this->expectExceptionMessage("Failed asserting that the response is invalid.\nFailed asserting that false is true.");

$this->getJson('/users')
->assertValidRequest()
->assertInvalidResponse();
}

public function test_validates_valid_streamed_json_response(): void
{
Route::get('/users', static function () {
Expand Down Expand Up @@ -421,10 +442,6 @@ public function test_nullable_array_of_nullable_strings($version, $payload, $isV
return ['data' => $payload];
})->middleware(Middleware::class);

$this->getJson('/nullable-array-of-nullable-string')
->assertValidRequest()
->assertValidResponse();

if ($isValid) {
$this->getJson('/nullable-array-of-nullable-string')
->assertValidRequest()
Expand Down Expand Up @@ -462,7 +479,7 @@ public static function nullableArrayOfNullableStringsProvider()
],
'3.0, array with int' => [
$v30,
['foo', null],
[1, null],
$invalidResponse,
],
'3.1, null' => [
Expand All @@ -482,10 +499,9 @@ public static function nullableArrayOfNullableStringsProvider()
],
'3.1, array with int' => [
$v31,
['foo', null],
[1, null],
$invalidResponse,
],

];
}

Expand Down Expand Up @@ -749,7 +765,7 @@ public static function allOfWithNullableProvider(): array
],
$invalid,
],
'invalid, invalid owner missing' => [
'invalid, invalid owner' => [
[
'id' => 1,
'pet_type' => 'Dog',
Expand Down
Loading