Skip to content

Commit 7bb3117

Browse files
cjean-wishibamNyholm
authored andcommitted
add more tests (#117)
1 parent a31f38c commit 7bb3117

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/Visitor/Php/Symfony/FormTypeChoices.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

14+
use Doctrine\Common\Annotations\DocParser;
1415
use PhpParser\Node;
1516
use PhpParser\NodeVisitor;
17+
use Translation\Extractor\Annotation\Ignore;
1618
use Translation\Extractor\Model\SourceLocation;
1719

1820
/**
@@ -96,6 +98,11 @@ public function enterNode(Node $node)
9698
continue;
9799
}
98100

101+
//do not parse choices if the @Ignore annotation is attached
102+
if ($this->isIgnored($item->key)) {
103+
continue;
104+
}
105+
99106
$choicesNodes[] = $item->value;
100107
}
101108

@@ -125,4 +132,28 @@ public function enterNode(Node $node)
125132
}
126133
}
127134
}
135+
136+
/**
137+
* @param Node $node
138+
*
139+
* @return bool
140+
*/
141+
protected function isIgnored(Node $node)
142+
{
143+
//because of getDocParser method is private, we have to create a new custom instance
144+
$docParser = new DocParser();
145+
$docParser->setImports([
146+
'ignore' => Ignore::class,
147+
]);
148+
$docParser->setIgnoreNotImportedAnnotations(true);
149+
if (null !== $docComment = $node->getDocComment()) {
150+
foreach ($docParser->parse($docComment->getText()) as $annotation) {
151+
if ($annotation instanceof Ignore) {
152+
return true;
153+
}
154+
}
155+
}
156+
157+
return false;
158+
}
128159
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Translation\Extractor\Annotation\Ignore;
6+
7+
class Issue111Type
8+
{
9+
public function buildForm(FormBuilderInterface $builder, array $options)
10+
{
11+
12+
$builder
13+
->add('field_1', null, array(
14+
/** @Ignore */
15+
'choices' => array(
16+
'github.issue_111.c' => 'c'
17+
)
18+
));
19+
20+
$builder->add('field_2', null, array(
21+
'choices' => array(
22+
'github.issue_111.d' => 'd'
23+
)
24+
));
25+
}
26+
27+
public function configureOptions(OptionsResolver $resolver)
28+
{
29+
$resolver->setDefaults(array(
30+
/** @Ignore */
31+
'choices' => function (Options $options) {
32+
return array(
33+
'github.issue_111.a' => 'a',
34+
'github.issue_111.b' => 'b'
35+
);
36+
},
37+
'foo_bar' => true
38+
));
39+
}
40+
}

tests/Smoke/AllExtractorsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function testNoException()
7676
$this->translationMissing($sc, 'github.issue_109.c');
7777
$this->translationExists($sc, 'github.issue_109.d');
7878

79+
$this->translationMissing($sc, 'github.issue_111.a');
80+
$this->translationMissing($sc, 'github.issue_111.b');
81+
$this->translationMissing($sc, 'github.issue_111.c');
82+
$this->translationExists($sc, 'github.issue_111.d');
83+
7984
/*
8085
* It is okey to increase the error count if you adding more fixtures/code.
8186
* We just need to be aware that it changes.

0 commit comments

Comments
 (0)