| <?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']); |
| } |
| } |
|
|