XliffLintCommandTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\Translation\Command\XliffLintCommand;
  16. /**
  17. * Tests the XliffLintCommand.
  18. *
  19. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  20. */
  21. class XliffLintCommandTest extends TestCase
  22. {
  23. private $files;
  24. public function testLintCorrectFile()
  25. {
  26. $tester = $this->createCommandTester();
  27. $filename = $this->createFile();
  28. $tester->execute(
  29. ['filename' => $filename],
  30. ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
  31. );
  32. $this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
  33. $this->assertContains('OK', trim($tester->getDisplay()));
  34. }
  35. public function testLintCorrectFiles()
  36. {
  37. $tester = $this->createCommandTester();
  38. $filename1 = $this->createFile();
  39. $filename2 = $this->createFile();
  40. $tester->execute(
  41. ['filename' => [$filename1, $filename2]],
  42. ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
  43. );
  44. $this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
  45. $this->assertContains('OK', trim($tester->getDisplay()));
  46. }
  47. /**
  48. * @dataProvider provideStrictFilenames
  49. */
  50. public function testStrictFilenames($requireStrictFileNames, $fileNamePattern, $targetLanguage, $mustFail)
  51. {
  52. $tester = $this->createCommandTester($requireStrictFileNames);
  53. $filename = $this->createFile('note', $targetLanguage, $fileNamePattern);
  54. $tester->execute(
  55. ['filename' => $filename],
  56. ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
  57. );
  58. $this->assertEquals($mustFail ? 1 : 0, $tester->getStatusCode());
  59. $this->assertContains($mustFail ? '[WARNING] 0 XLIFF files have valid syntax and 1 contain errors.' : '[OK] All 1 XLIFF files contain valid syntax.', $tester->getDisplay());
  60. }
  61. public function testLintIncorrectXmlSyntax()
  62. {
  63. $tester = $this->createCommandTester();
  64. $filename = $this->createFile('note <target>');
  65. $tester->execute(['filename' => $filename], ['decorated' => false]);
  66. $this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
  67. $this->assertContains('Opening and ending tag mismatch: target line 6 and source', trim($tester->getDisplay()));
  68. }
  69. public function testLintIncorrectTargetLanguage()
  70. {
  71. $tester = $this->createCommandTester();
  72. $filename = $this->createFile('note', 'es');
  73. $tester->execute(['filename' => $filename], ['decorated' => false]);
  74. $this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
  75. $this->assertContains('There is a mismatch between the language included in the file name ("messages.en.xlf") and the "es" value used in the "target-language" attribute of the file.', trim($tester->getDisplay()));
  76. }
  77. public function testLintTargetLanguageIsCaseInsensitive()
  78. {
  79. $tester = $this->createCommandTester();
  80. $filename = $this->createFile('note', 'zh-cn', 'messages.zh_CN.xlf');
  81. $tester->execute(['filename' => $filename], ['decorated' => false]);
  82. $this->assertEquals(0, $tester->getStatusCode());
  83. $this->assertContains('[OK] All 1 XLIFF files contain valid syntax.', trim($tester->getDisplay()));
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. */
  88. public function testLintFileNotReadable()
  89. {
  90. $tester = $this->createCommandTester();
  91. $filename = $this->createFile();
  92. unlink($filename);
  93. $tester->execute(['filename' => $filename], ['decorated' => false]);
  94. }
  95. public function testGetHelp()
  96. {
  97. $command = new XliffLintCommand();
  98. $expected = <<<EOF
  99. The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
  100. the first encountered syntax error.
  101. You can validates XLIFF contents passed from STDIN:
  102. <info>cat filename | php %command.full_name%</info>
  103. You can also validate the syntax of a file:
  104. <info>php %command.full_name% filename</info>
  105. Or of a whole directory:
  106. <info>php %command.full_name% dirname</info>
  107. <info>php %command.full_name% dirname --format=json</info>
  108. EOF;
  109. $this->assertEquals($expected, $command->getHelp());
  110. }
  111. /**
  112. * @return string Path to the new file
  113. */
  114. private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf')
  115. {
  116. $xliffContent = <<<XLIFF
  117. <?xml version="1.0"?>
  118. <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  119. <file source-language="en" target-language="$targetLanguage" datatype="plaintext" original="file.ext">
  120. <body>
  121. <trans-unit id="note">
  122. <source>$sourceContent</source>
  123. <target>NOTE</target>
  124. </trans-unit>
  125. </body>
  126. </file>
  127. </xliff>
  128. XLIFF;
  129. $filename = sprintf('%s/translation-xliff-lint-test/%s', sys_get_temp_dir(), str_replace('%locale%', 'en', $fileNamePattern));
  130. file_put_contents($filename, $xliffContent);
  131. $this->files[] = $filename;
  132. return $filename;
  133. }
  134. /**
  135. * @return CommandTester
  136. */
  137. private function createCommandTester($requireStrictFileNames = true, $application = null)
  138. {
  139. if (!$application) {
  140. $application = new Application();
  141. $application->add(new XliffLintCommand(null, null, null, $requireStrictFileNames));
  142. }
  143. $command = $application->find('lint:xliff');
  144. if ($application) {
  145. $command->setApplication($application);
  146. }
  147. return new CommandTester($command);
  148. }
  149. protected function setUp()
  150. {
  151. $this->files = [];
  152. @mkdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  153. }
  154. protected function tearDown()
  155. {
  156. foreach ($this->files as $file) {
  157. if (file_exists($file)) {
  158. unlink($file);
  159. }
  160. }
  161. rmdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  162. }
  163. public function provideStrictFilenames()
  164. {
  165. yield [false, 'messages.%locale%.xlf', 'en', false];
  166. yield [false, 'messages.%locale%.xlf', 'es', true];
  167. yield [false, '%locale%.messages.xlf', 'en', false];
  168. yield [false, '%locale%.messages.xlf', 'es', true];
  169. yield [true, 'messages.%locale%.xlf', 'en', false];
  170. yield [true, 'messages.%locale%.xlf', 'es', true];
  171. yield [true, '%locale%.messages.xlf', 'en', true];
  172. yield [true, '%locale%.messages.xlf', 'es', true];
  173. }
  174. }