Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the translation commands when a template contains a syntax error #34711

Merged
merged 1 commit into from Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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