Download tests/Feature/FreepikMediaServiceTest.php from medilis/teknolis: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/spaces/medilis/teknolis/resolve/main/tests/Feature/FreepikMediaServiceTest.php
- Command line
-
hf download hf://spaces/medilis/teknolis/tests/Feature/FreepikMediaServiceTest.php
-
curl -L -o FreepikMediaServiceTest.php https://huggingface.co/spaces/medilis/teknolis/resolve/main/tests/Feature/FreepikMediaServiceTest.php
5.2 kB
| namespace Tests\Feature; | |
| use App\Services\FreepikImageService; | |
| use App\Services\FreepikVideoService; | |
| use Illuminate\Http\Client\Request; | |
| use Illuminate\Support\Facades\Http; | |
| use Illuminate\Support\Facades\Storage; | |
| use Tests\TestCase; | |
| class FreepikMediaServiceTest extends TestCase | |
| { | |
| public function test_freepik_image_service_uses_configured_endpoint_and_returns_multiple_variants(): 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', | |
| 'services.freepik.timeout' => 30, | |
| 'filesystems.disks.public.root' => $diskRoot, | |
| 'filesystems.disks.public.url' => '/storage', | |
| ]); | |
| $createCount = 0; | |
| Http::fake(function (Request $request) use (&$createCount) { | |
| if ($request->method() === 'POST' && $request->url() === 'https://api.freepik.com/v1/ai/text-to-image') { | |
| $createCount++; | |
| return Http::response([ | |
| 'data' => [ | |
| [ | |
| 'base64' => base64_encode('fake-image-'.$createCount.'-a'), | |
| ], | |
| [ | |
| 'base64' => base64_encode('fake-image-'.$createCount.'-b'), | |
| ], | |
| ], | |
| ], 200); | |
| } | |
| return Http::response(['message' => 'Unexpected request'], 500); | |
| }); | |
| $service = app(FreepikImageService::class); | |
| $urls = $service->generate('Premium social story visual', 'portrait', 2); | |
| $this->assertCount(2, $urls); | |
| foreach ($urls as $url) { | |
| $relativePath = ltrim((string) str_replace('/storage/', '', parse_url($url, PHP_URL_PATH) ?: ''), '/'); | |
| $this->assertTrue(Storage::disk('public')->exists($relativePath)); | |
| } | |
| Http::assertSentCount(1); | |
| Http::assertSent(function (Request $request) { | |
| return $request->method() === 'POST' | |
| && $request->url() === 'https://api.freepik.com/v1/ai/text-to-image' | |
| && $request['width'] === 768 | |
| && $request['height'] === 1344 | |
| && $request['num_images'] === 2; | |
| }); | |
| } | |
| public function test_freepik_video_service_downloads_remote_video_to_public_storage(): 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', | |
| 'services.freepik.timeout' => 30, | |
| 'filesystems.disks.public.root' => $diskRoot, | |
| 'filesystems.disks.public.url' => '/storage', | |
| ]); | |
| Http::fake(function (Request $request) { | |
| if ($request->method() === 'POST' && $request->url() === 'https://api.freepik.com/v1/ai/text-to-video/runway-4-5') { | |
| return Http::response([ | |
| 'data' => [ | |
| 'task_id' => 'video-task-1', | |
| 'status' => 'CREATED', | |
| 'generated' => [], | |
| ], | |
| ], 200); | |
| } | |
| if ($request->method() === 'GET' && $request->url() === 'https://api.freepik.com/v1/ai/text-to-video/runway-4-5/video-task-1') { | |
| return Http::response([ | |
| 'data' => [ | |
| 'task_id' => 'video-task-1', | |
| 'status' => 'COMPLETED', | |
| 'generated' => [ | |
| 'https://cdn.example.com/story.mp4', | |
| ], | |
| ], | |
| ], 200); | |
| } | |
| if ($request->method() === 'GET' && $request->url() === 'https://cdn.example.com/story.mp4') { | |
| return Http::response('fake-video-binary', 200, [ | |
| 'Content-Type' => 'video/mp4', | |
| ]); | |
| } | |
| return Http::response(['message' => 'Unexpected request'], 500); | |
| }); | |
| $service = app(FreepikVideoService::class); | |
| $video = $service->generate('Vertical software launch ad'); | |
| $this->assertSame('freepik-runway', $video['provider']); | |
| $this->assertSame('9:16', $video['aspect']); | |
| $this->assertSame(5, $video['duration_seconds']); | |
| $relativePath = ltrim((string) str_replace('/storage/', '', parse_url((string) $video['url'], PHP_URL_PATH) ?: ''), '/'); | |
| $this->assertTrue(Storage::disk('public')->exists($relativePath)); | |
| Http::assertSent(function (Request $request) { | |
| return $request->method() === 'POST' | |
| && $request->url() === 'https://api.freepik.com/v1/ai/text-to-video/runway-4-5' | |
| && $request['ratio'] === '720:1280' | |
| && $request['duration'] === 5; | |
| }); | |
| } | |
| } | |