release/v1.3.5 #122
3 changed files with 27 additions and 8 deletions
|
|
@ -6,6 +6,9 @@ class BelgaHomepageParser
|
||||||
{
|
{
|
||||||
private const ARTICLE_URL_TEMPLATE = 'https://www.belganewsagency.eu/press-releases/%s/';
|
private const ARTICLE_URL_TEMPLATE = 'https://www.belganewsagency.eu/press-releases/%s/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
public static function extractArticleUrls(string $json): array
|
public static function extractArticleUrls(string $json): array
|
||||||
{
|
{
|
||||||
$decoded = json_decode($json, true);
|
$decoded = json_decode($json, true);
|
||||||
|
|
@ -16,8 +19,8 @@ public static function extractArticleUrls(string $json): array
|
||||||
|
|
||||||
return collect($decoded['data'])
|
return collect($decoded['data'])
|
||||||
->pluck('id')
|
->pluck('id')
|
||||||
->filter(fn ($id) => is_int($id) || (is_string($id) && ctype_digit($id)))
|
->filter(fn($id) => is_int($id) || (is_string($id) && ctype_digit($id)))
|
||||||
->map(fn ($id) => sprintf(self::ARTICLE_URL_TEMPLATE, $id))
|
->map(fn($id) => sprintf(self::ARTICLE_URL_TEMPLATE, $id))
|
||||||
->unique()
|
->unique()
|
||||||
->values()
|
->values()
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,11 @@ class ArticleFetcherBelgaTest extends TestCase
|
||||||
|
|
||||||
private function apiResponse(): string
|
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
|
private function belgaFeed(): Feed
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,19 @@ class BelgaHomepageParserTest extends TestCase
|
||||||
{
|
{
|
||||||
private function fixture(): string
|
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<int, array<string, mixed>> $data
|
||||||
|
*/
|
||||||
|
private function jsonWith(array $data): string
|
||||||
|
{
|
||||||
|
return (string) json_encode(['data' => $data]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_extracts_article_urls_from_api_json(): void
|
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
|
public function test_deduplicates_urls(): void
|
||||||
{
|
{
|
||||||
$json = json_encode(['data' => [
|
$json = $this->jsonWith([
|
||||||
['id' => 100],
|
['id' => 100],
|
||||||
['id' => 100],
|
['id' => 100],
|
||||||
['id' => '100'],
|
['id' => '100'],
|
||||||
['id' => 101],
|
['id' => 101],
|
||||||
]]);
|
]);
|
||||||
|
|
||||||
$urls = BelgaHomepageParser::extractArticleUrls($json);
|
$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
|
public function test_skips_entries_without_usable_id(): void
|
||||||
{
|
{
|
||||||
$json = json_encode(['data' => [
|
$json = $this->jsonWith([
|
||||||
['id' => 200],
|
['id' => 200],
|
||||||
['id' => null],
|
['id' => null],
|
||||||
['publishDate' => '2026-08-01T06:24:19'],
|
['publishDate' => '2026-08-01T06:24:19'],
|
||||||
['id' => 'not-numeric'],
|
['id' => 'not-numeric'],
|
||||||
['id' => 201],
|
['id' => 201],
|
||||||
]]);
|
]);
|
||||||
|
|
||||||
$urls = BelgaHomepageParser::extractArticleUrls($json);
|
$urls = BelgaHomepageParser::extractArticleUrls($json);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue