PIMS/tests/Unit/Movies/OmdbMovieProviderTest.php

101 lines
3.7 KiB
PHP
Raw Normal View History

2025-12-07 03:49:26 +00:00
<?php
namespace Tests\Unit\Movies;
use App\Modules\Movies\Services\Omdb\OmdbMovieProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class OmdbMovieProviderTest extends TestCase
{
use RefreshDatabase; // not strictly needed, but keeps env consistent
public function test_search_normalizes_results_and_uses_cache(): void
{
Http::fake([
'*' => Http::response([
'Response' => 'True',
'totalResults' => '12',
'Search' => [
[
'Title' => 'Demo Film',
'Year' => '2020',
'imdbID' => 'tt1111111',
'Type' => 'movie',
'Poster' => 'https://img/poster1.jpg',
],
[
'Title' => 'Another Demo',
'Year' => '2019',
'imdbID' => 'tt2222222',
'Type' => 'movie',
'Poster' => 'N/A',
],
],
], 200),
]);
$provider = new OmdbMovieProvider();
// First call should hit HTTP
$res1 = $provider->search('demo', 1);
$this->assertSame(12, $res1['total']);
$this->assertTrue($res1['has_more']);
$this->assertCount(2, $res1['results']);
$first = $res1['results'][0];
$this->assertSame('omdb', $first['provider']);
$this->assertSame('tt1111111', $first['provider_id']);
$this->assertSame('Demo Film', $first['title']);
$this->assertSame('2020', $first['year']);
$this->assertNotNull($first['poster_url']);
// Second identical call should be served from cache, not hitting HTTP again
Http::assertSentCount(1);
$res2 = $provider->search('demo', 1);
Http::assertSentCount(1); // still 1
$this->assertSame($res1['total'], $res2['total']);
}
public function test_details_normalizes_payload_and_uses_cache(): void
{
Http::fake([
'*' => Http::response([
'Response' => 'True',
'Title' => 'Demo Film',
'Year' => '2020',
'Released' => '01 Jan 2020',
'Runtime' => '123 min',
'Genre' => 'Action, Comedy',
'Director' => 'John Smith',
'Actors' => 'Alice, Bob',
'Language' => 'English',
'Country' => 'United States',
'Poster' => 'https://img/poster1.jpg',
'Plot' => 'Something happens.',
'Rated' => 'PG-13',
'imdbID' => 'tt1111111',
], 200),
]);
$provider = new OmdbMovieProvider();
$d1 = $provider->details('tt1111111');
$this->assertSame('omdb', $d1['provider']);
$this->assertSame('tt1111111', $d1['provider_id']);
$this->assertSame(['imdb' => 'tt1111111', 'omdb' => 'tt1111111'], $d1['external_ids']);
$this->assertSame(['Action', 'Comedy'], $d1['genres']);
$this->assertSame(['Alice', 'Bob'], $d1['actors']);
$this->assertSame(['John Smith'], $d1['directors']);
$this->assertSame(['United States'], $d1['countries']);
$this->assertSame(['English'], $d1['languages']);
$this->assertSame(123, $d1['runtime']);
$this->assertSame('2020-01-01', $d1['release_date']);
Http::assertSentCount(1);
$d2 = $provider->details('tt1111111');
Http::assertSentCount(1); // cached
$this->assertSame($d1['title'], $d2['title']);
}
}