Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Dec 17, 2023
1 parent 990cfa9 commit fb0730c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Recorders/ValidationErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public function record(RequestHandled $event): void
protected function parseValidationErrors(Request $request, BaseResponse $response): array
{
return $this->parseSessionValidationErrors($request, $response)
?? $this->parseJsonValidationErrors($response, $response)
?? $this->parseJsonValidationErrors($request, $response)
?? $this->parseInertiaValidationErrors($request, $response)
?? $this->parseUnknownValidationErrors($request, $response)
?? [];
}

Expand Down Expand Up @@ -133,4 +134,16 @@ protected function parseInertiaValidationErrors(Request $request, BaseResponse $

return array_keys((array) $response->original['props']['errors']);
}

/**
* Parse unknown validation errors.
*/
protected function parseUnknownValidationErrors(Request $request, BaseResponse $response): ?array
{
if ($response->getStatusCode() !== 422) {
return null;
}

return ['__unknown'];
}
}
25 changes: 21 additions & 4 deletions tests/Feature/Recorders/ValidationErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

use function Pest\Laravel\post;

it('captures session based validation errors', function () {
Pulse::register([ValidationErrors::class => []]);
beforeEach(fn () => Pulse::register([ValidationErrors::class => []]));

it('captures validation errors from the session', function () {
Route::post('users', fn () => Request::validate([
'email' => 'required',
]))->middleware('web');
Expand All @@ -28,8 +29,7 @@
expect($aggregates->pluck('value')->all())->toBe(array_fill(0, 4, '1.00'));
});

it('captures one entry when a single field as multiple errors', function () {
Pulse::register([ValidationErrors::class => []]);
it('captures one entry for field when multiple errors are present for the given field from the session', function () {
Route::post('users', fn () => Request::validate([
'email' => 'string|min:5',
]))->middleware('web');
Expand All @@ -56,3 +56,20 @@
expect($aggregates->pluck('aggregate')->all())->toBe(array_fill(0, 4, 'count'));
expect($aggregates->pluck('value')->all())->toBe(array_fill(0, 4, '1.00'));
});

it('captures a generic error when it is unable to parse the validation error fields from the session', function () {
Route::post('users', fn () => response('<p>An error occurred.</p>', 422))->middleware('web');

$response = post('users');

$response->assertStatus(422);

$entries = Pulse::ignore(fn () => DB::table('pulse_entries')->whereType('validation_error')->get());
expect($entries)->toHaveCount(1);
expect($entries[0]->key)->toBe('["POST","\/users","Closure","__unknown"]');

$aggregates = Pulse::ignore(fn () => DB::table('pulse_aggregates')->whereType('validation_error')->orderBy('period')->get());
expect($aggregates->pluck('key')->all())->toBe(array_fill(0, 4, '["POST","\/users","Closure","__unknown"]'));
expect($aggregates->pluck('aggregate')->all())->toBe(array_fill(0, 4, 'count'));
expect($aggregates->pluck('value')->all())->toBe(array_fill(0, 4, '1.00'));
});

0 comments on commit fb0730c

Please sign in to comment.