Skip to content

Fix deprecated writeTranslations() call with write() when available #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
$path = (string) $resource;
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
$options['path'] = str_replace($matches[0], '', $path);
$this->writer->writeTranslations($catalogue, $matches[1], $options);
$this->writeTranslations($catalogue, $matches[1], $options);
$written = true;
}
}
Expand All @@ -176,7 +176,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)

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

/**
Expand Down Expand Up @@ -210,4 +210,23 @@ private function loadCatalogue($locale, array $dirs)

$this->catalogues[$locale] = $currentCatalogue;
}

/**
* This method calls the new TranslationWriter::write() if exist,
* otherwise fallback to TranslationWriter::writeTranslations() call
* to avoid BC breaks.
*
* @param MessageCatalogue $catalogue
* @param string $format
* @param array $options
*/
private function writeTranslations(MessageCatalogue $catalogue, $format, array $options)
{
if (method_exists($this->writer, 'write')) {
$this->writer->write($catalogue, $format, $options);
} else {
// This method is deprecated since 3.4, maintained to avoid BC breaks
$this->writer->writeTranslations($catalogue, $format, $options);
}
}
}
33 changes: 21 additions & 12 deletions tests/Unit/FileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function testConstructorEmptyArray()
public function testCreateNewCatalogue()
{
$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();
$writer->expects($this->once())
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->isInstanceOf(MessageCatalogueInterface::class),
'xlf',
Expand All @@ -66,11 +66,11 @@ public function testCreateNewCatalogue()
$storage->create(new Message('key', 'domain', 'en', 'Message'));

$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();
$writer->expects($this->once())
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->isInstanceOf(MessageCatalogueInterface::class),
'format',
Expand All @@ -84,11 +84,11 @@ public function testCreateNewCatalogue()
public function testCreateExistingCatalogue()
{
$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();
$writer->expects($this->once())
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->isInstanceOf(MessageCatalogueInterface::class),
'xlf',
Expand Down Expand Up @@ -127,11 +127,11 @@ public function testGet()
public function testUpdate()
{
$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();
$writer->expects($this->exactly(2))
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->isInstanceOf(MessageCatalogueInterface::class),
'xlf',
Expand All @@ -149,12 +149,12 @@ public function testUpdate()
public function testDelete()
{
$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();

$writer->expects($this->once())
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->callback(function (MessageCatalogueInterface $catalogue) {
return !$catalogue->defines('test_0', 'messages');
Expand All @@ -173,12 +173,12 @@ public function testDelete()
public function testImport()
{
$writer = $this->getMockBuilder(TranslationWriter::class)
->setMethods(['writeTranslations'])
->setMethods([$this->getMethodNameToWriteTranslations()])
->disableOriginalConstructor()
->getMock();

$writer->expects($this->once())
->method('writeTranslations')
->method($this->getMethodNameToWriteTranslations())
->with(
$this->callback(function (MessageCatalogueInterface $catalogue) {
return $catalogue->defines('test_4711', 'messages');
Expand Down Expand Up @@ -235,4 +235,13 @@ private function createTranslationLoader()

return new TranslationLoader();
}

private function getMethodNameToWriteTranslations()
{
if (method_exists(TranslationWriter::class, 'write')) {
return 'write';
}

return 'writeTranslations';
}
}