|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test script for all API methods in the PHP SDK |
| 4 | + * |
| 5 | + * This script tests all available methods in the PHP SDK with real API calls |
| 6 | + * to ensure they work correctly with our fixed implementation. |
| 7 | + * |
| 8 | + * Usage: php test-all-methods.php <api_key> |
| 9 | + */ |
| 10 | + |
| 11 | +require "vendor/autoload.php"; |
| 12 | + |
| 13 | +use LingoDotDev\Sdk\LingoDotDevEngine; |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | + |
| 16 | +$apiKey = $argv[1] ?? null; |
| 17 | + |
| 18 | +if (!$apiKey) { |
| 19 | + echo "Error: API key is required. Pass it as a command-line argument.\n"; |
| 20 | + exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +$engine = new LingoDotDevEngine([ |
| 24 | + "apiKey" => $apiKey, |
| 25 | +]); |
| 26 | + |
| 27 | +function runTest($name, $callback) { |
| 28 | + echo "\n=== Testing $name ===\n"; |
| 29 | + try { |
| 30 | + $result = $callback(); |
| 31 | + echo "✅ Test passed!\n"; |
| 32 | + echo "Result: " . json_encode($result, JSON_PRETTY_PRINT) . "\n"; |
| 33 | + return true; |
| 34 | + } catch (\Exception $e) { |
| 35 | + echo "❌ Test failed!\n"; |
| 36 | + echo "Error: " . $e->getMessage() . "\n"; |
| 37 | + |
| 38 | + if ($e instanceof RequestException && $e->hasResponse()) { |
| 39 | + $response = $e->getResponse(); |
| 40 | + echo "Status Code: " . $response->getStatusCode() . "\n"; |
| 41 | + echo "Response Body: " . $response->getBody() . "\n"; |
| 42 | + } |
| 43 | + return false; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +runTest("localizeText", function() use ($engine) { |
| 48 | + return $engine->localizeText("Hello, this is my first localization with Lingo.dev!", [ |
| 49 | + "sourceLocale" => "en", |
| 50 | + "targetLocale" => "es", |
| 51 | + ]); |
| 52 | +}); |
| 53 | + |
| 54 | +runTest("localizeObject", function() use ($engine) { |
| 55 | + return $engine->localizeObject([ |
| 56 | + "greeting" => "Hello", |
| 57 | + "farewell" => "Goodbye", |
| 58 | + "messages" => [ |
| 59 | + "welcome" => "Welcome to our service", |
| 60 | + "thanks" => "Thank you for your business" |
| 61 | + ] |
| 62 | + ], [ |
| 63 | + "sourceLocale" => "en", |
| 64 | + "targetLocale" => "fr", |
| 65 | + ]); |
| 66 | +}); |
| 67 | + |
| 68 | +runTest("localizeChat", function() use ($engine) { |
| 69 | + return $engine->localizeChat([ |
| 70 | + ["name" => "Alice", "text" => "Hello, how are you?"], |
| 71 | + ["name" => "Bob", "text" => "I am fine, thank you!"], |
| 72 | + ["name" => "Alice", "text" => "What are you doing today?"] |
| 73 | + ], [ |
| 74 | + "sourceLocale" => "en", |
| 75 | + "targetLocale" => "de", |
| 76 | + ]); |
| 77 | +}); |
| 78 | + |
| 79 | +runTest("batchLocalizeText", function() use ($engine) { |
| 80 | + return $engine->batchLocalizeText("Hello, world!", [ |
| 81 | + "sourceLocale" => "en", |
| 82 | + "targetLocales" => ["es", "fr", "de"], |
| 83 | + ]); |
| 84 | +}); |
| 85 | + |
| 86 | +runTest("recognizeLocale", function() use ($engine) { |
| 87 | + return $engine->recognizeLocale("Bonjour le monde"); |
| 88 | +}); |
| 89 | + |
| 90 | +runTest("Progress Callback", function() use ($engine) { |
| 91 | + $progressCalled = false; |
| 92 | + $progressValue = 0; |
| 93 | + |
| 94 | + $result = $engine->localizeText("Hello, this is a test with progress callback!", [ |
| 95 | + "sourceLocale" => "en", |
| 96 | + "targetLocale" => "es", |
| 97 | + ], function ($progress) use (&$progressCalled, &$progressValue) { |
| 98 | + $progressCalled = true; |
| 99 | + $progressValue = $progress; |
| 100 | + echo "Progress: $progress%\n"; |
| 101 | + }); |
| 102 | + |
| 103 | + if (!$progressCalled) { |
| 104 | + throw new \Exception("Progress callback was not called"); |
| 105 | + } |
| 106 | + |
| 107 | + return [ |
| 108 | + "result" => $result, |
| 109 | + "progressCalled" => $progressCalled, |
| 110 | + "progressValue" => $progressValue |
| 111 | + ]; |
| 112 | +}); |
| 113 | + |
| 114 | +echo "\n=== All tests completed ===\n"; |
0 commit comments