File size: 2,075 Bytes
1501522 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php
namespace Tests\Feature;
use App\Services\FreepikStockAssetService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FreepikStockAssetServiceTest extends TestCase
{
public function test_download_to_project_stores_freepik_resource_inside_project(): void
{
$diskRoot = sys_get_temp_dir().'/dev-local-public-'.uniqid();
if (! is_dir($diskRoot)) {
mkdir($diskRoot, 0777, true);
}
config([
'services.freepik.api_key' => 'test-freepik-key',
'services.freepik.base_url' => 'https://api.freepik.com/v1/ai/text-to-image',
'filesystems.disks.public.root' => $diskRoot,
'filesystems.disks.public.url' => '/storage',
]);
Http::fake([
'https://api.freepik.com/v1/resources/10' => Http::response([
'data' => [
'id' => 10,
'title' => 'Waterfall hero',
'image' => [
'source' => ['url' => 'https://img.example.com/resource.jpg'],
],
],
], 200),
'https://api.freepik.com/v1/resources/10/download*' => Http::response([
'data' => [
'filename' => 'waterfall-hero.jpg',
'signed_url' => 'https://downloads.example.com/waterfall-hero.jpg',
],
], 200),
'https://downloads.example.com/waterfall-hero.jpg' => Http::response('fake-image-binary', 200, [
'Content-Type' => 'image/jpeg',
]),
]);
$service = app(FreepikStockAssetService::class);
$download = $service->downloadToProject('images', 10);
$relativePath = ltrim(str_replace('/storage/', '', $download['public_url']), '/');
$this->assertTrue(Storage::disk('public')->exists($relativePath));
$this->assertSame('waterfall-hero.jpg', $download['filename']);
$this->assertSame('images', $download['kind']);
}
}
|