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