diff --git a/app/Services/Parsers/BelgaHomepageParser.php b/app/Services/Parsers/BelgaHomepageParser.php index f5374c65..cd8e112a 100644 --- a/app/Services/Parsers/BelgaHomepageParser.php +++ b/app/Services/Parsers/BelgaHomepageParser.php @@ -6,6 +6,9 @@ class BelgaHomepageParser { private const ARTICLE_URL_TEMPLATE = 'https://www.belganewsagency.eu/press-releases/%s/'; + /** + * @return array + */ public static function extractArticleUrls(string $json): array { $decoded = json_decode($json, true); @@ -16,8 +19,8 @@ public static function extractArticleUrls(string $json): array return collect($decoded['data']) ->pluck('id') - ->filter(fn ($id) => is_int($id) || (is_string($id) && ctype_digit($id))) - ->map(fn ($id) => sprintf(self::ARTICLE_URL_TEMPLATE, $id)) + ->filter(fn($id) => is_int($id) || (is_string($id) && ctype_digit($id))) + ->map(fn($id) => sprintf(self::ARTICLE_URL_TEMPLATE, $id)) ->unique() ->values() ->toArray(); diff --git a/tests/Unit/Services/ArticleFetcherBelgaTest.php b/tests/Unit/Services/ArticleFetcherBelgaTest.php index 3b319a5b..8442dc05 100644 --- a/tests/Unit/Services/ArticleFetcherBelgaTest.php +++ b/tests/Unit/Services/ArticleFetcherBelgaTest.php @@ -22,7 +22,11 @@ class ArticleFetcherBelgaTest extends TestCase private function apiResponse(): string { - return file_get_contents(__DIR__.'/../../Fixtures/belga-pressreleases.json'); + $contents = file_get_contents(__DIR__.'/../../Fixtures/belga-pressreleases.json'); + + $this->assertNotFalse($contents, 'Belga fixture could not be read.'); + + return $contents; } private function belgaFeed(): Feed diff --git a/tests/Unit/Services/Parsers/BelgaHomepageParserTest.php b/tests/Unit/Services/Parsers/BelgaHomepageParserTest.php index 04be29cf..7fd3b933 100644 --- a/tests/Unit/Services/Parsers/BelgaHomepageParserTest.php +++ b/tests/Unit/Services/Parsers/BelgaHomepageParserTest.php @@ -10,7 +10,19 @@ class BelgaHomepageParserTest extends TestCase { private function fixture(): string { - return file_get_contents(__DIR__.'/../../../Fixtures/belga-pressreleases.json'); + $contents = file_get_contents(__DIR__.'/../../../Fixtures/belga-pressreleases.json'); + + $this->assertNotFalse($contents, 'Belga fixture could not be read.'); + + return $contents; + } + + /** + * @param array> $data + */ + private function jsonWith(array $data): string + { + return (string) json_encode(['data' => $data]); } public function test_extracts_article_urls_from_api_json(): void @@ -24,12 +36,12 @@ public function test_extracts_article_urls_from_api_json(): void public function test_deduplicates_urls(): void { - $json = json_encode(['data' => [ + $json = $this->jsonWith([ ['id' => 100], ['id' => 100], ['id' => '100'], ['id' => 101], - ]]); + ]); $urls = BelgaHomepageParser::extractArticleUrls($json); @@ -55,13 +67,13 @@ public function test_returns_empty_array_when_data_key_missing_or_empty(): void public function test_skips_entries_without_usable_id(): void { - $json = json_encode(['data' => [ + $json = $this->jsonWith([ ['id' => 200], ['id' => null], ['publishDate' => '2026-08-01T06:24:19'], ['id' => 'not-numeric'], ['id' => 201], - ]]); + ]); $urls = BelgaHomepageParser::extractArticleUrls($json);