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\Bundle\FrameworkBundle\Translation\TranslationLoader; |
16
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
17
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
18
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriter; |
19
|
|
|
use Translation\Common\Model\Message; |
20
|
|
|
use Translation\SymfonyStorage\FileStorage; |
21
|
|
|
use Translation\SymfonyStorage\Loader\XliffLoader; |
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(), new TranslationLoader(), ['foo']); |
31
|
|
|
$this->assertInstanceOf(FileStorage::class, $storage); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @expectedException \LogicException |
36
|
|
|
*/ |
37
|
|
|
public function testConstructorInvalidLoader() |
38
|
|
|
{ |
39
|
|
|
new FileStorage(new TranslationWriter(), new TranslationWriter(), ['foo']); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \LogicException |
44
|
|
|
*/ |
45
|
|
|
public function testConstructorEmptyArray() |
46
|
|
|
{ |
47
|
|
|
new FileStorage(new TranslationWriter(), new TranslationLoader(), []); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testCreateNewCatalogue() |
51
|
|
|
{ |
52
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
53
|
|
|
->setMethods(['writeTranslations']) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
$writer->expects($this->once()) |
57
|
|
|
->method('writeTranslations') |
58
|
|
|
->with( |
59
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
60
|
|
|
'xlf', |
61
|
|
|
['path' => 'foo'] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$storage = new FileStorage($writer, new TranslationLoader(), ['foo']); |
65
|
|
|
$storage->create(new Message('key', 'domain', 'en', 'Message')); |
66
|
|
|
|
67
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
68
|
|
|
->setMethods(['writeTranslations']) |
69
|
|
|
->disableOriginalConstructor() |
70
|
|
|
->getMock(); |
71
|
|
|
$writer->expects($this->once()) |
72
|
|
|
->method('writeTranslations') |
73
|
|
|
->with( |
74
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
75
|
|
|
'format', |
76
|
|
|
['path' => 'bar', 'default_output_format' => 'format'] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$storage = new FileStorage($writer, new TranslationLoader(), ['bar'], ['default_output_format' => 'format']); |
80
|
|
|
$storage->create(new Message('key', 'domain', 'en', 'Message')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testCreateExistingCatalogue() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
86
|
|
|
->setMethods(['writeTranslations']) |
87
|
|
|
->disableOriginalConstructor() |
88
|
|
|
->getMock(); |
89
|
|
|
$writer->expects($this->once()) |
90
|
|
|
->method('writeTranslations') |
91
|
|
|
->with( |
92
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
93
|
|
|
'xlf', |
94
|
|
|
['path' => $this->getFixturePath()] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$loader = new TranslationLoader(); |
98
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
99
|
|
|
$storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]); |
100
|
|
|
|
101
|
|
|
$storage->create(new Message('key', 'messages', 'en', 'Translation')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGet() |
105
|
|
|
{ |
106
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
107
|
|
|
->disableOriginalConstructor() |
108
|
|
|
->getMock(); |
109
|
|
|
|
110
|
|
|
$loader = new TranslationLoader(); |
111
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
112
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation()); |
115
|
|
|
|
116
|
|
|
// Missing locale |
117
|
|
|
$this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation()); |
118
|
|
|
|
119
|
|
|
// Missing domain |
120
|
|
|
$this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation()); |
121
|
|
|
|
122
|
|
|
// Missing key |
123
|
|
|
$this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testUpdate() |
127
|
|
|
{ |
128
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
129
|
|
|
->setMethods(['writeTranslations']) |
130
|
|
|
->disableOriginalConstructor() |
131
|
|
|
->getMock(); |
132
|
|
|
$writer->expects($this->exactly(2)) |
133
|
|
|
->method('writeTranslations') |
134
|
|
|
->with( |
135
|
|
|
$this->isInstanceOf(MessageCatalogueInterface::class), |
136
|
|
|
'xlf', |
137
|
|
|
['path' => $this->getFixturePath()] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$loader = new TranslationLoader(); |
141
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
142
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
143
|
|
|
|
144
|
|
|
$storage->update(new Message('key', 'messages', 'en', 'Translation')); |
145
|
|
|
$storage->update(new Message('test_1', 'messages', 'en', 'Translation')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
151
|
|
|
->setMethods(['writeTranslations']) |
152
|
|
|
->disableOriginalConstructor() |
153
|
|
|
->getMock(); |
154
|
|
|
|
155
|
|
|
$writer->expects($this->once()) |
156
|
|
|
->method('writeTranslations') |
157
|
|
|
->with( |
158
|
|
|
$this->callback(function (MessageCatalogueInterface $catalogue) { |
159
|
|
|
return !$catalogue->defines('test_0', 'messages'); |
160
|
|
|
}), |
161
|
|
|
'xlf', |
162
|
|
|
['path' => $this->getFixturePath()] |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$loader = new TranslationLoader(); |
166
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
167
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
168
|
|
|
|
169
|
|
|
$storage->delete('en', 'messages', 'test_0'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testImport() |
173
|
|
|
{ |
174
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
175
|
|
|
->setMethods(['writeTranslations']) |
176
|
|
|
->disableOriginalConstructor() |
177
|
|
|
->getMock(); |
178
|
|
|
|
179
|
|
|
$writer->expects($this->once()) |
180
|
|
|
->method('writeTranslations') |
181
|
|
|
->with( |
182
|
|
|
$this->callback(function (MessageCatalogueInterface $catalogue) { |
183
|
|
|
return $catalogue->defines('test_4711', 'messages'); |
184
|
|
|
}), |
185
|
|
|
'xlf', |
186
|
|
|
['path' => $this->getFixturePath()] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$loader = new TranslationLoader(); |
190
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
191
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
192
|
|
|
$catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]); |
193
|
|
|
|
194
|
|
|
$storage->import($catalogue); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testExport() |
198
|
|
|
{ |
199
|
|
|
$writer = $this->getMockBuilder(TranslationWriter::class) |
200
|
|
|
->disableOriginalConstructor() |
201
|
|
|
->getMock(); |
202
|
|
|
|
203
|
|
|
$loader = new TranslationLoader(); |
204
|
|
|
$loader->addLoader('xlf', new XliffLoader()); |
205
|
|
|
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
206
|
|
|
|
207
|
|
|
$catalogue = new MessageCatalogue('en'); |
208
|
|
|
$storage->export($catalogue); |
209
|
|
|
$this->assertTrue($catalogue->defines('test_0', 'messages')); |
210
|
|
|
$this->assertTrue($catalogue->defines('test_1', 'messages')); |
211
|
|
|
|
212
|
|
|
// Wrong locale |
213
|
|
|
$catalogue = new MessageCatalogue('sv'); |
214
|
|
|
$storage->export($catalogue); |
215
|
|
|
$this->assertFalse($catalogue->defines('test_0', 'messages')); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
private function getFixturePath() |
222
|
|
|
{ |
223
|
|
|
return realpath(__DIR__.'/../Fixtures/single-file'); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: