Skip to content

Commit baad742

Browse files
authored
Merge pull request #7 from Jean-Beru/translation/php-ast-extractor-tests-rework-review-0001
[Translation] Rework PhpAstExtractor tests organization for future improvements - REVIEW0001
2 parents 23ead95 + 1dd419e commit baad742

File tree

10 files changed

+42
-74
lines changed

10 files changed

+42
-74
lines changed

src/Symfony/Component/Translation/Extractor/Visitor/AbstractVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ protected function addMessageToCatalogue(string $message, ?string $domain, int $
3535
$domain ??= 'messages';
3636
$this->catalogue->set($message, $this->messagePrefix.$message, $domain);
3737
$metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
38-
$metadata['sources'][] = $this->file->getPathname().':'.$line;
38+
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
39+
$metadata['sources'][] = $normalizedFilename.':'.$line;
3940
$this->catalogue->setMetadata($message, $metadata, $domain);
4041
}
4142

src/Symfony/Component/Translation/Tests/Extractor/PhpAstExtractorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testExtractFiles(iterable|string $resource)
3131
$extractor->extract($resource, $catalogue);
3232

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

3737
public static function resourcesProvider(): \Generator

src/Symfony/Component/Translation/Tests/Extractor/Visitor/AbstractVisitorTest.php

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\Extractor\Visitor;
4+
5+
use PhpParser\NodeVisitor;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
8+
use Symfony\Component\Translation\MessageCatalogue;
9+
10+
abstract class AbstractVisitorTestCase extends TestCase
11+
{
12+
/**
13+
* @param string|iterable<string> $resource Files, a file or a directory
14+
*/
15+
public function extract(NodeVisitor $visitor, string|iterable $resource): MessageCatalogue
16+
{
17+
$extractor = new PhpAstExtractor([$visitor]);
18+
$extractor->setPrefix('prefix');
19+
20+
$catalogue = new MessageCatalogue('en');
21+
22+
$extractor->extract($resource, $catalogue);
23+
24+
return $catalogue;
25+
}
26+
}

src/Symfony/Component/Translation/Tests/Extractor/Visitor/ConstraintVisitorTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,16 @@
22

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

5-
use PhpParser\NodeVisitor;
65
use Symfony\Component\Translation\Extractor\Visitor\ConstraintVisitor;
7-
use Symfony\Component\Translation\MessageCatalogue;
86

9-
class ConstraintVisitorTest extends AbstractVisitorTest
7+
final class ConstraintVisitorTest extends AbstractVisitorTestCase
108
{
119
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/constraint-visitor/';
1210

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

18-
public function getResource(): iterable|string
19-
{
20-
return self::FIXTURES_FOLDER;
21-
}
22-
23-
public function assertCatalogue(MessageCatalogue $catalogue): void
24-
{
2515
$this->assertEquals(
2616
[
2717
'validators' => [
@@ -42,6 +32,6 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
4232
$catalogue->all(),
4333
);
4434

45-
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'validator-constraints.php:8']], $catalogue->getMetadata('message-in-constraint-attribute', 'validators'));
35+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'validator-constraints.php:7']], $catalogue->getMetadata('message-in-constraint-attribute', 'validators'));
4636
}
4737
}

src/Symfony/Component/Translation/Tests/Extractor/Visitor/TransMethodVisitorTest.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@
22

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

5-
use PhpParser\NodeVisitor;
65
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
7-
use Symfony\Component\Translation\MessageCatalogue;
86

9-
class TransMethodVisitorTest extends AbstractVisitorTest
7+
final class TransMethodVisitorTest extends AbstractVisitorTestCase
108
{
119
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/trans-method-visitor/';
1210
public const OTHER_DOMAIN = 'not_messages';
1311

14-
public function getVisitor(): NodeVisitor
12+
public function testExtractMessages()
1513
{
16-
return new TransMethodVisitor();
17-
}
14+
$catalogue = $this->extract(new TransMethodVisitor(), self::FIXTURES_FOLDER);
1815

19-
public function getResource(): iterable|string
20-
{
21-
return self::FIXTURES_FOLDER;
22-
}
23-
24-
public function assertCatalogue(MessageCatalogue $catalogue): void
25-
{
2616
$expectedHeredoc = <<<EOF
2717
heredoc key with whitespace and escaped \$\n sequences
2818
EOF;

src/Symfony/Component/Translation/Tests/Extractor/Visitor/TranslatableMessageVisitorTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,16 @@
22

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

5-
use PhpParser\NodeVisitor;
65
use Symfony\Component\Translation\Extractor\Visitor\TranslatableMessageVisitor;
7-
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
8-
use Symfony\Component\Translation\MessageCatalogue;
96

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

14-
public function getVisitor(): NodeVisitor
11+
public function testExtractMessages()
1512
{
16-
return new TranslatableMessageVisitor();
17-
}
13+
$catalogue = $this->extract(new TranslatableMessageVisitor(), self::FIXTURES_FOLDER);
1814

19-
public function getResource(): iterable|string
20-
{
21-
return self::FIXTURES_FOLDER;
22-
}
23-
24-
public function assertCatalogue(MessageCatalogue $catalogue): void
25-
{
2615
$expectedHeredoc = <<<EOF
2716
heredoc key with whitespace and escaped \$\n sequences
2817
EOF;
@@ -72,8 +61,8 @@ public function assertCatalogue(MessageCatalogue $catalogue): void
7261
$catalogue->all(),
7362
);
7463

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

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

src/Symfony/Component/Translation/Tests/Fixtures/extractor-php-ast/constraint-visitor/validator-constraints.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
This template is used for translation message extraction tests
21
<?php
32

43
use Symfony\Component\Validator\Constraints as Assert;

src/Symfony/Component/Translation/Tests/Fixtures/extractor-php-ast/form-type-visitor/form-type.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
This template is used for translation message extraction tests
21
<?php
32
// @see https://github.com/php-translation/extractor/blob/master/tests/Resources/Php/Symfony/ExplicitLabelType.php
43

src/Symfony/Component/Translation/Tests/Fixtures/extractor-php-ast/translatable-message-visitor/translatable.html.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
This template is used for translation message extraction tests
2+
<?php use Symfony\Component\Translation\TranslatableMessage; ?>
23
<?php new TranslatableMessage('translatable single-quoted key'); ?>
34
<?php new TranslatableMessage('translatable double-quoted key'); ?>
45
<?php new TranslatableMessage(<<<EOF

0 commit comments

Comments
 (0)