/
FewBufferTranslationServiceAtOnceTest.php
109 lines (91 loc) · 5.52 KB
/
FewBufferTranslationServiceAtOnceTest.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
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
<?php
namespace ALI\BufferTranslation\Tests\unit;
use ALI\BufferTranslation\BufferTranslation;
use ALI\BufferTranslation\Tests\components\Factories\SourceFactory;
use ALI\Translator\Languages\Language;
use ALI\Translator\Languages\Repositories\ArrayLanguageRepository;
use ALI\Translator\PlainTranslator\PlainTranslatorFactory;
use ALI\Translator\Source\SourceInterface;
use PHPUnit\Framework\TestCase;
class FewBufferTranslationServiceAtOnceTest extends TestCase
{
public function test()
{
$languageRepository = new ArrayLanguageRepository();
$languageRepository->save(new Language(SourceFactory::ORIGINAL_LANGUAGE_ISO, 'Language 1', SourceFactory::ORIGINAL_LANGUAGE_ALIAS), true);
$languageRepository->save(new Language(SourceFactory::ADDITIONAL_ORIGINAL_LANGUAGE_ISO, 'Language 2', SourceFactory::ADDITIONAL_ORIGINAL_LANGUAGE_ALIAS), true);
$languageRepository->save(new Language(SourceFactory::TRANSLATION_FOR_LANGUAGE_ISO, 'Language 3', SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS), true);
$sourceFactory = new SourceFactory();
// EN -> UK buffer
{
$firstSource = $sourceFactory->generateSource(SourceFactory::ORIGINAL_LANGUAGE_ALIAS);
$firstSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'Hello {animal}!', 'Привіт {animal}!');
$firstPlainTranslator = (new PlainTranslatorFactory())->createPlainTranslator($firstSource, SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS);
$firstBufferTranslation = new BufferTranslation($firstPlainTranslator, $languageRepository);
}
// RU -> UK buffer
{
$secondSource = $sourceFactory->generateSource(SourceFactory::ADDITIONAL_ORIGINAL_LANGUAGE_ALIAS);
$secondSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'Кот', 'Кіт');
$secondPlainTranslator = (new PlainTranslatorFactory())->createPlainTranslator($secondSource, SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS);
$secondBufferTranslation = new BufferTranslation($secondPlainTranslator, $languageRepository);
}
// Bae checks
{
// Create a "soup" of buffers
$buffer = $firstBufferTranslation->add('Hello {animal}!', [
'animal' => $secondBufferTranslation->createAndAddTextTemplateItem('Кот')
]);
$firstBufferTranslation->preTranslateAllInsideTextTemplates();
// Check that own "TextTemplateItem" is translated
$ownTextTemplate = $firstBufferTranslation->getTextTemplateItemByBufferKey($buffer);
$this->assertEquals('Привіт {animal}!', $ownTextTemplate->getContent());
// Check that "TextTemplateItem" from the second Buffer do not translated
$this->assertEquals(
'Кот',
$ownTextTemplate->getChildTextTemplatesCollection()->get('animal')->getContent()
);
// Now translate "$secondBufferTranslation" and again check
$secondBufferTranslation->preTranslateAllInsideTextTemplates();
$this->assertEquals(
'Кіт',
$ownTextTemplate->getChildTextTemplatesCollection()->get('animal')->getContent()
);
// And full translation
$this->assertEquals('Привіт Кіт!', $firstBufferTranslation->translateBuffer($buffer));
}
// High investment
{
$buffer = $this->createHighInvestmentBuffer($firstBufferTranslation, $secondBufferTranslation, $firstSource, $secondSource);
$this->assertEquals('Це же милий стол!', $firstBufferTranslation->translateBuffer($buffer));
$secondBufferTranslation->preTranslateAllInsideTextTemplates();
$this->assertEquals('Це ж милий стіл!', $firstBufferTranslation->translateBuffer($buffer));
// Make preTranslation of all buffers
$buffer = $this->createHighInvestmentBuffer($firstBufferTranslation, $secondBufferTranslation, $firstSource, $secondSource);
$secondBufferTranslation->preTranslateAllInsideTextTemplates();
$firstBufferTranslation->preTranslateAllInsideTextTemplates();
$this->assertEquals('Це ж милий стіл!', $firstBufferTranslation->translateBuffer($buffer));
}
}
protected function createHighInvestmentBuffer(
BufferTranslation $firstBufferTranslation,
BufferTranslation $secondBufferTranslation,
SourceInterface $firstSource,
SourceInterface $secondSource
): string
{
$firstBufferTranslation->flush();
$secondBufferTranslation->flush();
$firstSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'It {object}!', 'Це {object}!');
$secondSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'же {object}', 'ж {object}');
$firstSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'fucking {object}', 'милий {object}');
$secondSource->saveTranslate(SourceFactory::TRANSLATION_FOR_LANGUAGE_ALIAS, 'стол', 'стіл');
return $firstBufferTranslation->add('It {object}!', [
'object' => $secondBufferTranslation->createAndAddTextTemplateItem('же {object}', [
'object' => $firstBufferTranslation->createAndAddTextTemplateItem('fucking {object}', [
'object' => $secondBufferTranslation->createAndAddTextTemplateItem('стол')
])
])
]);
}
}