From 0d45d844a02e65d5cc05046a51a51912bd989566 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Thu, 21 Dec 2017 11:05:13 +0200 Subject: [PATCH 1/5] Fix deprecated writeTranslations() call with write() when available --- src/FileStorage.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/FileStorage.php b/src/FileStorage.php index c447d8e..58f9d78 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -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; } } @@ -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); } /** @@ -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 $format + * @param array $options + */ + private function writeTranslations(MessageCatalogue $catalogue, $format, $options = array()) + { + 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); + } + } } From b9a9cbc5eb799fd89770e91fbfd581ca17b03ca2 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Thu, 21 Dec 2017 11:25:43 +0200 Subject: [PATCH 2/5] Fix tests when writeTranslations() deprecated method is called --- tests/Unit/FileStorageTest.php | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/Unit/FileStorageTest.php b/tests/Unit/FileStorageTest.php index 26f6e1d..706972e 100644 --- a/tests/Unit/FileStorageTest.php +++ b/tests/Unit/FileStorageTest.php @@ -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', @@ -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', @@ -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', @@ -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', @@ -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'); @@ -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'); @@ -235,4 +235,13 @@ private function createTranslationLoader() return new TranslationLoader(); } + + private function getMethodNameToWriteTranslations() + { + if (method_exists(TranslationWriter::class, 'write')) { + return 'write'; + } + + return 'writeTranslations'; + } } From 09b4346739bba3e089bdae4b6b5ef6c82d21de2c Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Thu, 21 Dec 2017 11:30:04 +0200 Subject: [PATCH 3/5] Fix StyleCI fail: Add dot in the end of description --- src/FileStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FileStorage.php b/src/FileStorage.php index 58f9d78..b2ea656 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -214,7 +214,7 @@ private function loadCatalogue($locale, array $dirs) /** * This method calls the new TranslationWriter::write() if exist, * otherwise fallback to TranslationWriter::writeTranslations() call - * to avoid BC breaks + * to avoid BC breaks. * * @param MessageCatalogue $catalogue * @param $format From a6fcd5ad8ddc0bf22c3b15fa0b4390e14831a055 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Thu, 21 Dec 2017 11:32:29 +0200 Subject: [PATCH 4/5] Actually, we do always pass $options arg - make it required --- src/FileStorage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FileStorage.php b/src/FileStorage.php index b2ea656..ccb32e4 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -217,10 +217,10 @@ private function loadCatalogue($locale, array $dirs) * to avoid BC breaks. * * @param MessageCatalogue $catalogue - * @param $format + * @param string $format * @param array $options */ - private function writeTranslations(MessageCatalogue $catalogue, $format, $options = array()) + private function writeTranslations(MessageCatalogue $catalogue, $format, array $options) { if (method_exists($this->writer, 'write')) { $this->writer->write($catalogue, $format, $options); From 9bee0e36fc2529bc6b59bd5e312286d1749c0491 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Thu, 21 Dec 2017 11:35:38 +0200 Subject: [PATCH 5/5] Align method parameters to fix StyleCI fails --- src/FileStorage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FileStorage.php b/src/FileStorage.php index ccb32e4..818b644 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -217,8 +217,8 @@ private function loadCatalogue($locale, array $dirs) * to avoid BC breaks. * * @param MessageCatalogue $catalogue - * @param string $format - * @param array $options + * @param string $format + * @param array $options */ private function writeTranslations(MessageCatalogue $catalogue, $format, array $options) {