XliffLintCommandTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /**
  78. * @expectedException \RuntimeException
  79. */
  80. public function testLintFileNotReadable()
  81. {
  82. $tester = $this->createCommandTester();
  83. $filename = $this->createFile();
  84. unlink($filename);
  85. $tester->execute(['filename' => $filename], ['decorated' => false]);
  86. }
  87. public function testGetHelp()
  88. {
  89. $command = new XliffLintCommand();
  90. $expected = <<<EOF
  91. The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
  92. the first encountered syntax error.
  93. You can validates XLIFF contents passed from STDIN:
  94. <info>cat filename | php %command.full_name%</info>
  95. You can also validate the syntax of a file:
  96. <info>php %command.full_name% filename</info>
  97. Or of a whole directory:
  98. <info>php %command.full_name% dirname</info>
  99. <info>php %command.full_name% dirname --format=json</info>
  100. EOF;
  101. $this->assertEquals($expected, $command->getHelp());
  102. }
  103. /**
  104. * @return string Path to the new file
  105. */
  106. private function createFile($sourceContent = 'note', $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf')
  107. {
  108. $xliffContent = <<<XLIFF
  109. <?xml version="1.0"?>
  110. <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  111. <file source-language="en" target-language="$targetLanguage" datatype="plaintext" original="file.ext">
  112. <body>
  113. <trans-unit id="note">
  114. <source>$sourceContent</source>
  115. <target>NOTE</target>
  116. </trans-unit>
  117. </body>
  118. </file>
  119. </xliff>
  120. XLIFF;
  121. $filename = sprintf('%s/translation-xliff-lint-test/%s', sys_get_temp_dir(), str_replace('%locale%', 'en', $fileNamePattern));
  122. file_put_contents($filename, $xliffContent);
  123. $this->files[] = $filename;
  124. return $filename;
  125. }
  126. /**
  127. * @return CommandTester
  128. */
  129. private function createCommandTester($requireStrictFileNames = true, $application = null)
  130. {
  131. if (!$application) {
  132. $application = new Application();
  133. $application->add(new XliffLintCommand(null, null, null, $requireStrictFileNames));
  134. }
  135. $command = $application->find('lint:xliff');
  136. if ($application) {
  137. $command->setApplication($application);
  138. }
  139. return new CommandTester($command);
  140. }
  141. protected function setUp()
  142. {
  143. $this->files = [];
  144. @mkdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  145. }
  146. protected function tearDown()
  147. {
  148. foreach ($this->files as $file) {
  149. if (file_exists($file)) {
  150. unlink($file);
  151. }
  152. }
  153. rmdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  154. }
  155. public function provideStrictFilenames()
  156. {
  157. yield [false, 'messages.%locale%.xlf', 'en', false];
  158. yield [false, 'messages.%locale%.xlf', 'es', true];
  159. yield [false, '%locale%.messages.xlf', 'en', false];
  160. yield [false, '%locale%.messages.xlf', 'es', true];
  161. yield [true, 'messages.%locale%.xlf', 'en', false];
  162. yield [true, 'messages.%locale%.xlf', 'es', true];
  163. yield [true, '%locale%.messages.xlf', 'en', true];
  164. yield [true, '%locale%.messages.xlf', 'es', true];
  165. }
  166. }