Skip to content
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
19 changes: 19 additions & 0 deletions app/Livewire/Dashboard/DashboardConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Models\FileRecord;
use App\Support\Converters\ConverterRegistry;
use App\Support\Converters\DTO\ConverterTarget;
use App\Support\Converters\Exceptions\InvalidConverterOptionsException;
use App\Support\Converters\OptionsValidator;
use App\Support\Files\UploadedFileRules;
use App\ViewModels\TargetFormatCardViewModel;
use Livewire\Component;
Expand Down Expand Up @@ -150,6 +152,23 @@ public function selectTargetFormat(string $targetFormat): void
$this->step = 'settings';
}

public function validateSettings(): bool
{
$this->resetErrorBag();

try {
app(OptionsValidator::class)->validate($this->optionsSchema, $this->options);
} catch (InvalidConverterOptionsException $exception) {
foreach ($exception->fieldErrors() as $field => $message) {
$this->addError("options.{$field}", $message);
}

return false;
}

return true;
}

private function initializeOptionsFromSchema(): void
{
$this->options = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,37 @@

final class InvalidConverterOptionsException extends DomainException
{
public function __construct(string $message, public readonly ?string $optionKey = null)
{
parent::__construct($message);
}

public static function becauseOptionIsUnknown(string $key): self
{
return new self("Unknown converter option: {$key}.");
return new self("Unknown converter option: {$key}.", $key);
}

public static function becauseOptionIsRequired(string $key): self
{
return new self("Converter option [{$key}] is required.");
return new self("Converter option [{$key}] is required.", $key);
}

public static function becauseValueIsNotAllowed(string $key): self
{
return new self("Converter option [{$key}] has an invalid value.");
return new self("Converter option [{$key}] has an invalid value.", $key);
}

/**
* Field-level errors keyed by the offending option key.
*
* @return array<string, string>
*/
public function fieldErrors(): array
{
if ($this->optionKey === null) {
return [];
}

return [$this->optionKey => $this->getMessage()];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<div data-testid="option-field-{{ $fieldKey }}">
@if ($fieldKey !== null && view()->exists($fieldView))
@include($fieldView, ['key' => $fieldKey, 'field' => $field])
@error('options.'.$fieldKey)
<p class="mt-1 text-sm text-[var(--ca-danger)]">{{ $message }}</p>
@enderror
@else
<p class="text-sm text-[var(--ca-danger)]">
Unsupported field type: {{ $field['type'] ?? 'unknown' }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use App\Livewire\Dashboard\DashboardConverter;
use App\Models\FileRecord;
use App\Models\User;
use Livewire\Livewire;

it('shows a validation error for an invalid settings option', function () {
$user = User::factory()->create();
$file = FileRecord::factory()->for($user)->create(['extension' => 'png']);

Livewire::actingAs($user)
->test(DashboardConverter::class)
->set('currentFileId', $file->id)
->call('selectTargetFormat', 'jpg')
->set('options.quality', 'invalid-quality')
->call('validateSettings')
->assertHasErrors(['options.quality']);
});

it('passes validation for the default options', function () {
$user = User::factory()->create();
$file = FileRecord::factory()->for($user)->create(['extension' => 'png']);

Livewire::actingAs($user)
->test(DashboardConverter::class)
->set('currentFileId', $file->id)
->call('selectTargetFormat', 'jpg')
->call('validateSettings')
->assertHasNoErrors();
});

it('rejects an unknown settings option', function () {
$user = User::factory()->create();
$file = FileRecord::factory()->for($user)->create(['extension' => 'png']);

Livewire::actingAs($user)
->test(DashboardConverter::class)
->set('currentFileId', $file->id)
->call('selectTargetFormat', 'jpg')
->set('options.bogus', 'value')
->call('validateSettings')
->assertHasErrors(['options.bogus']);
});

it('does not create any extra records during settings validation', function () {
$user = User::factory()->create();
$file = FileRecord::factory()->for($user)->create(['extension' => 'png']);

Livewire::actingAs($user)
->test(DashboardConverter::class)
->set('currentFileId', $file->id)
->call('selectTargetFormat', 'jpg')
->set('options.quality', 'best')
->call('validateSettings')
->assertHasNoErrors();

expect(FileRecord::query()->where('user_id', $user->id)->count())->toBe(1);
});