forked from Stichoza/google-translate-php
/
TranslationTest.php
84 lines (61 loc) · 3.61 KB
/
TranslationTest.php
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
<?php
namespace Stichoza\GoogleTranslate\Tests;
use PHPUnit\Framework\TestCase;
use Stichoza\GoogleTranslate\GoogleTranslate;
class TranslationTest extends TestCase
{
public GoogleTranslate $tr;
public function setUp(): void
{
$this->tr = new GoogleTranslate();
}
public function testTranslation(): void
{
$result = $this->tr->setSource('en')->setTarget('ka')->translate('Hello');
$this->assertEqualsIgnoringCase($result, 'გამარჯობა', 'Translation should be correct.');
}
public function testTranslationEquality(): void
{
$resultOne = GoogleTranslate::trans('Hello', 'ka', 'en');
$resultTwo = $this->tr->setSource('en')->setTarget('ka')->translate('Hello');
$this->assertEqualsIgnoringCase($resultOne, $resultTwo, 'Static and instance methods should return same result.');
}
public function testTranslationKeyExtraction(): void
{
$result = $this->tr->setSource('en')->setTarget('fr')->preserveParameters()->translate('Hello :name, how are :type_of_greeting?');
$this->assertEquals('Bonjour :name, comment vont :type_of_greeting ?', $result, 'Translation should be correct with proper key extraction.');
}
public function testCanIgnoreTranslationKeyExtraction()
{
$result = $this->tr->setSource('en')->setTarget('fr')->translate('Hello :name how are :greeting?');
$this->assertEquals('Bonjour :nom, comment allez-vous :salut ?', $result, 'Translation should be correct and ignores key extraction if not set.');
}
public function testCanCustomizeExtractionPattern()
{
$result = $this->tr->setSource('en')->setTarget('fr')->preserveParameters('/\{\{([^}]+)\}\}/')->translate('Hello {{name}}, how are {{type_of_greeting}}?');
$this->assertEquals('Bonjour {{name}}, comment vont {{type_of_greeting}} ?', $result, 'Translation should be correct and ignores key extraction if not set.');
}
public function testNewerLanguageTranslation(): void
{
$result = $this->tr->setSource('en')->setTarget('tk')->translate('Hello');
$this->assertEqualsIgnoringCase($result, 'Salam', 'Newer languages should be translatable.');
}
public function testUTF16Translation(): void
{
$result = $this->tr->setSource('en')->setTarget('de')->translate('yes 👍🏽');
$this->assertEqualsIgnoringCase($result, 'ja 👍🏽', 'UTF-16 strings should be translatable');
}
public function testLargeTextTranslation(): void
{
$text = "Google Translate is a multilingual neural machine translation service developed by Google to translate text, documents and websites from one language into another. It offers a website interface, a mobile app for Android and iOS, and an API that helps developers build browser extensions and software applications. As of November 2022, Google Translate supports 133 languages at various levels, and as of April 2016, claimed over 500 million total users, with more than 100 billion words translated daily, after the company stated in May 2013 that it served over 200 million people daily.";
$output = $this->tr->setTarget('uk')->translate($text);
$this->assertIsString($output, 'Translation should be string');
$this->assertNotEmpty($output, 'Translation should not be empty');
$this->assertNotEqualsIgnoringCase($text, $output, 'Translation should be different from original');
}
public function testRawResponse(): void
{
$rawResult = $this->tr->getResponse('cat');
$this->assertIsArray($rawResult, 'Method getResponse() should return an array');
}
}