118 lines
3.8 KiB
PHP
118 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Movies;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Movies\Models\Movie;
|
|
use App\Modules\Movies\Services\Contracts\MovieProvider;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AdminAcceptMovieTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function signIn(): User
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
return $user;
|
|
}
|
|
|
|
protected function bindStubProvider(array $details): void
|
|
{
|
|
$this->app->bind(MovieProvider::class, function () use ($details) {
|
|
return new class($details) implements MovieProvider {
|
|
public function __construct(private array $d) {}
|
|
public function search(string $query, int $page = 1): array
|
|
{ return ['results' => collect(), 'total' => 0, 'page' => 1, 'has_more' => false]; }
|
|
public function details(string $id): array
|
|
{ return $this->d; }
|
|
};
|
|
});
|
|
}
|
|
|
|
private function sample(string $providerId = 'tt9999999', string $title = 'Movie One'): array
|
|
{
|
|
return [
|
|
'provider' => 'omdb',
|
|
'provider_id' => $providerId,
|
|
'external_ids' => ['imdb' => $providerId, 'omdb' => $providerId],
|
|
'title' => $title,
|
|
'original_title' => $title,
|
|
'description' => 'desc',
|
|
'poster_url' => null,
|
|
'backdrop_url' => null,
|
|
'genres' => ['Action'],
|
|
'rating' => 'PG',
|
|
'release_date' => '2020-01-01',
|
|
'year' => 2020,
|
|
'runtime' => 100,
|
|
'actors' => ['A'],
|
|
'directors' => ['D'],
|
|
'studios' => ['S'],
|
|
'countries' => ['United States'],
|
|
'languages' => ['English'],
|
|
];
|
|
}
|
|
|
|
public function test_requires_authentication(): void
|
|
{
|
|
$response = $this->postJson('/admin/movies/accept', [
|
|
'provider_id' => 'tt9999999',
|
|
'mode' => 'overwrite',
|
|
]);
|
|
// JSON unauthenticated should return 401
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
public function test_overwrite_updates_existing_record_when_duplicate(): void
|
|
{
|
|
$this->signIn();
|
|
$details = $this->sample('tt1111222', 'First Title');
|
|
$this->bindStubProvider($details);
|
|
|
|
// First accept creates
|
|
$this->postJson('/admin/movies/accept', [
|
|
'provider_id' => 'tt1111222',
|
|
'mode' => 'overwrite',
|
|
])->assertOk();
|
|
|
|
$this->assertDatabaseHas('movies', ['provider_id' => 'tt1111222', 'title' => 'First Title']);
|
|
$movie = Movie::query()->where('provider_id', 'tt1111222')->first();
|
|
|
|
// Change provider details title and accept again with overwrite
|
|
$details['title'] = 'Updated Title';
|
|
$this->bindStubProvider($details);
|
|
|
|
$this->postJson('/admin/movies/accept', [
|
|
'provider_id' => 'tt1111222',
|
|
'mode' => 'overwrite',
|
|
])->assertOk();
|
|
|
|
$this->assertDatabaseCount('movies', 1);
|
|
$this->assertDatabaseHas('movies', ['id' => $movie->id, 'title' => 'Updated Title']);
|
|
}
|
|
|
|
public function test_duplicate_creates_second_record_with_same_provider_id(): void
|
|
{
|
|
$this->signIn();
|
|
$details = $this->sample('tt3333444', 'First');
|
|
$this->bindStubProvider($details);
|
|
|
|
$this->postJson('/admin/movies/accept', [
|
|
'provider_id' => 'tt3333444',
|
|
'mode' => 'overwrite',
|
|
])->assertOk();
|
|
|
|
// Accept with duplicate mode should create a new row
|
|
$this->postJson('/admin/movies/accept', [
|
|
'provider_id' => 'tt3333444',
|
|
'mode' => 'duplicate',
|
|
])->assertOk();
|
|
|
|
$this->assertDatabaseCount('movies', 2);
|
|
$this->assertSame(2, Movie::query()->where('provider_id', 'tt3333444')->count());
|
|
}
|
|
}
|