Skip to content

Commit bd6f711

Browse files
bocharsky-bwNyholm
authored andcommitted
Fix deprecated writeTranslations() call with write() when available (#22)
* Fix deprecated writeTranslations() call with write() when available * Fix tests when writeTranslations() deprecated method is called * Fix StyleCI fail: Add dot in the end of description * Actually, we do always pass $options arg - make it required * Align method parameters to fix StyleCI fails
1 parent f76b3a1 commit bd6f711

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/FileStorage.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
164164
$path = (string) $resource;
165165
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
166166
$options['path'] = str_replace($matches[0], '', $path);
167-
$this->writer->writeTranslations($catalogue, $matches[1], $options);
167+
$this->writeTranslations($catalogue, $matches[1], $options);
168168
$written = true;
169169
}
170170
}
@@ -176,7 +176,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
176176

177177
$options['path'] = reset($this->dir);
178178
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
179-
$this->writer->writeTranslations($catalogue, $format, $options);
179+
$this->writeTranslations($catalogue, $format, $options);
180180
}
181181

182182
/**
@@ -210,4 +210,23 @@ private function loadCatalogue($locale, array $dirs)
210210

211211
$this->catalogues[$locale] = $currentCatalogue;
212212
}
213+
214+
/**
215+
* This method calls the new TranslationWriter::write() if exist,
216+
* otherwise fallback to TranslationWriter::writeTranslations() call
217+
* to avoid BC breaks.
218+
*
219+
* @param MessageCatalogue $catalogue
220+
* @param string $format
221+
* @param array $options
222+
*/
223+
private function writeTranslations(MessageCatalogue $catalogue, $format, array $options)
224+
{
225+
if (method_exists($this->writer, 'write')) {
226+
$this->writer->write($catalogue, $format, $options);
227+
} else {
228+
// This method is deprecated since 3.4, maintained to avoid BC breaks
229+
$this->writer->writeTranslations($catalogue, $format, $options);
230+
}
231+
}
213232
}

tests/Unit/FileStorageTest.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function testConstructorEmptyArray()
5151
public function testCreateNewCatalogue()
5252
{
5353
$writer = $this->getMockBuilder(TranslationWriter::class)
54-
->setMethods(['writeTranslations'])
54+
->setMethods([$this->getMethodNameToWriteTranslations()])
5555
->disableOriginalConstructor()
5656
->getMock();
5757
$writer->expects($this->once())
58-
->method('writeTranslations')
58+
->method($this->getMethodNameToWriteTranslations())
5959
->with(
6060
$this->isInstanceOf(MessageCatalogueInterface::class),
6161
'xlf',
@@ -66,11 +66,11 @@ public function testCreateNewCatalogue()
6666
$storage->create(new Message('key', 'domain', 'en', 'Message'));
6767

6868
$writer = $this->getMockBuilder(TranslationWriter::class)
69-
->setMethods(['writeTranslations'])
69+
->setMethods([$this->getMethodNameToWriteTranslations()])
7070
->disableOriginalConstructor()
7171
->getMock();
7272
$writer->expects($this->once())
73-
->method('writeTranslations')
73+
->method($this->getMethodNameToWriteTranslations())
7474
->with(
7575
$this->isInstanceOf(MessageCatalogueInterface::class),
7676
'format',
@@ -84,11 +84,11 @@ public function testCreateNewCatalogue()
8484
public function testCreateExistingCatalogue()
8585
{
8686
$writer = $this->getMockBuilder(TranslationWriter::class)
87-
->setMethods(['writeTranslations'])
87+
->setMethods([$this->getMethodNameToWriteTranslations()])
8888
->disableOriginalConstructor()
8989
->getMock();
9090
$writer->expects($this->once())
91-
->method('writeTranslations')
91+
->method($this->getMethodNameToWriteTranslations())
9292
->with(
9393
$this->isInstanceOf(MessageCatalogueInterface::class),
9494
'xlf',
@@ -127,11 +127,11 @@ public function testGet()
127127
public function testUpdate()
128128
{
129129
$writer = $this->getMockBuilder(TranslationWriter::class)
130-
->setMethods(['writeTranslations'])
130+
->setMethods([$this->getMethodNameToWriteTranslations()])
131131
->disableOriginalConstructor()
132132
->getMock();
133133
$writer->expects($this->exactly(2))
134-
->method('writeTranslations')
134+
->method($this->getMethodNameToWriteTranslations())
135135
->with(
136136
$this->isInstanceOf(MessageCatalogueInterface::class),
137137
'xlf',
@@ -149,12 +149,12 @@ public function testUpdate()
149149
public function testDelete()
150150
{
151151
$writer = $this->getMockBuilder(TranslationWriter::class)
152-
->setMethods(['writeTranslations'])
152+
->setMethods([$this->getMethodNameToWriteTranslations()])
153153
->disableOriginalConstructor()
154154
->getMock();
155155

156156
$writer->expects($this->once())
157-
->method('writeTranslations')
157+
->method($this->getMethodNameToWriteTranslations())
158158
->with(
159159
$this->callback(function (MessageCatalogueInterface $catalogue) {
160160
return !$catalogue->defines('test_0', 'messages');
@@ -173,12 +173,12 @@ public function testDelete()
173173
public function testImport()
174174
{
175175
$writer = $this->getMockBuilder(TranslationWriter::class)
176-
->setMethods(['writeTranslations'])
176+
->setMethods([$this->getMethodNameToWriteTranslations()])
177177
->disableOriginalConstructor()
178178
->getMock();
179179

180180
$writer->expects($this->once())
181-
->method('writeTranslations')
181+
->method($this->getMethodNameToWriteTranslations())
182182
->with(
183183
$this->callback(function (MessageCatalogueInterface $catalogue) {
184184
return $catalogue->defines('test_4711', 'messages');
@@ -235,4 +235,13 @@ private function createTranslationLoader()
235235

236236
return new TranslationLoader();
237237
}
238+
239+
private function getMethodNameToWriteTranslations()
240+
{
241+
if (method_exists(TranslationWriter::class, 'write')) {
242+
return 'write';
243+
}
244+
245+
return 'writeTranslations';
246+
}
238247
}

0 commit comments

Comments
 (0)