-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-all-methods.php
More file actions
153 lines (134 loc) · 4.07 KB
/
test-all-methods.php
File metadata and controls
153 lines (134 loc) · 4.07 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Test script for all API methods in the PHP SDK
*
* This script tests all available methods in the PHP SDK with real API calls
* to ensure they work correctly.
*
* Usage: php test-all-methods.php <api_key> [engine_id] [api_url]
*/
require "vendor/autoload.php";
use LingoDotDev\Sdk\LingoDotDevEngine;
use GuzzleHttp\Exception\RequestException;
$apiKey = $argv[1] ?? null;
$engineId = $argv[2] ?? null;
$apiUrl = $argv[3] ?? null;
if (!$apiKey) {
echo "Usage: php test-all-methods.php <api_key> [engine_id] [api_url]\n";
exit(1);
}
$config = [
"apiKey" => $apiKey,
];
if ($engineId) {
$config["engineId"] = $engineId;
}
if ($apiUrl) {
$config["apiUrl"] = $apiUrl;
}
$engine = new LingoDotDevEngine($config);
$passed = 0;
$failed = 0;
function runTest($name, $callback) {
global $passed, $failed;
echo "\n=== Testing $name ===\n";
try {
$result = $callback();
echo "✅ Test passed!\n";
echo "Result: " . json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
$passed++;
return true;
} catch (\Exception $e) {
echo "❌ Test failed!\n";
echo "Error: " . $e->getMessage() . "\n";
if ($e instanceof RequestException && $e->hasResponse()) {
$response = $e->getResponse();
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Response Body: " . $response->getBody() . "\n";
}
$failed++;
return false;
}
}
// 1. localizeText
runTest("localizeText", function() use ($engine) {
return $engine->localizeText("Hello, this is my first localization with Lingo.dev!", [
"sourceLocale" => "en",
"targetLocale" => "es",
]);
});
// 2. localizeObject
runTest("localizeObject", function() use ($engine) {
return $engine->localizeObject([
"greeting" => "Hello",
"farewell" => "Goodbye",
"messages" => [
"welcome" => "Welcome to our service",
"thanks" => "Thank you for your business"
]
], [
"sourceLocale" => "en",
"targetLocale" => "fr",
]);
});
// 3. localizeChat
runTest("localizeChat", function() use ($engine) {
return $engine->localizeChat([
["name" => "Alice", "text" => "Hello, how are you?"],
["name" => "Bob", "text" => "I am fine, thank you!"],
["name" => "Alice", "text" => "What are you doing today?"]
], [
"sourceLocale" => "en",
"targetLocale" => "de",
]);
});
// 4. batchLocalizeText
runTest("batchLocalizeText", function() use ($engine) {
return $engine->batchLocalizeText("Hello, world!", [
"sourceLocale" => "en",
"targetLocales" => ["es", "fr", "de"],
]);
});
// 5. recognizeLocale
runTest("recognizeLocale", function() use ($engine) {
return $engine->recognizeLocale("Bonjour le monde");
});
// 6. localizeObject with reference
runTest("localizeObject with reference", function() use ($engine) {
return $engine->localizeObject([
"greeting" => "Hello",
"farewell" => "Goodbye",
], [
"sourceLocale" => "en",
"targetLocale" => "es",
"reference" => [
"fr" => [
"greeting" => "Bonjour",
"farewell" => "Au revoir"
]
],
]);
});
// 7. Progress callback
runTest("Progress Callback", function() use ($engine) {
$progressCalled = false;
$progressValue = 0;
$result = $engine->localizeText("Hello, this is a test with progress callback!", [
"sourceLocale" => "en",
"targetLocale" => "es",
], function ($progress) use (&$progressCalled, &$progressValue) {
$progressCalled = true;
$progressValue = $progress;
echo "Progress: $progress%\n";
});
if (!$progressCalled) {
throw new \Exception("Progress callback was not called");
}
return [
"result" => $result,
"progressCalled" => $progressCalled,
"progressValue" => $progressValue
];
});
echo "\n=== All tests completed: $passed passed, $failed failed ===\n";
exit($failed > 0 ? 1 : 0);