From 4e80fd54b7bacbf76af8b47683e746899e5e97d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20S=C3=A9rgio=20Dantas?= Date: Wed, 1 Jul 2026 12:16:15 -0300 Subject: [PATCH] fix(media): don't 500 when the Unsplash/Giphy key is set to null MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `config('services.unsplash.access_key', '')` only falls back to '' when the key is *absent* — if the env var is present but empty, config returns null, and assigning null to the `string` property throws a TypeError, taking the whole request down with a 500 during media search. Cast to `(string)` so a null/empty key degrades gracefully to '' and the service returns an empty result set (as it already does for a missing key) instead of throwing. Covered by a regression test for both services. --- app/Services/GiphyService.php | 2 +- app/Services/UnsplashService.php | 2 +- .../Services/MediaSearchKeyFallbackTest.php | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Services/MediaSearchKeyFallbackTest.php diff --git a/app/Services/GiphyService.php b/app/Services/GiphyService.php index e60ff667..f5e674d4 100644 --- a/app/Services/GiphyService.php +++ b/app/Services/GiphyService.php @@ -15,7 +15,7 @@ class GiphyService public function __construct() { - $this->apiKey = config('services.giphy.api_key', ''); + $this->apiKey = (string) config('services.giphy.api_key', ''); } /** diff --git a/app/Services/UnsplashService.php b/app/Services/UnsplashService.php index 5ac1ad9a..db16237e 100644 --- a/app/Services/UnsplashService.php +++ b/app/Services/UnsplashService.php @@ -15,7 +15,7 @@ class UnsplashService public function __construct() { - $this->accessKey = config('services.unsplash.access_key', ''); + $this->accessKey = (string) config('services.unsplash.access_key', ''); } /** diff --git a/tests/Unit/Services/MediaSearchKeyFallbackTest.php b/tests/Unit/Services/MediaSearchKeyFallbackTest.php new file mode 100644 index 00000000..6fa85c47 --- /dev/null +++ b/tests/Unit/Services/MediaSearchKeyFallbackTest.php @@ -0,0 +1,22 @@ + null]); + + $result = (new UnsplashService)->search('cats'); + + expect($result)->toBe(['results' => [], 'total' => 0, 'total_pages' => 0]); +}); + +test('giphy service tolerates a null api key without throwing', function () { + config(['services.giphy.api_key' => null]); + + $result = (new GiphyService)->search('cats'); + + expect($result)->toBe(['results' => [], 'total' => 0, 'total_pages' => 0]); +});