Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes translation_domain detection with setDefault #453

Merged
merged 1 commit into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Tests\Translation\Extractor\File\Fixture;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MyFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('firstname', 'text', array(
'label' => 'form.label.firstname',
))
->add('lastname', 'text', array(
'label' => /** @Desc("Lastname") */ 'form.label.lastname',
))
->add('street', 'text', array(
'label' => /** @Desc("Street") */ 'form.label.street',
'translation_domain' => 'address'
))
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefault('translation_domain', 'person');
$resolver->setDefault('other_option', 'should_not_override_domain');
}
}
28 changes: 21 additions & 7 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,11 @@ public function testExtractWithInterface()
$this->assertEquals($expected, $this->extract('MyFormTypeWithInterface.php'));
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly
*/
public function testExtractWithDefaultDomain()
protected function getDefaultDomainFixture($fixtureFile)
{
$expected = new MessageCatalogue();
$fileSourceFactory = $this->getFileSourceFactory();
$fixtureSplInfo = new \SplFileInfo(__DIR__.'/Fixture/MyFormTypeWithDefaultDomain.php');
$fixtureSplInfo = new \SplFileInfo($fixtureFile);

$message = new Message('form.label.lastname', 'person');
$message->setDesc('Lastname');
Expand All @@ -220,7 +216,25 @@ public function testExtractWithDefaultDomain()
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 37));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormTypeWithDefaultDomain.php'));
return $expected;
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly
*/
public function testExtractWithDefaultDomain()
{
$this->assertEquals($this->getDefaultDomainFixture(__DIR__.'/Fixture/MyFormTypeWithDefaultDomain.php'), $this->extract('MyFormTypeWithDefaultDomain.php'));
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly when set via setDefault
*/
public function testExtractWithDefaultDomainSetDefault()
{
$this->assertEquals($this->getDefaultDomainFixture(__DIR__.'/Fixture/MyFormTypeWithDefaultDomainSetDefault.php'), $this->extract('MyFormTypeWithDefaultDomainSetDefault.php'));
}

/**
Expand Down
11 changes: 10 additions & 1 deletion Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function enterNode(Node $node)
}

$name = strtolower($node->name);
if ('setdefaults' === $name || 'replacedefaults' === $name) {
if ('setdefaults' === $name || 'replacedefaults' === $name || 'setdefault' === $name) {
$this->parseDefaultsCall($node);
return;
}
Expand Down Expand Up @@ -310,6 +310,15 @@ private function parseDefaultsCall(Node $node)
return;
}

if (isset($node->args[1])
&& $node->args[0]->value instanceof Node\Scalar\String_
&& $node->args[1]->value instanceof Node\Scalar\String_
&& 'translation_domain' === $node->args[0]->value->value
) {
$this->defaultDomain = $node->args[1]->value->value;
return;
}

// ignore everything except an array
if (!$node->args[0]->value instanceof Node\Expr\Array_) {
return;
Expand Down