forked from php-translation/extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllExtractorsTest.php
173 lines (147 loc) · 6.65 KB
/
AllExtractorsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Translation\Extractor\Tests\Smoke;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
use Symfony\Component\Finder\Finder;
use Translation\Extractor\Extractor;
use Translation\Extractor\FileExtractor\PHPFileExtractor;
use Translation\Extractor\FileExtractor\TwigFileExtractor;
use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Model\SourceLocation;
use Translation\Extractor\Tests\Functional\Visitor\Twig\TwigEnvironmentFactory;
use Translation\Extractor\Visitor\Php\Knp\Menu\ItemLabel;
use Translation\Extractor\Visitor\Php\Knp\Menu\LinkTitle;
use Translation\Extractor\Visitor\Php\SourceLocationContainerVisitor;
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTrans;
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTransChoice;
use Translation\Extractor\Visitor\Php\Symfony\FlashMessage;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeChoices;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeEmptyValue;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeInvalidMessage;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
use Translation\Extractor\Visitor\Php\Symfony\FormTypeTitle;
use Translation\Extractor\Visitor\Php\TranslateAnnotationVisitor;
use Translation\Extractor\Visitor\Twig\TwigVisitor;
/**
* Smoke test to make sure no extractor throws exceptions.
*
* @author Tobias Nyholm <[email protected]>
*/
class AllExtractorsTest extends TestCase
{
public function testNoException()
{
$extractor = new Extractor();
$extractor->addFileExtractor($this->getPHPFileExtractor());
$extractor->addFileExtractor($this->getTwigFileExtractor());
$finder = new Finder();
$finder->in(\dirname(__DIR__));
if (!class_exists(TransChoiceTokenParser::class)) {
$finder->notName('transchoice.html.twig');
}
$sc = $extractor->extract($finder);
$this->translationExists($sc, 'trans.issue_34');
$this->translationMissing($sc, 'trans.issue_62');
$this->translationMissing($sc, 'github.issue_78a');
$this->translationMissing($sc, 'github.issue_78b');
$source = $this->translationExists($sc, 'github.issue_82a');
$this->assertEquals('custom', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_82b');
$this->assertEquals('foobar', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_82c');
$this->assertEquals('custom', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96a.placeholder');
$this->assertEquals('custom', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96b.placeholder');
$this->assertEquals('foobar', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96c.placeholder');
$this->assertEquals('foobar', $source->getContext()['domain']);
$source = $this->translationExists($sc, 'github.issue_96d.placeholder');
$this->assertEquals('custom', $source->getContext()['domain']);
$this->translationExists($sc, 'github.issue_109.a');
$this->translationMissing($sc, 'github.issue_109.b');
$this->translationMissing($sc, 'github.issue_109.c');
$this->translationExists($sc, 'github.issue_109.d');
$this->translationMissing($sc, 'github.issue_111.a');
$this->translationMissing($sc, 'github.issue_111.b');
$this->translationMissing($sc, 'github.issue_111.c');
$this->translationExists($sc, 'github.issue_111.d');
$this->translationExists($sc, 'github.issue_125.a');
/*
* It is okey to increase the error count if you adding more fixtures/code.
* We just need to be aware that it changes.
*/
$this->assertCount(13, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
}
/**
* Assert that a translation key exists in source collection.
*/
private function translationExists(SourceCollection $sc, string $translationKey, ?string $message = null): SourceLocation
{
if (empty($message)) {
$message = sprintf('Tried to find "%s" but failed', $translationKey);
}
$source = null;
$found = false;
foreach ($sc as $source) {
if ($translationKey === $source->getMessage()) {
$found = true;
break;
}
}
$this->assertTrue($found, $message);
return $source;
}
/**
* Assert that a translation key is missing in source collection.
*/
private function translationMissing(SourceCollection $sc, string $translationKey, ?string $message = null): void
{
if (empty($message)) {
$message = sprintf('The translation key "%s" should not exist', $translationKey);
}
$found = false;
foreach ($sc as $source) {
if ($translationKey === $source->getMessage()) {
$found = true;
break;
}
}
$this->assertFalse($found, $message);
}
private function getPHPFileExtractor(): PHPFileExtractor
{
$file = new PHPFileExtractor();
$file->addVisitor(new ContainerAwareTrans());
$file->addVisitor(new ContainerAwareTransChoice());
$file->addVisitor(new FlashMessage());
$file->addVisitor(new FormTypeChoices());
$file->addVisitor(new FormTypeEmptyValue());
$file->addVisitor(new FormTypeInvalidMessage());
$file->addVisitor(new FormTypeLabelExplicit());
$file->addVisitor(new FormTypeLabelImplicit());
$file->addVisitor(new FormTypePlaceholder());
$file->addVisitor(new FormTypeTitle());
$file->addVisitor(new SourceLocationContainerVisitor());
$file->addVisitor(new TranslateAnnotationVisitor());
$file->addVisitor(new ItemLabel());
$file->addVisitor(new LinkTitle());
return $file;
}
private function getTwigFileExtractor(): TwigFileExtractor
{
$file = new TwigFileExtractor(TwigEnvironmentFactory::create());
$file->addVisitor(new TwigVisitor());
return $file;
}
}