fix(media): don't 500 when the Unsplash/Giphy key is set to null
`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.
This commit is contained in:
parent
bfa018a4cd
commit
4e80fd54b7
3 changed files with 24 additions and 2 deletions
|
|
@ -15,7 +15,7 @@ class GiphyService
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->apiKey = config('services.giphy.api_key', '');
|
||||
$this->apiKey = (string) config('services.giphy.api_key', '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class UnsplashService
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->accessKey = config('services.unsplash.access_key', '');
|
||||
$this->accessKey = (string) config('services.unsplash.access_key', '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
22
tests/Unit/Services/MediaSearchKeyFallbackTest.php
Normal file
22
tests/Unit/Services/MediaSearchKeyFallbackTest.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Services\GiphyService;
|
||||
use App\Services\UnsplashService;
|
||||
|
||||
test('unsplash service tolerates a null access key without throwing', function () {
|
||||
config(['services.unsplash.access_key' => 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]);
|
||||
});
|
||||
Loading…
Reference in a new issue