PIMS/tests/Unit/Movies/UpsertMovieFromProviderTest.php

91 lines
3.1 KiB
PHP

<?php
namespace Tests\Unit\Movies;
use App\Modules\Movies\Models\{Movie, Genre, Actor, Director, Studio, Country, Language};
use App\Modules\Movies\Services\UpsertMovieFromProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UpsertMovieFromProviderTest extends TestCase
{
use RefreshDatabase;
private function sampleDetails(): array
{
return [
'provider' => 'omdb',
'provider_id' => 'tt1234567',
'external_ids' => [
'imdb' => 'tt1234567',
'omdb' => 'tt1234567',
],
'title' => 'Sample Movie',
'original_title' => 'Sample Movie Original',
'description' => 'An example movie.',
'poster_url' => 'https://example.com/poster.jpg',
'backdrop_url' => null,
'genres' => ['Action', 'Comedy'],
'rating' => 'PG-13',
'release_date' => '2020-01-01',
'year' => 2020,
'runtime' => 120,
'actors' => ['John Doe', 'Jane Doe'],
'directors' => ['Some Director'],
'studios' => ['Studio One', 'Studio Two'],
'countries' => ['United States'],
'languages' => ['English'],
];
}
public function test_create_duplicate_mode_creates_new_record_with_same_provider_id(): void
{
$service = new UpsertMovieFromProvider();
$details = $this->sampleDetails();
$first = $service->handle($details, 'duplicate');
$second = $service->handle($details, 'duplicate');
$this->assertNotSame($first->id, $second->id);
$this->assertSame($first->provider_id, $second->provider_id);
$this->assertDatabaseCount('movies', 2);
$this->assertDatabaseHas('movies', ['id' => $first->id, 'provider_id' => 'tt1234567']);
$this->assertDatabaseHas('movies', ['id' => $second->id, 'provider_id' => 'tt1234567']);
}
public function test_overwrite_mode_updates_existing_record(): void
{
$service = new UpsertMovieFromProvider();
$details = $this->sampleDetails();
$movie = $service->handle($details, 'overwrite');
// Change some fields and call again (overwrite)
$details['title'] = 'Updated Title';
$details['genres'] = ['Drama'];
$updated = $service->handle($details, 'overwrite');
$this->assertSame($movie->id, $updated->id);
$this->assertSame('Updated Title', $updated->title);
// Relations synced to just Drama
$this->assertCount(1, $updated->genres);
$this->assertSame('Drama', $updated->genres->first()->name);
}
public function test_relations_are_synced_by_names(): void
{
$service = new UpsertMovieFromProvider();
$details = $this->sampleDetails();
$movie = $service->handle($details, 'overwrite');
$this->assertCount(2, $movie->genres);
$this->assertCount(2, $movie->actors);
$this->assertCount(1, $movie->directors);
$this->assertCount(2, $movie->studios);
$this->assertCount(1, $movie->countries);
$this->assertCount(1, $movie->languages);
}
}