Skip to content

Commit a9af883

Browse files
Jean-BeruwelcoMattic
authored andcommitted
rework tests
1 parent 477311f commit a9af883

17 files changed

+714
-193
lines changed

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

Lines changed: 14 additions & 193 deletions
Large diffs are not rendered by default.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\Extractor\Visitor;
4+
5+
use PhpParser\NodeVisitor;
6+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
7+
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
8+
use Symfony\Component\Translation\MessageCatalogue;
9+
10+
abstract class AbstractVisitorTest extends TestCase
11+
{
12+
abstract public function getVisitor(): NodeVisitor;
13+
abstract public function getResource(): iterable|string;
14+
abstract public function assertCatalogue(MessageCatalogue $catalogue): void;
15+
16+
public function testVisitor()
17+
{
18+
$extractor = new PhpAstExtractor([$this->getVisitor()]);
19+
$extractor->setPrefix('prefix');
20+
$catalogue = new MessageCatalogue('en');
21+
22+
$extractor->extract($this->getResource(), $catalogue);
23+
24+
$this->assertCatalogue($catalogue);
25+
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\Extractor\Visitor;
4+
5+
use PhpParser\NodeVisitor;
6+
use Symfony\Component\Translation\Extractor\Visitor\ConstraintVisitor;
7+
use Symfony\Component\Translation\MessageCatalogue;
8+
9+
class ConstraintVisitorTest extends AbstractVisitorTest
10+
{
11+
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/constraint-visitor/';
12+
13+
public function getVisitor(): NodeVisitor
14+
{
15+
return new ConstraintVisitor(['NotBlank', 'Isbn', 'Length']);
16+
}
17+
18+
public function getResource(): iterable|string
19+
{
20+
return self::FIXTURES_FOLDER;
21+
}
22+
23+
public function assertCatalogue(MessageCatalogue $catalogue): void
24+
{
25+
$this->assertEquals(
26+
[
27+
'validators' => [
28+
'message-in-constraint-attribute' => 'prefixmessage-in-constraint-attribute',
29+
// 'custom Isbn message from attribute' => 'prefixcustom Isbn message from attribute',
30+
'custom Isbn message from attribute with options as array' => 'prefixcustom Isbn message from attribute with options as array',
31+
'custom Length exact message from attribute from named argument' => 'prefixcustom Length exact message from attribute from named argument',
32+
'custom Length exact message from attribute from named argument 1/2' => 'prefixcustom Length exact message from attribute from named argument 1/2',
33+
'custom Length min message from attribute from named argument 2/2' => 'prefixcustom Length min message from attribute from named argument 2/2',
34+
// 'custom Isbn message' => 'prefixcustom Isbn message',
35+
'custom Isbn message with options as array' => 'prefixcustom Isbn message with options as array',
36+
'custom Isbn message from named argument' => 'prefixcustom Isbn message from named argument',
37+
'custom Length exact message from named argument' => 'prefixcustom Length exact message from named argument',
38+
'custom Length exact message from named argument 1/2' => 'prefixcustom Length exact message from named argument 1/2',
39+
'custom Length min message from named argument 2/2' => 'prefixcustom Length min message from named argument 2/2',
40+
],
41+
],
42+
$catalogue->all(),
43+
);
44+
45+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'validator-constraints.php:8']], $catalogue->getMetadata('message-in-constraint-attribute', 'validators'));
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\Extractor\Visitor;
4+
5+
use PhpParser\NodeVisitor;
6+
use Symfony\Component\Translation\Extractor\Visitor\FormTypeVisitor;
7+
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
8+
use Symfony\Component\Translation\MessageCatalogue;
9+
10+
class FormTypeVisitorTest extends AbstractVisitorTest
11+
{
12+
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/form-type-visitor/';
13+
14+
public function getVisitor(): FormTypeVisitor
15+
{
16+
return new FormTypeVisitor();
17+
}
18+
19+
public function getResource(): iterable|string
20+
{
21+
return self::FIXTURES_FOLDER;
22+
}
23+
24+
public function assertCatalogue(MessageCatalogue $catalogue): void
25+
{
26+
$this->assertEquals(
27+
[
28+
'messages' => [
29+
'label.foo1' => 'prefixlabel.foo1',
30+
'label.find1' => 'prefixlabel.find1',
31+
'find2' => 'prefixfind2',
32+
'FOUND3' => 'prefixFOUND3',
33+
'label.find4' => 'prefixlabel.find4',
34+
'label.find5' => 'prefixlabel.find5',
35+
'find6' => 'prefixfind6',
36+
'bigger_find7' => 'prefixbigger_find7',
37+
'camelFind8' => 'prefixcamelFind8',
38+
'label.find9' => 'prefixlabel.find9',
39+
],
40+
],
41+
$catalogue->all(),
42+
);
43+
44+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'form-type.php:27']], $catalogue->getMetadata('label.find1'));
45+
}
46+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\Extractor\Visitor;
4+
5+
use PhpParser\NodeVisitor;
6+
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
7+
use Symfony\Component\Translation\MessageCatalogue;
8+
9+
class TransMethodVisitorTest extends AbstractVisitorTest
10+
{
11+
private const FIXTURES_FOLDER = __DIR__ . '/../../Fixtures/extractor-php-ast/trans-method-visitor/';
12+
public const OTHER_DOMAIN = 'not_messages';
13+
14+
public function getVisitor(): NodeVisitor
15+
{
16+
return new TransMethodVisitor();
17+
}
18+
19+
public function getResource(): iterable|string
20+
{
21+
return self::FIXTURES_FOLDER;
22+
}
23+
24+
public function assertCatalogue(MessageCatalogue $catalogue): void
25+
{
26+
$expectedHeredoc = <<<EOF
27+
heredoc key with whitespace and escaped \$\n sequences
28+
EOF;
29+
$expectedNowdoc = <<<'EOF'
30+
nowdoc key with whitespace and nonescaped \$\n sequences
31+
EOF;
32+
33+
$this->assertEquals(
34+
[
35+
'messages' => [
36+
'single-quoted key' => 'prefixsingle-quoted key',
37+
'double-quoted key' => 'prefixdouble-quoted key',
38+
'heredoc key' => 'prefixheredoc key',
39+
'nowdoc key' => 'prefixnowdoc key',
40+
"double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
41+
'single-quoted key with whitespace and nonescaped \\$\\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \\$\\n\' sequences',
42+
$expectedHeredoc => 'prefix'.$expectedHeredoc,
43+
$expectedNowdoc => 'prefix'.$expectedNowdoc,
44+
'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
45+
'concatenated message with heredoc and nowdoc' => 'prefixconcatenated message with heredoc and nowdoc',
46+
'default domain' => 'prefixdefault domain',
47+
'mix-named-arguments' => 'prefixmix-named-arguments',
48+
'mix-named-arguments-locale' => 'prefixmix-named-arguments-locale',
49+
'mix-named-arguments-without-domain' => 'prefixmix-named-arguments-without-domain',
50+
"heredoc\nindented\n further" => "prefixheredoc\nindented\n further",
51+
"nowdoc\nindented\n further" => "prefixnowdoc\nindented\n further",
52+
'translatable-short single-quoted key' => 'prefixtranslatable-short single-quoted key',
53+
'translatable-short double-quoted key' => 'prefixtranslatable-short double-quoted key',
54+
'translatable-short heredoc key' => 'prefixtranslatable-short heredoc key',
55+
'translatable-short nowdoc key' => 'prefixtranslatable-short nowdoc key',
56+
"translatable-short double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixtranslatable-short double-quoted key with whitespace and escaped \$\n\" sequences",
57+
'translatable-short single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixtranslatable-short single-quoted key with whitespace and nonescaped \$\n\' sequences',
58+
'translatable-short single-quoted key with "quote mark at the end"' => 'prefixtranslatable-short single-quoted key with "quote mark at the end"',
59+
'translatable-short '.$expectedHeredoc => 'prefixtranslatable-short '.$expectedHeredoc,
60+
'translatable-short '.$expectedNowdoc => 'prefixtranslatable-short '.$expectedNowdoc,
61+
'translatable-short concatenated message with heredoc and nowdoc' => 'prefixtranslatable-short concatenated message with heredoc and nowdoc',
62+
'translatable-short default domain' => 'prefixtranslatable-short default domain',
63+
'translatable-short-fqn single-quoted key' => 'prefixtranslatable-short-fqn single-quoted key',
64+
'translatable-short-fqn double-quoted key' => 'prefixtranslatable-short-fqn double-quoted key',
65+
'translatable-short-fqn heredoc key' => 'prefixtranslatable-short-fqn heredoc key',
66+
'translatable-short-fqn nowdoc key' => 'prefixtranslatable-short-fqn nowdoc key',
67+
"translatable-short-fqn double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixtranslatable-short-fqn double-quoted key with whitespace and escaped \$\n\" sequences",
68+
'translatable-short-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixtranslatable-short-fqn single-quoted key with whitespace and nonescaped \$\n\' sequences',
69+
'translatable-short-fqn single-quoted key with "quote mark at the end"' => 'prefixtranslatable-short-fqn single-quoted key with "quote mark at the end"',
70+
'translatable-short-fqn '.$expectedHeredoc => 'prefixtranslatable-short-fqn '.$expectedHeredoc,
71+
'translatable-short-fqn '.$expectedNowdoc => 'prefixtranslatable-short-fqn '.$expectedNowdoc,
72+
'translatable-short-fqn concatenated message with heredoc and nowdoc' => 'prefixtranslatable-short-fqn concatenated message with heredoc and nowdoc',
73+
'translatable-short-fqn default domain' => 'prefixtranslatable-short-fqn default domain',
74+
],
75+
'not_messages' => [
76+
'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
77+
'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
78+
'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
79+
'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
80+
'typecast' => 'prefixtypecast',
81+
'ordered-named-arguments-in-trans-method' => 'prefixordered-named-arguments-in-trans-method',
82+
'disordered-named-arguments-in-trans-method' => 'prefixdisordered-named-arguments-in-trans-method',
83+
'variable-assignation-inlined-in-trans-method-call1' => 'prefixvariable-assignation-inlined-in-trans-method-call1',
84+
'variable-assignation-inlined-in-trans-method-call2' => 'prefixvariable-assignation-inlined-in-trans-method-call2',
85+
'variable-assignation-inlined-in-trans-method-call3' => 'prefixvariable-assignation-inlined-in-trans-method-call3',
86+
'variable-assignation-inlined-with-named-arguments-in-trans-method' => 'prefixvariable-assignation-inlined-with-named-arguments-in-trans-method',
87+
'mix-named-arguments-without-parameters' => 'prefixmix-named-arguments-without-parameters',
88+
'mix-named-arguments-disordered' => 'prefixmix-named-arguments-disordered',
89+
'const-domain' => 'prefixconst-domain',
90+
'translatable-short other-domain-test-no-params-short-array' => 'prefixtranslatable-short other-domain-test-no-params-short-array',
91+
'translatable-short other-domain-test-no-params-long-array' => 'prefixtranslatable-short other-domain-test-no-params-long-array',
92+
'translatable-short other-domain-test-params-short-array' => 'prefixtranslatable-short other-domain-test-params-short-array',
93+
'translatable-short other-domain-test-params-long-array' => 'prefixtranslatable-short other-domain-test-params-long-array',
94+
'translatable-short typecast' => 'prefixtranslatable-short typecast',
95+
'translatable-short-fqn other-domain-test-no-params-short-array' => 'prefixtranslatable-short-fqn other-domain-test-no-params-short-array',
96+
'translatable-short-fqn other-domain-test-no-params-long-array' => 'prefixtranslatable-short-fqn other-domain-test-no-params-long-array',
97+
'translatable-short-fqn other-domain-test-params-short-array' => 'prefixtranslatable-short-fqn other-domain-test-params-short-array',
98+
'translatable-short-fqn other-domain-test-params-long-array' => 'prefixtranslatable-short-fqn other-domain-test-params-long-array',
99+
'translatable-short-fqn typecast' => 'prefixtranslatable-short-fqn typecast',
100+
],
101+
],
102+
$catalogue->all(),
103+
);
104+
105+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translation.html.php:2']], $catalogue->getMetadata('single-quoted key'));
106+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translation.html.php:37']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
107+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translation-73.html.php:8']], $catalogue->getMetadata("nowdoc\nindented\n further"));
108+
109+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short.html.php:2']], $catalogue->getMetadata('translatable-short single-quoted key'));
110+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short.html.php:37']], $catalogue->getMetadata('translatable-short other-domain-test-no-params-short-array', 'not_messages'));
111+
112+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short-fqn.html.php:2']], $catalogue->getMetadata('translatable-short-fqn single-quoted key'));
113+
$this->assertEquals(['sources' => [self::FIXTURES_FOLDER . 'translatable-short-fqn.html.php:37']], $catalogue->getMetadata('translatable-short-fqn other-domain-test-no-params-short-array', 'not_messages'));
114+
}
115+
}

0 commit comments

Comments
 (0)