XliffLintCommandTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. array('filename' => $filename),
  30. array('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 testLintIncorrectXmlSyntax()
  36. {
  37. $tester = $this->createCommandTester();
  38. $filename = $this->createFile('note <target>');
  39. $tester->execute(array('filename' => $filename), array('decorated' => false));
  40. $this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
  41. $this->assertContains('Opening and ending tag mismatch: target line 6 and source', trim($tester->getDisplay()));
  42. }
  43. public function testLintIncorrectTargetLanguage()
  44. {
  45. $tester = $this->createCommandTester();
  46. $filename = $this->createFile('note', 'es');
  47. $tester->execute(array('filename' => $filename), array('decorated' => false));
  48. $this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
  49. $this->assertContains('There is a mismatch between the file extension ("en.xlf") and the "es" value used in the "target-language" attribute of the file.', trim($tester->getDisplay()));
  50. }
  51. /**
  52. * @expectedException \RuntimeException
  53. */
  54. public function testLintFileNotReadable()
  55. {
  56. $tester = $this->createCommandTester();
  57. $filename = $this->createFile();
  58. unlink($filename);
  59. $tester->execute(array('filename' => $filename), array('decorated' => false));
  60. }
  61. public function testGetHelp()
  62. {
  63. $command = new XliffLintCommand();
  64. $expected = <<<EOF
  65. The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
  66. the first encountered syntax error.
  67. You can validates XLIFF contents passed from STDIN:
  68. <info>cat filename | php %command.full_name%</info>
  69. You can also validate the syntax of a file:
  70. <info>php %command.full_name% filename</info>
  71. Or of a whole directory:
  72. <info>php %command.full_name% dirname</info>
  73. <info>php %command.full_name% dirname --format=json</info>
  74. EOF;
  75. $this->assertEquals($expected, $command->getHelp());
  76. }
  77. /**
  78. * @return string Path to the new file
  79. */
  80. private function createFile($sourceContent = 'note', $targetLanguage = 'en')
  81. {
  82. $xliffContent = <<<XLIFF
  83. <?xml version="1.0"?>
  84. <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  85. <file source-language="en" target-language="$targetLanguage" datatype="plaintext" original="file.ext">
  86. <body>
  87. <trans-unit id="note">
  88. <source>$sourceContent</source>
  89. <target>NOTE</target>
  90. </trans-unit>
  91. </body>
  92. </file>
  93. </xliff>
  94. XLIFF;
  95. $filename = sprintf('%s/translation-xliff-lint-test/messages.en.xlf', sys_get_temp_dir());
  96. file_put_contents($filename, $xliffContent);
  97. $this->files[] = $filename;
  98. return $filename;
  99. }
  100. /**
  101. * @return CommandTester
  102. */
  103. private function createCommandTester($application = null)
  104. {
  105. if (!$application) {
  106. $application = new Application();
  107. $application->add(new XliffLintCommand());
  108. }
  109. $command = $application->find('lint:xliff');
  110. if ($application) {
  111. $command->setApplication($application);
  112. }
  113. return new CommandTester($command);
  114. }
  115. protected function setUp()
  116. {
  117. $this->files = array();
  118. @mkdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  119. }
  120. protected function tearDown()
  121. {
  122. foreach ($this->files as $file) {
  123. if (file_exists($file)) {
  124. unlink($file);
  125. }
  126. }
  127. rmdir(sys_get_temp_dir().'/translation-xliff-lint-test');
  128. }
  129. }