94 lines
3.4 KiB
PHP
94 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\PhotoCategory;
|
|
use App\Models\Photo;
|
|
use App\Models\Property;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
|
|
class PhotoImportTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private string $sourceDirectory;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Storage::fake('public');
|
|
$this->sourceDirectory = storage_path('framework/testing/photo-import');
|
|
File::ensureDirectoryExists($this->sourceDirectory.'/2023/11');
|
|
foreach (['2023/11/01_wohn_web.jpg', '2023/11/20_garten_web.jpg'] as $file) {
|
|
$image = UploadedFile::fake()->image(basename($file), 1200, 800);
|
|
File::copy($image->getPathname(), $this->sourceDirectory.'/'.$file);
|
|
}
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
File::deleteDirectory($this->sourceDirectory);
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_manifest_import_uses_categories_and_descriptions_and_keeps_the_sources(): void
|
|
{
|
|
$property = Property::factory()->create();
|
|
$manifest = $this->manifest([
|
|
'2023/11/01_wohn_web.jpg;apartment;Wohn- und Essbereich',
|
|
'2023/11/20_garten_web.jpg;outdoor;Sitzplatz im Garten',
|
|
'2023/11/fehlt.jpg;area;Gibt es nicht',
|
|
]);
|
|
|
|
$this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])
|
|
->expectsOutputToContain('Fehlt: 2023/11/fehlt.jpg')
|
|
->expectsOutputToContain('2 Bilder importiert.')
|
|
->assertSuccessful();
|
|
|
|
$photos = Photo::orderBy('sort_order')->get();
|
|
$this->assertSame(['Wohn- und Essbereich', 'Sitzplatz im Garten'], $photos->pluck('alt_text')->all());
|
|
$this->assertSame([PhotoCategory::Apartment, PhotoCategory::Outdoor], $photos->pluck('category')->all());
|
|
$this->assertTrue($photos->every(fn (Photo $photo): bool => $photo->property_id === $property->id));
|
|
$this->assertFileExists($this->sourceDirectory.'/2023/11/01_wohn_web.jpg');
|
|
}
|
|
|
|
public function test_running_the_import_again_skips_photos_already_imported(): void
|
|
{
|
|
Property::factory()->create();
|
|
$manifest = $this->manifest(['2023/11/01_wohn_web.jpg;apartment;Wohn- und Essbereich']);
|
|
|
|
$this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])->assertSuccessful();
|
|
$this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])
|
|
->expectsOutputToContain('bereits vorhanden: 2023/11/01_wohn_web.jpg')
|
|
->assertSuccessful();
|
|
|
|
$this->assertSame(1, Photo::count());
|
|
}
|
|
|
|
public function test_invalid_manifest_line_aborts_before_importing(): void
|
|
{
|
|
$manifest = $this->manifest(['2023/11/01_wohn_web.jpg;keller;Unbekannte Kategorie']);
|
|
|
|
$this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])->assertFailed();
|
|
|
|
$this->assertSame(0, Photo::count());
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $lines
|
|
*/
|
|
private function manifest(array $lines): string
|
|
{
|
|
$path = $this->sourceDirectory.'/photos.csv';
|
|
File::put($path, implode("\n", ['file;category;alt_text', ...$lines])."\n");
|
|
|
|
return $path;
|
|
}
|
|
}
|