Skip to content

Commit 7396a12

Browse files
Update tests to match fixed API implementation and add test script for all methods (#7)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent 9748933 commit 7396a12

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

test-all-methods.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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";

tests/LingoDotDevEngineTest.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ public function testLocalizeChat()
193193
200, [], json_encode(
194194
[
195195
'data' => [
196-
'chat_0' => '¡Hola, cómo estás?',
197-
'chat_1' => '¡Estoy bien, gracias!'
196+
'chat' => [
197+
['text' => '¡Hola, cómo estás?'],
198+
['text' => '¡Estoy bien, gracias!']
199+
]
198200
]
199201
]
200202
)
@@ -272,6 +274,53 @@ public function testErrorHandling()
272274
]
273275
);
274276
}
277+
278+
/**
279+
* Tests the reference parameter handling
280+
*
281+
* @return void
282+
*/
283+
public function testReferenceParameterHandling()
284+
{
285+
$mock = new MockHandler([
286+
new Response(200, [], json_encode(['data' => ['text' => 'Hola, mundo!']]))
287+
]);
288+
289+
$requestData = null;
290+
$history = [];
291+
$historyMiddleware = \GuzzleHttp\Middleware::history($history);
292+
293+
$handlerStack = HandlerStack::create($mock);
294+
$handlerStack->push($historyMiddleware);
295+
296+
$client = new Client(['handler' => $handlerStack]);
297+
298+
$engine = new LingoDotDevEngine(['apiKey' => 'test-api-key']);
299+
300+
$reflection = new ReflectionClass($engine);
301+
$property = $reflection->getProperty('_httpClient');
302+
$property->setAccessible(true);
303+
$property->setValue($engine, $client);
304+
305+
$engine->localizeText('Hello, world!', [
306+
'sourceLocale' => 'en',
307+
'targetLocale' => 'es'
308+
]);
309+
310+
$this->assertCount(1, $history);
311+
$request = $history[0]['request'];
312+
$requestBody = json_decode($request->getBody()->getContents(), true);
313+
314+
$this->assertArrayHasKey('reference', $requestBody);
315+
$this->assertEquals(new \stdClass(), $requestBody['reference']);
316+
317+
$this->assertArrayHasKey('params', $requestBody);
318+
$this->assertArrayHasKey('locale', $requestBody);
319+
$this->assertArrayHasKey('data', $requestBody);
320+
$this->assertEquals('en', $requestBody['locale']['source']);
321+
$this->assertEquals('es', $requestBody['locale']['target']);
322+
$this->assertEquals(['text' => 'Hello, world!'], $requestBody['data']);
323+
}
275324

276325
/**
277326
* Tests the progress callback functionality

0 commit comments

Comments
 (0)