forked from php-gettext/Gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTest.php
More file actions
142 lines (115 loc) · 5.22 KB
/
MergeTest.php
File metadata and controls
142 lines (115 loc) · 5.22 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
<?php
declare(strict_types = 1);
namespace Gettext\Tests;
use Brick\VarExporter\VarExporter;
use Gettext\Merge;
use Gettext\Translation;
use Gettext\Translations;
use PHPUnit\Framework\TestCase;
class MergeTest extends TestCase
{
private static function createPOT(): Translations
{
$translations = Translations::create('my-domain');
$translations->getHeaders()
->set('POT-Creation-Date', '2019-10-10 10:10:10')
->set('Last-Translator', '')
->set('X-Foo', 'foo')
->set('X-Generator', 'PHP Gettext scanner');
$translations->getFlags()->add('fuzzy');
$translation = Translation::create(null, 'title');
$translation->getReferences()->add('template.php', 3);
$translations->add($translation);
$translation = Translation::create(null, 'intro');
$translation->getReferences()->add('template.php', 4);
$translations->add($translation);
$translation = Translation::create(null, 'one comment');
$translation->setPlural('%s comments');
$translation->getReferences()->add('template.php', 5);
$translation->getExtractedComments()->add('Number of comments of the article');
$translations->add($translation);
$translation = Translation::create(null, 'This is a flagged element');
$translation->getReferences()->add('template.php', 10);
$translation->getFlags()->add('c-code');
$translations->add($translation);
$translation = Translation::create(null, 'This is a new translation');
$translation->getReferences()->add('template.php', 11);
$translations->add($translation);
return $translations;
}
private static function createPO(): Translations
{
$translations = Translations::create('my-domain');
$translations->getHeaders()
->set('Last-Translator', 'Oscar')
->set('X-Generator', 'PHP Gettext scanner')
->set('Language-Team', 'My Team')
->set('X-Foo', 'bar')
->set('Language', 'gl_ES');
$translations->setDescription('This is a description');
$translation = Translation::create(null, 'title');
$translation->getReferences()
->add('template.php', 2)
->add('other-template.php', 2);
$translation->translate('Título');
$translations->add($translation);
$translation = Translation::create(null, 'subtitle');
$translation->getReferences()->add('template.php', 2);
$translation->translate('Subtítulo');
$translations->add($translation);
$translation = Translation::create(null, 'intro');
$translation->getReferences()->add('template.php', 4);
$translation->getComments()->add('Disabled comment');
$translation->translate('Intro');
$translation->disable();
$translations->add($translation);
$translation = Translation::create(null, 'one comment');
$translation->setPlural('%s comments');
$translation->getReferences()->add('template.php', 6);
$translation->getExtractedComments()->add('Number of comments of the article');
$translation->translate('Un comentario');
$translation->translatePlural('%s comentarios');
$translations->add($translation);
$translation = Translation::create(null, 'This is a flagged element');
$translation->getFlags()->add('a-code');
$translation->getComments()->add('This is a comment');
$translations->add($translation);
return $translations;
}
public function testNoStrategy(): void
{
$pot = self::createPOT();
$po = self::createPO();
$merged = $pot->mergeWith($po);
$this->assertSnapshot(__FUNCTION__, $merged);
}
/**
* We want to use the scanner to fetch new entries and complete them with PO files.
*/
public function testScanAndLoadStrategy(): void
{
$pot = self::createPOT();
$po = self::createPO();
$strategy = Merge::HEADERS_OVERRIDE // Override the headers with the PO values
| Merge::TRANSLATIONS_OURS // Keep only the scanned entries
| Merge::TRANSLATIONS_OVERRIDE // Apply the changes of the PO
| Merge::EXTRACTED_COMMENTS_OURS // Keep only the extracted comments
| Merge::REFERENCES_OURS // Keep only the scanned references
| Merge::FLAGS_THEIRS // Keep the flags in PO
| Merge::COMMENTS_THEIRS; // Keep the comments in PO
$this->assertSame($strategy, Merge::SCAN_AND_LOAD);
$merged = $pot->mergeWith($po, $strategy);
$this->assertSnapshot(__FUNCTION__, $merged);
}
private function assertSnapshot(string $name, Translations $translations, bool $forceCreate = false): void
{
$file = __DIR__."/snapshots/{$name}.php";
$array = $translations->toArray();
if (!is_file($file) || $forceCreate) {
$code = sprintf('<?php %s', VarExporter::export($array, VarExporter::ADD_RETURN));
file_put_contents($file, $code);
}
$expected = require $file;
$this->assertSame($expected, $array);
}
}