Skip to content

Commit

Permalink
Merge pull request #12940 from recca0120/fix/test-fake-uploaded-file
Browse files Browse the repository at this point in the history
UploadedFile should be converted to TemporaryUploadedFile
  • Loading branch information
danharrin authored May 27, 2024
2 parents 3e132fd + 7801b0b commit 8887010
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/forms/src/Testing/TestsForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Testing\Assert;
use Livewire\Features\SupportTesting\Testable;
Expand Down Expand Up @@ -42,6 +43,15 @@ public function fillForm(): Closure
$state = Arr::undot([$formStatePath => $state]);
}

foreach (Arr::dot($state) as $key => $value) {
if ($value instanceof UploadedFile ||
(is_array($value) && isset($value[0]) && $value[0] instanceof UploadedFile)
) {
$this->set($key, $value);
Arr::set($state, $key, $this->get($key));
}
}

$this->call('fillFormDataForTesting', $state);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/src/Forms/Components/FileUploadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Filament\Forms\Components\FileUpload;
use Filament\Forms\Form;
use Filament\Tests\Forms\Fixtures\Livewire;
use Filament\Tests\TestCase;
use Illuminate\Contracts\View\View;
use Illuminate\Http\UploadedFile;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

use function Filament\Tests\livewire;

uses(TestCase::class);

it('UploadedFile should be converted to TemporaryUploadedFile', function () {
livewire(TestComponentWithFileUpload::class)
->fillForm([
'single-file' => UploadedFile::fake()->image('single-file.jpg'),
'multiple-files' => [
UploadedFile::fake()->image('multiple-file1.jpg'),
UploadedFile::fake()->image('multiple-file2.jpg'),
],
])
->assertFormSet(function (array $data) {
expect($data['single-file'][0])->toBeInstanceOf(TemporaryUploadedFile::class)
->and($data['multiple-files'][0])->toBeInstanceOf(TemporaryUploadedFile::class)
->and($data['multiple-files'][1])->toBeInstanceOf(TemporaryUploadedFile::class);
});
});

class TestComponentWithFileUpload extends Livewire
{
public function form(Form $form): Form
{
return $form
->schema([
FileUpload::make('single-file'),
FileUpload::make('multiple-files')->multiple(),
])
->statePath('data');
}

public function render(): View
{
return view('forms.fixtures.form');
}
}

0 comments on commit 8887010

Please sign in to comment.