assertFalse($scraper->fetchById('1234'));
$this->assertFalse($scraper->fetchById('123456789'));
$this->assertFalse($scraper->fetchById('abc1234'));
}
public function test_search_returns_empty_for_empty_query(): void
{
$scraper = new ImdbScraper;
$this->assertSame([], $scraper->search(''));
$this->assertSame([], $scraper->search(' '));
}
public function test_fetch_by_id_uses_dom_fallback_when_jsonld_is_missing(): void
{
$html = <<<'HTML'
Fallback Movie
Fallback plot line.
8.1/10
DirectorFallback Director
EnglishGerman
HTML;
$scraper = $this->makeScraperWithResponses([
new Response(200, ['Content-Type' => 'text/html; charset=UTF-8'], $html),
]);
$data = $scraper->fetchById('7654321');
$this->assertIsArray($data);
$this->assertSame('Fallback Movie', $data['title']);
$this->assertSame('2025', $data['year']);
$this->assertSame('Fallback plot line.', $data['plot']);
$this->assertSame('8.1', $data['rating']);
$this->assertSame(['Drama', 'Thriller'], $data['genre']);
$this->assertSame(['Fallback Director'], $data['director']);
$this->assertSame(['Fallback Actor'], $data['actors']);
$this->assertSame('English, German', $data['language']);
}
public function test_search_falls_back_to_html_results_when_suggestion_json_is_unavailable(): void
{
$html = <<<'HTML'
HTML;
$scraper = $this->makeScraperWithResponses([
new Response(500, ['Content-Type' => 'application/json; charset=UTF-8'], '{}'),
new Response(200, ['Content-Type' => 'text/html; charset=UTF-8'], $html),
]);
$results = $scraper->search('Inception');
$this->assertCount(2, $results);
$this->assertSame('1375666', $results[0]['imdbid']);
$this->assertSame('Inception', $results[0]['title']);
$this->assertSame('2010', $results[0]['year']);
}
/**
* @param array $responses
*/
private function makeScraperWithResponses(array $responses): ImdbScraper
{
$mock = new MockHandler($responses);
$client = new Client([
'handler' => HandlerStack::create($mock),
'http_errors' => false,
]);
return new ImdbScraper($client);
}
}