Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ protected function addMessageToCatalogue(string $message, ?string $domain, int $
$domain ??= 'messages';
$this->catalogue->set($message, $this->messagePrefix.$message, $domain);
$metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
$metadata['sources'][] = $this->file->getPathname().':'.$line;
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
$metadata['sources'][] = $normalizedFilename.':'.$line;
$this->catalogue->setMetadata($message, $metadata, $domain);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testExtractFiles(iterable|string $resource)
$extractor->extract($resource, $catalogue);

$this->assertEquals(['messages' => ['example' => 'example']], $catalogue->all());
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER.'translation.html.php:1']], $catalogue->getMetadata('example'));
$this->assertEquals(['sources' => [preg_replace('{[\\\\/]+}', '/', self::FIXTURES_FOLDER).'translation.html.php:1']], $catalogue->getMetadata('example'));
}

public static function resourcesProvider(): \Generator
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Component\Translation\Tests\Extractor\Visitor;

use PhpParser\NodeVisitor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
use Symfony\Component\Translation\MessageCatalogue;

abstract class AbstractVisitorTestCase extends TestCase
{
/**
* @param string|iterable<string> $resource Files, a file or a directory
*/
public function extract(NodeVisitor $visitor, string|iterable $resource): MessageCatalogue
{
$extractor = new PhpAstExtractor([$visitor]);
$extractor->setPrefix('prefix');

$catalogue = new MessageCatalogue('en');

$extractor->extract($resource, $catalogue);

return $catalogue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

namespace Symfony\Component\Translation\Tests\Extractor\Visitor;

use PhpParser\NodeVisitor;
use Symfony\Component\Translation\Extractor\Visitor\ConstraintVisitor;
use Symfony\Component\Translation\MessageCatalogue;

class ConstraintVisitorTest extends AbstractVisitorTest
final class ConstraintVisitorTest extends AbstractVisitorTestCase
{
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/constraint-visitor/';

public function getVisitor(): NodeVisitor
public function testExtractMessages()
{
return new ConstraintVisitor(['NotBlank', 'Isbn', 'Length']);
}
$catalogue = $this->extract(new ConstraintVisitor(['NotBlank', 'Isbn', 'Length']), self::FIXTURES_FOLDER);

public function getResource(): iterable|string
{
return self::FIXTURES_FOLDER;
}

public function assertCatalogue(MessageCatalogue $catalogue): void
{
$this->assertEquals(
[
'validators' => [
Expand All @@ -42,6 +32,6 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
$catalogue->all(),
);

$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'validator-constraints.php:8']], $catalogue->getMetadata('message-in-constraint-attribute', 'validators'));
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'validator-constraints.php:7']], $catalogue->getMetadata('message-in-constraint-attribute', 'validators'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@

namespace Symfony\Component\Translation\Tests\Extractor\Visitor;

use PhpParser\NodeVisitor;
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
use Symfony\Component\Translation\MessageCatalogue;

class TransMethodVisitorTest extends AbstractVisitorTest
final class TransMethodVisitorTest extends AbstractVisitorTestCase
{
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/trans-method-visitor/';
public const OTHER_DOMAIN = 'not_messages';

public function getVisitor(): NodeVisitor
public function testExtractMessages()
{
return new TransMethodVisitor();
}
$catalogue = $this->extract(new TransMethodVisitor(), self::FIXTURES_FOLDER);

public function getResource(): iterable|string
{
return self::FIXTURES_FOLDER;
}

public function assertCatalogue(MessageCatalogue $catalogue): void
{
$expectedHeredoc = <<<EOF
heredoc key with whitespace and escaped \$\n sequences
EOF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@

namespace Symfony\Component\Translation\Tests\Extractor\Visitor;

use PhpParser\NodeVisitor;
use Symfony\Component\Translation\Extractor\Visitor\TranslatableMessageVisitor;
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
use Symfony\Component\Translation\MessageCatalogue;

class TranslatableMessageVisitorTest extends AbstractVisitorTest
final class TranslatableMessageVisitorTest extends AbstractVisitorTestCase
{
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/translatable-message-visitor/';

public function getVisitor(): NodeVisitor
public function testExtractMessages()
{
return new TranslatableMessageVisitor();
}
$catalogue = $this->extract(new TranslatableMessageVisitor(), self::FIXTURES_FOLDER);

public function getResource(): iterable|string
{
return self::FIXTURES_FOLDER;
}

public function assertCatalogue(MessageCatalogue $catalogue): void
{
$expectedHeredoc = <<<EOF
heredoc key with whitespace and escaped \$\n sequences
EOF;
Expand Down Expand Up @@ -72,8 +61,8 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
$catalogue->all(),
);

$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable.html.php:2']], $catalogue->getMetadata('translatable single-quoted key'));
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable.html.php:37']], $catalogue->getMetadata('translatable other-domain-test-no-params-short-array', 'not_messages'));
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable.html.php:3']], $catalogue->getMetadata('translatable single-quoted key'));
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable.html.php:38']], $catalogue->getMetadata('translatable other-domain-test-no-params-short-array', 'not_messages'));

$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-fqn.html.php:2']], $catalogue->getMetadata('translatable-fqn single-quoted key'));
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-fqn.html.php:37']], $catalogue->getMetadata('translatable-fqn other-domain-test-no-params-short-array', 'not_messages'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
This template is used for translation message extraction tests
<?php

use Symfony\Component\Validator\Constraints as Assert;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
This template is used for translation message extraction tests
<?php
// @see https://github.com/php-translation/extractor/blob/master/tests/Resources/Php/Symfony/ExplicitLabelType.php

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
This template is used for translation message extraction tests
<?php use Symfony\Component\Translation\TranslatableMessage; ?>
<?php new TranslatableMessage('translatable single-quoted key'); ?>
<?php new TranslatableMessage('translatable double-quoted key'); ?>
<?php new TranslatableMessage(<<<EOF
Expand Down
Loading