Skip to content

Commit 4cc3e57

Browse files
authored
Added more test, test resources and bugfixes (#24)
* Added more test, test resources and bugfixes * cs fix
1 parent f79648e commit 4cc3e57

File tree

6 files changed

+100
-35
lines changed

6 files changed

+100
-35
lines changed

src/Visitor/Php/Symfony/FormTypeChoices.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,53 @@ public function enterNode(Node $node)
4646
}
4747

4848
// symfony 3 displays key by default, where symfony 2 displays value
49-
$useKey = $this->symfonyMajorVersion == 3;
49+
$useKey = $this->symfonyMajorVersion === 3;
5050

5151
// remember choices in this node
5252
$choicesNodes = [];
5353

5454
// loop through array
55-
if ($node instanceof Node\Expr\Array_) {
56-
foreach ($node->items as $item) {
57-
if (!$item->key instanceof Node\Scalar\String_) {
58-
continue;
59-
}
55+
if (!$node instanceof Node\Expr\Array_) {
56+
return;
57+
}
6058

61-
if ($item->key->value === 'choices_as_values') {
62-
$useKey = true;
63-
continue;
64-
}
59+
foreach ($node->items as $item) {
60+
if (!$item->key instanceof Node\Scalar\String_) {
61+
continue;
62+
}
6563

66-
if ($item->key->value !== 'choices') {
67-
continue;
68-
}
64+
if ($item->key->value === 'choices_as_values') {
65+
$useKey = true;
66+
continue;
67+
}
6968

70-
if (!$item->value instanceof Node\Expr\Array_) {
71-
continue;
72-
}
69+
if ($item->key->value !== 'choices') {
70+
continue;
71+
}
7372

74-
$choicesNodes[] = $item->value;
73+
if (!$item->value instanceof Node\Expr\Array_) {
74+
continue;
7575
}
7676

77-
if (count($choicesNodes) > 0) {
78-
// probably will be only 1, but who knows
79-
foreach ($choicesNodes as $choices) {
80-
// TODO: do something with grouped (multi-dimensional) arrays here
81-
if (!$choices instanceof Node\Expr\Array_) {
82-
continue;
83-
}
77+
$choicesNodes[] = $item->value;
78+
}
8479

85-
foreach ($choices as $citem) {
86-
$labelNode = $useKey ? $citem[0]->key : $citem[0]->value;
87-
if (!$labelNode instanceof Node\Scalar\String_) {
88-
continue;
89-
}
80+
if (count($choicesNodes) > 0) {
81+
// probably will be only 1, but who knows
82+
foreach ($choicesNodes as $choices) {
83+
// TODO: do something with grouped (multi-dimensional) arrays here
84+
if (!$choices instanceof Node\Expr\Array_) {
85+
continue;
86+
}
9087

91-
$sl = new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'));
92-
$this->collection->addLocation($sl);
88+
foreach ($choices->items as $citem) {
89+
$labelNode = $useKey ? $citem->key : $citem->value;
90+
if (!$labelNode instanceof Node\Scalar\String_) {
91+
continue;
9392
}
93+
94+
$sl = new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'));
95+
$this->collection->addLocation($sl);
9496
}
9597
}
9698
}

tests/Functional/Visitor/Php/BasePHPVisitorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
*/
2121
abstract class BasePHPVisitorTest extends \PHPUnit_Framework_TestCase
2222
{
23+
/**
24+
* @param $visitor
25+
* @param $namespaceForTestFile
26+
*
27+
* @return SourceCollection
28+
*
29+
* @throws \Exception
30+
*/
2331
protected function getSourceLocations($visitor, $namespaceForTestFile)
2432
{
2533
$extractor = new PHPFileExtractor();

tests/Functional/Visitor/Php/Symfony/ContainerAwareTransTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ public function testExtract()
2424
{
2525
$collection = $this->getSourceLocations(new ContainerAwareTrans(), Resources\Php\Symfony\ContainerAwareTrans::class);
2626

27-
$this->assertCount(1, $collection);
28-
$source = $collection->first();
29-
$this->assertEquals('foobar', $source->getMessage());
27+
$this->assertCount(4, $collection);
28+
29+
$this->assertEquals('trans0', $collection->get(0)->getMessage());
30+
$this->assertEquals('trans1', $collection->get(1)->getMessage());
31+
$this->assertEquals('trans_line', $collection->get(2)->getMessage());
32+
$this->assertEquals('variable', $collection->get(3)->getMessage());
3033
}
3134
}

tests/Functional/Visitor/Php/Symfony/FormTypeChoicesTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ public function testSimpleSymfony27()
4242
$this->assertEquals('label4', $collection->get(3)->getMessage());
4343
$this->assertEquals(12, $collection->get(0)->getLine());
4444
}
45+
46+
public function testChainedChoice()
47+
{
48+
$visitor = new FormTypeChoices();
49+
$visitor->setSymfonyMajorVersion(3);
50+
$collection = $this->getSourceLocations($visitor, Resources\Php\Symfony\ChainedChoiceType::class);
51+
52+
$this->assertCount(2, $collection, print_r($collection, true));
53+
$this->assertEquals('label1', $collection->get(0)->getMessage());
54+
$this->assertEquals('label2', $collection->get(1)->getMessage());
55+
}
4556
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
class ChainedChoiceType
6+
{
7+
public function buildForm(FormBuilderInterface $builder, array $options)
8+
{
9+
$builder
10+
->add('name')
11+
->add('test', null, [
12+
'choices' => [
13+
'label1' => 'key',
14+
'label2' => 'key',
15+
],
16+
]);
17+
}
18+
}

tests/Resources/Php/Symfony/ContainerAwareTrans.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,31 @@ class ContainerAwareTrans
99
*/
1010
public function newAction()
1111
{
12-
$translated = $this->get('translator')->trans('foobar');
12+
// Using container
13+
$translated = $this->get('translator')->trans('trans0');
14+
15+
// As a parameter
16+
$model->setMessage(
17+
$this->get('translator')->trans(
18+
'trans1',
19+
array(
20+
'a' => 'x',
21+
'b' => 'y',
22+
'c' => 'z',
23+
)
24+
)
25+
);
1326

1427
return array();
1528
}
29+
30+
public function getLine()
31+
{
32+
return $this->translator->trans('trans_line');
33+
}
34+
public function getTranslatorVariable()
35+
{
36+
$translator = $this->get('translator');
37+
$translator->trans('variable');
38+
}
1639
}

0 commit comments

Comments
 (0)