File size: 4,096 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

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;
        });
    }
}