Skip to content

Commit

Permalink
Fix the translation commands when a template contains a syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 30, 2019
1 parent fa783f9 commit 7f803bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
23 changes: 7 additions & 16 deletions src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php
Expand Up @@ -16,7 +16,6 @@
use Symfony\Bridge\Twig\Translation\TwigExtractor;
use Symfony\Component\Translation\MessageCatalogue;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Loader\ArrayLoader;

class TwigExtractorTest extends TestCase
Expand Down Expand Up @@ -78,23 +77,15 @@ public function getExtractData()
/**
* @dataProvider resourcesWithSyntaxErrorsProvider
*/
public function testExtractSyntaxError($resources)
public function testExtractSyntaxError($resources, array $messages)
{
$this->expectException('Twig\Error\Error');
$twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
$twig->addExtension(new TranslationExtension($this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock()));

$extractor = new TwigExtractor($twig);

try {
$extractor->extract($resources, new MessageCatalogue('en'));
} catch (Error $e) {
$this->assertSame(\dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', \DIRECTORY_SEPARATOR), $e->getFile());
$this->assertSame(1, $e->getLine());
$this->assertSame('Unclosed "block".', $e->getMessage());

throw $e;
}
$catalogue = new MessageCatalogue('en');
$extractor->extract($resources, $catalogue);
$this->assertSame($messages, $catalogue->all());
}

/**
Expand All @@ -103,9 +94,9 @@ public function testExtractSyntaxError($resources)
public function resourcesWithSyntaxErrorsProvider()
{
return [
[__DIR__.'/../Fixtures'],
[__DIR__.'/../Fixtures/extractor/syntax_error.twig'],
[new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig')],
[__DIR__.'/../Fixtures', ['messages' => ['Hi!' => 'Hi!']]],
[__DIR__.'/../Fixtures/extractor/syntax_error.twig', []],
[new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig'), []],
];
}

Expand Down
9 changes: 1 addition & 8 deletions src/Symfony/Bridge/Twig/Translation/TwigExtractor.php
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bridge\Twig\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
Expand Down Expand Up @@ -58,13 +57,7 @@ public function extract($resource, MessageCatalogue $catalogue)
try {
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
} catch (Error $e) {
if ($file instanceof \SplFileInfo) {
$path = $file->getRealPath() ?: $file->getPathname();
$name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
$e->setSourceContext(new Source('', $name, $path));
}

throw $e;
// ignore errors, these should be fixed by using the linter
}
}
}
Expand Down

0 comments on commit 7f803bc

Please sign in to comment.