From 720779a1c0fd93368eb06cb723dbba4e7c9fd2cb Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 12:42:51 +0100 Subject: [PATCH 1/9] Validate XLIFF translation files --- .../Tests/Resources/TranslationFilesTest.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php new file mode 100644 index 0000000000000..8bdb215691225 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Resources; + +class ConstraintTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + libxml_use_internal_errors(true); + libxml_clear_errors(); + } + + public function testXlfFilesAreValid() + { + $translationFiles = glob(__DIR__.'/../../Resources/translations/*.xlf'); + foreach ($translationFiles as $filePath) { + $xlfFile = simplexml_load_file($filePath); + $errors = libxml_get_errors(); + + if (!empty($errors)) { + $this->renderErrors($errors); + } + + $this->assertEmpty($errors, sprintf('The "%s" file is not a valid XML file.', realpath($filePath))); + } + } + + private function renderErrors(array $errors) + { + $firstError = $errors[0]; + + echo sprintf("\nErrors found in '%s' file\n", basename($firstError->file)); + echo sprintf("(path: %s)\n\n", realpath($firstError->file)); + + foreach ($errors as $error) { + echo sprintf(" Line %d, Column %d\n", $error->line, $error->column); + echo sprintf(" %s\n", $error->message); + } + } +} From 9a60ed2c5abe0261597781652354496894c8c1c6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:14:31 +0100 Subject: [PATCH 2/9] Fixed the name of the test --- .../Validator/Tests/Resources/TranslationFilesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index 8bdb215691225..a30982c9dcd49 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Validator\Tests\Resources; -class ConstraintTest extends \PHPUnit_Framework_TestCase +class TranslationFilesTest extends \PHPUnit_Framework_TestCase { public function setUp() { From e28ee89fca79150f2763ec1dbb223fe56a4587ec Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:21:56 +0100 Subject: [PATCH 3/9] Minor refactor --- .../Validator/Tests/Resources/TranslationFilesTest.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index a30982c9dcd49..8d7f467290164 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -13,17 +13,13 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase { - public function setUp() - { - libxml_use_internal_errors(true); - libxml_clear_errors(); - } - public function testXlfFilesAreValid() { $translationFiles = glob(__DIR__.'/../../Resources/translations/*.xlf'); foreach ($translationFiles as $filePath) { - $xlfFile = simplexml_load_file($filePath); + libxml_use_internal_errors(true); + libxml_clear_errors(); + simplexml_load_file($filePath); $errors = libxml_get_errors(); if (!empty($errors)) { From 93861ac211e9304355852645e12944be9aafa7d8 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:22:54 +0100 Subject: [PATCH 4/9] Another minor refactor --- .../Validator/Tests/Resources/TranslationFilesTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index 8d7f467290164..d16655d29d7c9 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -15,9 +15,10 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase { public function testXlfFilesAreValid() { + libxml_use_internal_errors(true); + $translationFiles = glob(__DIR__.'/../../Resources/translations/*.xlf'); foreach ($translationFiles as $filePath) { - libxml_use_internal_errors(true); libxml_clear_errors(); simplexml_load_file($filePath); $errors = libxml_get_errors(); From 078fd0295027929e9e940031768b1954df1fbf96 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:37:36 +0100 Subject: [PATCH 5/9] Refactored entirely --- .../Tests/Resources/TranslationFilesTest.php | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index d16655d29d7c9..77bf89a659daf 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -13,34 +13,23 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase { - public function testXlfFilesAreValid() + /** + * @dataProvider provideTranslationFiles + */ + public function testTranslationFilesAreValid($filePath) { libxml_use_internal_errors(true); + libxml_clear_errors(); + simplexml_load_file($filePath); - $translationFiles = glob(__DIR__.'/../../Resources/translations/*.xlf'); - foreach ($translationFiles as $filePath) { - libxml_clear_errors(); - simplexml_load_file($filePath); - $errors = libxml_get_errors(); - - if (!empty($errors)) { - $this->renderErrors($errors); - } - - $this->assertEmpty($errors, sprintf('The "%s" file is not a valid XML file.', realpath($filePath))); - } + $this->assertSame(array(), libxml_get_errors()); } - private function renderErrors(array $errors) + public function provideTranslationFiles() { - $firstError = $errors[0]; - - echo sprintf("\nErrors found in '%s' file\n", basename($firstError->file)); - echo sprintf("(path: %s)\n\n", realpath($firstError->file)); - - foreach ($errors as $error) { - echo sprintf(" Line %d, Column %d\n", $error->line, $error->column); - echo sprintf(" %s\n", $error->message); - } + return array_map( + function ($filePath) { return (array) $filePath; }, + glob(__DIR__.'/../../Resources/translations/*.xlf') + ); } } From b56e9c2bafa6f41804f7a5a6d06a0a092793282b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:47:28 +0100 Subject: [PATCH 6/9] Final refactor --- .../Tests/Resources/TranslationFilesTest.php | 35 +++++++++++++++++++ .../Tests/Resources/TranslationFilesTest.php | 35 +++++++++++++++++++ .../Tests/Resources/TranslationFilesTest.php | 10 +++--- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php create mode 100644 src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php diff --git a/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php new file mode 100644 index 0000000000000..56c798f6ae34a --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Resources; + +class TranslationFilesTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider provideTranslationFiles + */ + public function testTranslationFilesAreValid($filePath) + { + try { + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); + } catch (\PHPUnit_Framework_Exception $e) { + $this->fail($e->getMessage()); + } + } + + public function provideTranslationFiles() + { + return array_map( + function ($filePath) { return (array) $filePath; }, + glob(__DIR__.'/../../Resources/translations/*.xlf') + ); + } +} diff --git a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php new file mode 100644 index 0000000000000..56c798f6ae34a --- /dev/null +++ b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Resources; + +class TranslationFilesTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider provideTranslationFiles + */ + public function testTranslationFilesAreValid($filePath) + { + try { + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); + } catch (\PHPUnit_Framework_Exception $e) { + $this->fail($e->getMessage()); + } + } + + public function provideTranslationFiles() + { + return array_map( + function ($filePath) { return (array) $filePath; }, + glob(__DIR__.'/../../Resources/translations/*.xlf') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index 77bf89a659daf..ba3123c6860fc 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -18,11 +18,11 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase */ public function testTranslationFilesAreValid($filePath) { - libxml_use_internal_errors(true); - libxml_clear_errors(); - simplexml_load_file($filePath); - - $this->assertSame(array(), libxml_get_errors()); + try { + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); + } catch (\PHPUnit_Framework_Exception $e) { + $this->fail($e->getMessage()); + } } public function provideTranslationFiles() From 43b88b0bb3bbe08ee789125e193439c4f4c73194 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:48:12 +0100 Subject: [PATCH 7/9] Fixed a namespace --- .../Component/Security/Tests/Resources/TranslationFilesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php index 56c798f6ae34a..2d5cbd6fb385c 100644 --- a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Form\Tests\Resources; +namespace Symfony\Component\Security\Tests\Resources; class TranslationFilesTest extends \PHPUnit_Framework_TestCase { From d32474318d7c28a3833b6f026ab65bc08f89688f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 15:59:12 +0100 Subject: [PATCH 8/9] Minor rename and simplified tests --- .../Form/Tests/Resources/TranslationFilesTest.php | 8 ++------ .../Security/Tests/Resources/TranslationFilesTest.php | 8 ++------ .../Validator/Tests/Resources/TranslationFilesTest.php | 8 ++------ 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php index 56c798f6ae34a..3c856e538f376 100644 --- a/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php @@ -16,13 +16,9 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase /** * @dataProvider provideTranslationFiles */ - public function testTranslationFilesAreValid($filePath) + public function testTranslationFileIsValid($filePath) { - try { - \PHPUnit_Util_XML::loadfile($filePath, false, false, true); - } catch (\PHPUnit_Framework_Exception $e) { - $this->fail($e->getMessage()); - } + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); } public function provideTranslationFiles() diff --git a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php index 2d5cbd6fb385c..c3de028bc992d 100644 --- a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php @@ -16,13 +16,9 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase /** * @dataProvider provideTranslationFiles */ - public function testTranslationFilesAreValid($filePath) + public function testTranslationFileIsValid($filePath) { - try { - \PHPUnit_Util_XML::loadfile($filePath, false, false, true); - } catch (\PHPUnit_Framework_Exception $e) { - $this->fail($e->getMessage()); - } + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); } public function provideTranslationFiles() diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index ba3123c6860fc..4750e333ad7a2 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -16,13 +16,9 @@ class TranslationFilesTest extends \PHPUnit_Framework_TestCase /** * @dataProvider provideTranslationFiles */ - public function testTranslationFilesAreValid($filePath) + public function testTranslationFileIsValid($filePath) { - try { - \PHPUnit_Util_XML::loadfile($filePath, false, false, true); - } catch (\PHPUnit_Framework_Exception $e) { - $this->fail($e->getMessage()); - } + \PHPUnit_Util_XML::loadfile($filePath, false, false, true); } public function provideTranslationFiles() From 2e62442ed679936c3e65fd3f07d00c36682bc73c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Feb 2016 16:06:17 +0100 Subject: [PATCH 9/9] Use dirname() to produce better results when an error occurs --- .../Component/Form/Tests/Resources/TranslationFilesTest.php | 2 +- .../Component/Security/Tests/Resources/TranslationFilesTest.php | 2 +- .../Validator/Tests/Resources/TranslationFilesTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php index 3c856e538f376..7596b879a18b4 100644 --- a/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Form/Tests/Resources/TranslationFilesTest.php @@ -25,7 +25,7 @@ public function provideTranslationFiles() { return array_map( function ($filePath) { return (array) $filePath; }, - glob(__DIR__.'/../../Resources/translations/*.xlf') + glob(dirname(dirname(__DIR__)).'/Resources/translations/*.xlf') ); } } diff --git a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php index c3de028bc992d..341ec87ea4105 100644 --- a/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Security/Tests/Resources/TranslationFilesTest.php @@ -25,7 +25,7 @@ public function provideTranslationFiles() { return array_map( function ($filePath) { return (array) $filePath; }, - glob(__DIR__.'/../../Resources/translations/*.xlf') + glob(dirname(dirname(__DIR__)).'/Resources/translations/*.xlf') ); } } diff --git a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php index 4750e333ad7a2..8a766611cd4f9 100644 --- a/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php +++ b/src/Symfony/Component/Validator/Tests/Resources/TranslationFilesTest.php @@ -25,7 +25,7 @@ public function provideTranslationFiles() { return array_map( function ($filePath) { return (array) $filePath; }, - glob(__DIR__.'/../../Resources/translations/*.xlf') + glob(dirname(dirname(__DIR__)).'/Resources/translations/*.xlf') ); } }