1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\SymfonyStorage\Tests\Unit; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\Translation\Loader\XliffFileLoader; |
16
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
17
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
18
|
|
|
use Symfony\Component\Translation\Reader\TranslationReader; |
19
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriter; |
20
|
|
|
use Translation\Common\Model\Message; |
21
|
|
|
use Translation\SymfonyStorage\FileStorage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Tobias Nyholm <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class FileStorageTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
public function testConstructor() |
29
|
|
|
{ |
30
|
|
|
$storage = new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), ['foo']); |
31
|
|
|
$this->assertInstanceOf(FileStorage::class, $storage); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testConstructorEmptyArray() |
35
|
|
|
{ |
36
|
|
|
$this->expectException(\LogicException::class); |
37
|
|
|
|
38
|
|
|
new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), []); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCreateNewCatalogue() |
42
|
|
|
{ |
43
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
44
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
45
|
|
|
->disableOriginalConstructor() |
46
|
|
|
->getMock(); |
47
|
|
|
$writer->expects($this->once()) |
48
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
49
|
|
|
->with( |
50
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
51
|
|
|
'xlf', |
52
|
|
|
['path' => 'foo', 'xliff_version' => '2.0'] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$storage = new FileStorage($writer, $this->createTranslationLoader(), ['foo']); |
|
|
|
|
56
|
|
|
$storage->create(new Message('key', 'domain', 'en', 'Message')); |
57
|
|
|
|
58
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
59
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
60
|
|
|
->disableOriginalConstructor() |
61
|
|
|
->getMock(); |
62
|
|
|
$writer->expects($this->once()) |
63
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
64
|
|
|
->with( |
65
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
66
|
|
|
'format', |
67
|
|
|
['path' => 'bar', 'default_output_format' => 'format', 'xliff_version' => '2.0'] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$storage = new FileStorage($writer, $this->createTranslationLoader(), ['bar'], ['default_output_format' => 'format']); |
|
|
|
|
71
|
|
|
$storage->create(new Message('key', 'domain', 'en', 'Message')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function testCreateExistingCatalogue() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
77
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->getMock(); |
80
|
|
|
$writer->expects($this->once()) |
81
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
82
|
|
|
->with( |
83
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
84
|
|
|
'xlf', |
85
|
|
|
['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$loader = $this->createTranslationLoader(); |
89
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
90
|
|
|
$storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$storage->create(new Message('key', 'messages', 'en', 'Translation')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGet() |
96
|
|
|
{ |
97
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
98
|
|
|
->disableOriginalConstructor() |
99
|
|
|
->getMock(); |
100
|
|
|
|
101
|
|
|
$loader = $this->createTranslationLoader(); |
102
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
103
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation()); |
106
|
|
|
|
107
|
|
|
// Missing locale |
108
|
|
|
$this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation()); |
109
|
|
|
|
110
|
|
|
// Missing domain |
111
|
|
|
$this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation()); |
112
|
|
|
|
113
|
|
|
// Missing key |
114
|
|
|
$this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testUpdate() |
118
|
|
|
{ |
119
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
120
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
121
|
|
|
->disableOriginalConstructor() |
122
|
|
|
->getMock(); |
123
|
|
|
$writer->expects($this->exactly(2)) |
124
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
125
|
|
|
->with( |
126
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
127
|
|
|
'xlf', |
128
|
|
|
['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$loader = $this->createTranslationLoader(); |
132
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
133
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
|
|
|
|
134
|
|
|
|
135
|
|
|
$storage->update(new Message('key', 'messages', 'en', 'Translation')); |
136
|
|
|
$storage->update(new Message('test_1', 'messages', 'en', 'Translation')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
142
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
143
|
|
|
->disableOriginalConstructor() |
144
|
|
|
->getMock(); |
145
|
|
|
|
146
|
|
|
$writer->expects($this->once()) |
147
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
148
|
|
|
->with( |
149
|
|
|
$this->callback(function (MessageCatalogueInterface $catalogue) { |
150
|
|
|
return !$catalogue->defines('test_0', 'messages'); |
151
|
|
|
}), |
152
|
|
|
'xlf', |
153
|
|
|
['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$loader = $this->createTranslationLoader(); |
157
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
158
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$storage->delete('en', 'messages', 'test_0'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testImport() |
164
|
|
|
{ |
165
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
|
|
|
|
166
|
|
|
->setMethods([$this->getMethodNameToWriteTranslations()]) |
167
|
|
|
->disableOriginalConstructor() |
168
|
|
|
->getMock(); |
169
|
|
|
|
170
|
|
|
$writer->expects($this->once()) |
171
|
|
|
->method($this->getMethodNameToWriteTranslations()) |
172
|
|
|
->with( |
173
|
|
|
$this->callback(function (MessageCatalogueInterface $catalogue) { |
174
|
|
|
return $catalogue->defines('test_4711', 'messages'); |
175
|
|
|
}), |
176
|
|
|
'xlf', |
177
|
|
|
['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$loader = $this->createTranslationLoader(); |
181
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
182
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
|
|
|
|
183
|
|
|
$catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]); |
184
|
|
|
|
185
|
|
|
$storage->import($catalogue); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testExport() |
189
|
|
|
{ |
190
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
191
|
|
|
->disableOriginalConstructor() |
192
|
|
|
->getMock(); |
193
|
|
|
|
194
|
|
|
$loader = $this->createTranslationLoader(); |
195
|
|
|
$loader->addLoader('xlf', new XliffFileLoader()); |
196
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
|
|
|
|
197
|
|
|
|
198
|
|
|
$catalogue = new MessageCatalogue('en'); |
199
|
|
|
$storage->export($catalogue); |
200
|
|
|
$this->assertTrue($catalogue->defines('test_0', 'messages')); |
201
|
|
|
$this->assertTrue($catalogue->defines('test_1', 'messages')); |
202
|
|
|
|
203
|
|
|
// Wrong locale |
204
|
|
|
$catalogue = new MessageCatalogue('sv'); |
205
|
|
|
$storage->export($catalogue); |
206
|
|
|
$this->assertFalse($catalogue->defines('test_0', 'messages')); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
private function getFixturePath() |
213
|
|
|
{ |
214
|
|
|
return realpath(__DIR__.'/../Fixtures/single-file'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return TranslationReader |
219
|
|
|
*/ |
220
|
|
|
private function createTranslationLoader() |
221
|
|
|
{ |
222
|
|
|
return new TranslationReader(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
private function getMethodNameToWriteTranslations() |
226
|
|
|
{ |
227
|
|
|
if (method_exists(TranslationWriter::class, 'write')) { |
228
|
|
|
return 'write'; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return 'writeTranslations'; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.