Files
newznab-tmux/tests/Unit/Services/MovieServiceTest.php
T
2026-04-09 17:16:17 +02:00

173 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\Services\MovieService;
use App\Services\TraktService;
use App\Services\TvProcessing\Providers\TraktProvider;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\Test;
use Tests\Unit\ImdbScraperTestCase;
class MovieServiceTest extends ImdbScraperTestCase
{
protected function setUp(): void
{
parent::setUp();
Schema::dropIfExists('movieinfo');
Schema::create('movieinfo', function (Blueprint $table): void {
$table->id();
$table->string('imdbid')->unique();
$table->unsignedInteger('tmdbid')->default(0);
$table->unsignedInteger('traktid')->default(0);
$table->string('title')->default('');
$table->string('tagline')->default('');
$table->string('rating', 4)->default('');
$table->string('rtrating', 10)->default('');
$table->string('plot')->default('');
$table->string('year', 4)->default('');
$table->string('genre', 64)->default('');
$table->string('type', 32)->default('');
$table->string('director', 64)->default('');
$table->text('actors')->default('');
$table->string('language', 64)->default('');
$table->boolean('cover')->default(false);
$table->boolean('backdrop')->default(false);
$table->string('trailer')->default('');
$table->timestamps();
});
}
#[Test]
public function it_accepts_numeric_trakt_years_when_matching_movie_metadata(): void
{
Cache::flush();
$service = $this->makeMovieServiceForTraktResponse([
'title' => 'Example Movie',
'year' => 2024,
'ids' => ['trakt' => 12345],
'overview' => 'Test overview',
'tagline' => 'Test tagline',
'genres' => ['Drama'],
'rating' => 7.5,
'votes' => 10,
'language' => 'en',
'runtime' => 100,
'trailer' => '',
]);
$this->setMovieServiceProperty($service, 'currentTitle', 'Example Movie');
$this->setMovieServiceProperty($service, 'currentYear', '2024');
$movie = $service->fetchTraktTVProperties('8169446');
$this->assertIsArray($movie);
$this->assertSame('Example Movie', $movie['title']);
$this->assertSame(2024, $movie['year']);
}
#[Test]
public function it_still_rejects_mismatched_numeric_trakt_years(): void
{
Cache::flush();
$service = $this->makeMovieServiceForTraktResponse([
'title' => 'Example Movie',
'year' => 2024,
'ids' => ['trakt' => 12345],
]);
$this->setMovieServiceProperty($service, 'currentTitle', 'Example Movie');
$this->setMovieServiceProperty($service, 'currentYear', '2023');
$this->assertFalse($service->fetchTraktTVProperties('8169447'));
}
#[Test]
public function it_finds_movie_info_for_imdb_ids_with_meaningful_leading_zeroes(): void
{
Cache::flush();
$service = new MovieService;
$service->echooutput = false;
$service->update([
'imdbid' => '0137523',
'title' => 'Example Movie',
'year' => '2024',
]);
$movie = $service->getMovieInfo('0137523');
$this->assertNotNull($movie);
$this->assertSame('0137523', $movie->imdbid);
$this->assertSame('Example Movie', $movie->title);
}
#[Test]
public function it_returns_existing_trailer_for_imdb_ids_with_meaningful_leading_zeroes(): void
{
Cache::flush();
$service = new MovieService;
$service->echooutput = false;
$service->update([
'imdbid' => '0137523',
'title' => 'Example Movie',
'year' => '2024',
'trailer' => 'https://example.test/embed/trailer',
]);
$this->assertSame('https://example.test/embed/trailer', $service->getTrailer('0137523'));
}
/**
* @param array<string, mixed> $response
*/
private function makeMovieServiceForTraktResponse(array $response): MovieService
{
$client = new class($response) extends TraktService
{
/**
* @param array<string, mixed> $response
*/
public function __construct(private array $response)
{
parent::__construct('test_trakt_key');
}
/**
* @return array<string, mixed>|null
*/
public function getMovieSummary(string $movie, string $extended = 'min'): ?array
{
return $this->response;
}
};
$provider = new TraktProvider;
$provider->client = $client;
$service = new MovieService;
$service->traktTv = $provider;
$service->echooutput = false;
$this->setMovieServiceProperty($service, 'traktcheck', 'test-key');
return $service;
}
private function setMovieServiceProperty(MovieService $service, string $property, mixed $value): void
{
$reflectionProperty = new \ReflectionProperty($service, $property);
$reflectionProperty->setValue($service, $value);
}
}