Skip to content

Commit

Permalink
add more tests (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjean-wishibam authored and Nyholm committed Jul 8, 2018
1 parent a31f38c commit 7bb3117
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Visitor/Php/Symfony/FormTypeChoices.php
Expand Up @@ -11,8 +11,10 @@

namespace Translation\Extractor\Visitor\Php\Symfony;

use Doctrine\Common\Annotations\DocParser;
use PhpParser\Node;
use PhpParser\NodeVisitor;
use Translation\Extractor\Annotation\Ignore;
use Translation\Extractor\Model\SourceLocation;

/**
Expand Down Expand Up @@ -96,6 +98,11 @@ public function enterNode(Node $node)
continue;
}

//do not parse choices if the @Ignore annotation is attached
if ($this->isIgnored($item->key)) {
continue;
}

$choicesNodes[] = $item->value;
}

Expand Down Expand Up @@ -125,4 +132,28 @@ public function enterNode(Node $node)
}
}
}

/**
* @param Node $node
*
* @return bool
*/
protected function isIgnored(Node $node)
{
//because of getDocParser method is private, we have to create a new custom instance
$docParser = new DocParser();
$docParser->setImports([
'ignore' => Ignore::class,
]);
$docParser->setIgnoreNotImportedAnnotations(true);
if (null !== $docComment = $node->getDocComment()) {
foreach ($docParser->parse($docComment->getText()) as $annotation) {
if ($annotation instanceof Ignore) {
return true;
}
}
}

return false;
}
}
40 changes: 40 additions & 0 deletions tests/Resources/Github/Issue_111.php
@@ -0,0 +1,40 @@
<?php

namespace Translation\Extractor\Tests\Resources\Php\Symfony;

use Translation\Extractor\Annotation\Ignore;

class Issue111Type
{
public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder
->add('field_1', null, array(
/** @Ignore */
'choices' => array(
'github.issue_111.c' => 'c'
)
));

$builder->add('field_2', null, array(
'choices' => array(
'github.issue_111.d' => 'd'
)
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
/** @Ignore */
'choices' => function (Options $options) {
return array(
'github.issue_111.a' => 'a',
'github.issue_111.b' => 'b'
);
},
'foo_bar' => true
));
}
}
5 changes: 5 additions & 0 deletions tests/Smoke/AllExtractorsTest.php
Expand Up @@ -76,6 +76,11 @@ public function testNoException()
$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');

/*
* It is okey to increase the error count if you adding more fixtures/code.
* We just need to be aware that it changes.
Expand Down

0 comments on commit 7bb3117

Please sign in to comment.