data-transfer-object/tests/FailedValidation/FailsMethodTest.php
2026-02-19 08:44:49 -03:00

108 lines
3.3 KiB
PHP

<?php
namespace Tests\FailedValidation;
use Illuminate\Validation\ValidationException;
use Tests\Classes\FailsReturnsDefault;
use Tests\Classes\FailsReturnsNull;
use Tests\Classes\FailsWithHttpResponse;
use Tests\Classes\PrimitiveData;
describe('fails method behavior', function () {
it('throws ValidationException when class does not implement fails()', function () {
expect(function () {
PrimitiveData::fromArray([
'int' => 0,
'float' => 3.14,
'bool' => true,
]);
})->toThrow(ValidationException::class);
});
it('returns null when fails() returns null', function () {
$result = FailsReturnsNull::fromArray([
'int' => 0,
]);
expect($result)->toBeNull();
});
it('returns static instance when fails() returns an object', function () {
$result = FailsReturnsDefault::fromArray([
'int' => 0,
]);
expect($result)->toBeInstanceOf(FailsReturnsDefault::class);
expect($result->string)->toBe('default_value');
expect($result->int)->toBe(42);
});
});
describe('HTTP request handling', function () {
beforeEach(function () {
\Illuminate\Support\Facades\Route::post('/test-validation-exception', function () {
PrimitiveData::fromArray([
'int' => 0,
'float' => 3.14,
'bool' => true,
]);
return response()->json(['success' => true]);
});
\Illuminate\Support\Facades\Route::post('/test-http-response-exception', function () {
FailsWithHttpResponse::fromArray([
'int' => 0,
]);
return response()->json(['success' => true]);
});
\Illuminate\Support\Facades\Route::post('/test-validation-exception-html', function () {
PrimitiveData::fromArray([
'int' => 0,
'float' => 3.14,
'bool' => true,
]);
return response('success');
});
});
it('returns 422 with errors when ValidationException is thrown in JSON request', function () {
$response = $this->postJson('/test-validation-exception', [
'int' => 0,
'float' => 3.14,
'bool' => true,
]);
$response->assertStatus(422);
$response->assertJsonValidationErrors(['string']);
});
it('returns custom JSON response when HttpResponseException is thrown', function () {
$response = $this->postJson('/test-http-response-exception', [
'int' => 0,
]);
$response->assertStatus(422);
$response->assertJsonStructure(['errors']);
$response->assertJsonFragment([
'errors' => [
'string' => ['The string field is required.'],
],
]);
});
it('redirects back with session errors when ValidationException is thrown in text/html request', function () {
$response = $this->post('/test-validation-exception-html', [
'int' => 0,
'float' => 3.14,
'bool' => true,
], [
'Accept' => 'text/html',
]);
$response->assertRedirect();
$response->assertSessionHasErrors(['string']);
});
});