| namespace Tests\Feature; | |
| use App\Services\FreepikStockContentService; | |
| use Illuminate\Http\Client\Request; | |
| use Illuminate\Support\Facades\Http; | |
| use Illuminate\Support\Str; | |
| use Tests\TestCase; | |
| class FreepikStockContentServiceTest extends TestCase | |
| { | |
| public function test_stock_search_maps_freepik_results_across_supported_types(): void | |
| { | |
| config([ | |
| 'services.freepik.api_key' => 'test-freepik-key', | |
| 'services.freepik.base_url' => 'https://api.freepik.com/v1/ai/text-to-image', | |
| ]); | |
| Http::fake([ | |
| 'https://api.freepik.com/v1/resources*' => Http::response([ | |
| 'data' => [[ | |
| 'id' => 10, | |
| 'title' => 'Waterfall hero', | |
| 'url' => 'https://www.freepik.com/asset/10', | |
| 'image' => [ | |
| 'type' => 'photo', | |
| 'orientation' => 'vertical', | |
| 'source' => ['url' => 'https://img.example.com/resource.jpg'], | |
| ], | |
| 'author' => ['name' => 'Freepik Author'], | |
| 'stats' => ['likes' => 9], | |
| ]], | |
| ], 200), | |
| 'https://api.freepik.com/v1/videos*' => Http::response([ | |
| 'data' => [[ | |
| 'id' => 20, | |
| 'name' => 'Forest Walk', | |
| 'url' => 'https://www.freepik.com/video/20', | |
| 'quality' => '4k', | |
| 'duration' => '00:00:10', | |
| 'aspect_ratio' => '16:9', | |
| 'item_subtype' => 'footage', | |
| 'author' => ['name' => 'Video Author'], | |
| 'thumbnails' => [['url' => 'https://img.example.com/video-thumb.jpg']], | |
| 'previews' => [['url' => 'https://img.example.com/video-preview.mp4']], | |
| ]], | |
| ], 200), | |
| 'https://api.freepik.com/v1/icons*' => Http::response([ | |
| 'data' => [[ | |
| 'id' => 30, | |
| 'name' => 'Nature icon', | |
| 'slug' => 'nature-icon', | |
| 'style' => ['name' => 'Outline'], | |
| 'author' => ['name' => 'Icon Author'], | |
| 'thumbnails' => [['url' => 'https://img.example.com/icon.png']], | |
| 'tags' => [['name' => 'leaf'], ['name' => 'tree']], | |
| ]], | |
| ], 200), | |
| 'https://api.freepik.com/v1/music*' => Http::response([ | |
| 'results' => [[ | |
| 'id' => 40, | |
| 'title' => 'Soft Seasons', | |
| 'time' => '3:05', | |
| 'artist' => ['name' => 'MudiG'], | |
| 'cover_url' => 'https://img.example.com/music.jpg', | |
| 'preview_url' => 'https://img.example.com/music.mp3', | |
| 'genres' => [['name' => 'Pop'], ['name' => 'Acoustic']], | |
| 'is_premium' => false, | |
| ]], | |
| ], 200), | |
| ]); | |
| $service = app(FreepikStockContentService::class); | |
| $result = $service->search('nature', 'all', 'relevance', 1); | |
| $this->assertCount(1, $result['results']['images']); | |
| $this->assertCount(1, $result['results']['videos']); | |
| $this->assertCount(1, $result['results']['icons']); | |
| $this->assertCount(1, $result['results']['audio']); | |
| $this->assertSame('Waterfall hero', $result['results']['images'][0]['title']); | |
| $this->assertSame('Forest Walk', $result['results']['videos'][0]['title']); | |
| $this->assertSame('Nature icon', $result['results']['icons'][0]['title']); | |
| $this->assertSame('Soft Seasons', $result['results']['audio'][0]['title']); | |
| $this->assertSame(1, $result['summary']['images']); | |
| $this->assertSame([], $result['errors']); | |
| Http::assertSent(function (Request $request) { | |
| return Str::startsWith($request->url(), 'https://api.freepik.com/v1/resources') | |
| && $request['term'] === 'nature' | |
| && $request['limit'] === 8; | |
| }); | |
| } | |
| } | |