From 0f84f3adfa879b8b66731af08e3a9b5dffa84b4b Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 8 Apr 2015 15:13:56 +0200 Subject: [PATCH 1/5] [Translator] Cache does not take fallback locales into consideration As we're dumping entire catalogues including their fallbacks (standalone or inlined in ~2.7), we need to use different cache files for different sets of fallback locales. --- .../Tests/Translation/TranslatorTest.php | 48 ++++++++++++++++--- .../Translation/Translator.php | 7 ++- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 2b2e1a6980..deaca9c2e2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Translation; use Symfony\Bundle\FrameworkBundle\Translation\Translator; +use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Translation\MessageSelector; @@ -183,6 +184,34 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase $this->assertSame('en-US', $translator->getLocale()); } + public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales() + { + /* + * Because the cache file contains a catalogue including all of its fallback + * catalogues, we must take the active set of fallback locales into + * consideration when loading a catalogue from the cache. + */ + $translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir)); + $translator->setLocale('a'); + $translator->setFallbackLocales(array('b')); + $translator->addResource('loader', array('foo' => 'foo (a)'), 'a'); + $translator->addResource('loader', array('bar' => 'bar (b)'), 'b'); + + $this->assertEquals('bar (b)', $translator->trans('bar')); + + // Remove fallback locale + $translator->setFallbackLocales(array()); + $this->assertEquals('bar', $translator->trans('bar')); + + // Use a fresh translator with no fallback locales, result should be the same + $translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir)); + $translator->setLocale('a'); + $translator->addResource('loader', array('foo' => 'foo (a)'), 'a'); + $translator->addResource('loader', array('bar' => 'bar (b)'), 'b'); + + $this->assertEquals('bar', $translator->trans('bar')); + } + protected function getCatalogue($locale, $messages) { $catalogue = new MessageCatalogue($locale); @@ -265,12 +294,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator') { - $translator = new $translatorClass( - $this->getContainer($loader), - new MessageSelector(), - array('loader' => array('loader')), - $options - ); + $translator = $this->createTranslator($loader, $options, $translatorClass); $translator->addResource('loader', 'foo', 'fr'); $translator->addResource('loader', 'foo', 'en'); @@ -282,6 +306,18 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase return $translator; } + + private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator') + { + $translator = new $translatorClass( + $this->getContainer($loader), + new MessageSelector(), + array('loader' => array('loader')), + $options + ); + + return $translator; + } } class TranslatorWithInvalidLocale extends Translator diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index ffc050fb48..f791a63509 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -95,7 +95,7 @@ class Translator extends BaseTranslator $this->assertValidLocale($locale); - $cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']); + $cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->options['debug']); if (!$cache->isFresh()) { $this->initialize(); @@ -157,4 +157,9 @@ EOF } } } + + private function getCatalogueCachePath($locale) + { + return $this->options['cache_dir'].'/catalogue.'.$locale.'.'.sha1(serialize($this->getFallbackLocales())).'.php'; + } } From 4d01fab152b3eaffb227826cc4c62daa2ecf28a4 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Wed, 8 Apr 2015 23:16:18 +0100 Subject: [PATCH 2/5] [2.3][Translation] test refresh cache when resources File change. --- .../Tests/Translation/TranslatorTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index deaca9c2e2..5c2ab2c9cc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -78,6 +78,8 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase // do it another time as the cache is primed now $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); + $loader->expects($this->never())->method('load'); + $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir)); $translator->setLocale('fr'); $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); @@ -91,6 +93,27 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); + + // refresh cache again when resource file resources file change + $resource = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface'); + $resource + ->expects($this->at(0)) + ->method('isFresh') + ->will($this->returnValue(false)) + ; + $catalogue = $this->getCatalogue('fr', array('foo' => 'foo fresh')); + $catalogue->addResource($resource); + + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); + $loader + ->expects($this->at(0)) + ->method('load') + ->will($this->returnValue($catalogue)) + ; + + $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir)); + $translator->setLocale('fr'); + $this->assertEquals('foo fresh', $translator->trans('foo')); } public function testTransWithCachingWithInvalidLocale() From df76126d47d582919dd5093426b1d050e1179dd5 Mon Sep 17 00:00:00 2001 From: Possum Date: Fri, 10 Apr 2015 08:59:04 +0200 Subject: [PATCH 3/5] Fix javascript --- .../Resources/views/Collector/time.html.twig | 6 +++--- .../Resources/views/Profiler/base_js.html.twig | 4 +--- .../Resources/views/Profiler/layout.html.twig | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig index ff90d27b9a..da791b1fec 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig @@ -103,13 +103,13 @@ return dict.hasOwnProperty(key) ? dict[key] : null; - } + }; this.set = function(key, value) { dict[key] = value; return value; - } + }; }; /** @@ -383,7 +383,7 @@ return this; }; - }; + } function canvasAutoUpdateOnResizeAndSubmit(e) { e.preventDefault(); diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig index 3830047198..ed221b6e31 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig @@ -98,9 +98,7 @@ }, toggle: function(selector, elOn, elOff) { - var i, - style, - tmp = elOn.style.display, + var tmp = elOn.style.display, el = document.getElementById(selector); elOn.style.display = elOff.style.display; diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig index 2933564de8..e8c0900952 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig @@ -121,7 +121,7 @@ for (elem in menuItems) { if (typeof(menuItems[elem].children) !== 'undefined' && menuItems[elem].children.length > 0) { - child = menuItems[elem].children[0] + child = menuItems[elem].children[0]; if ('' === child.getAttribute('title') || null === child.getAttribute('title')) { From c1983cad4db30ad24a9fab492fe4ee73bb694844 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 10 Apr 2015 20:06:04 +0200 Subject: [PATCH 4/5] [2.3] Fix @link annotations --- src/Symfony/Bridge/Twig/Node/RenderBlockNode.php | 2 +- .../Form/Extension/Core/ChoiceList/ChoiceListInterface.php | 4 ++-- src/Symfony/Component/Form/Extension/Core/Type/BaseType.php | 2 +- .../HttpFoundation/EventListener/BindRequestListener.php | 2 +- .../Extension/Validator/ViolationMapper/ViolationPath.php | 2 +- src/Symfony/Component/Form/NativeRequestHandler.php | 2 +- src/Symfony/Component/Validator/DefaultTranslator.php | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php b/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php index 457532ecd5..35dcb40b62 100644 --- a/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php +++ b/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php @@ -12,7 +12,7 @@ namespace Symfony\Bridge\Twig\Node; /** - * Compiles a call to {@link FormRendererInterface::renderBlock()}. + * Compiles a call to {@link \Symfony\Component\Form\FormRendererInterface::renderBlock()}. * * The function name is used as block name. For example, if the function name * is "foo", the block "foo" will be rendered. diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php index d205b92d4d..a1265a80dc 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php @@ -127,7 +127,7 @@ interface ChoiceListInterface * Returns the indices corresponding to the given choices. * * The indices must be positive integers or strings accepted by - * {@link FormConfigBuilder::validateName()}. + * {@link \Symfony\Component\Form\FormConfigBuilder::validateName()}. * * The index "placeholder" is internally reserved. * @@ -145,7 +145,7 @@ interface ChoiceListInterface * Returns the indices corresponding to the given values. * * The indices must be positive integers or strings accepted by - * {@link FormConfigBuilder::validateName()}. + * {@link \Symfony\Component\Form\FormConfigBuilder::validateName()}. * * The index "placeholder" is internally reserved. * diff --git a/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php b/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php index e22bc880e6..43d90d9599 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php @@ -21,7 +21,7 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface; * Encapsulates common logic of {@link FormType} and {@link ButtonType}. * * This type does not appear in the form's type inheritance chain and as such - * cannot be extended (via {@link FormTypeExtension}s) nor themed. + * cannot be extended (via {@link \Symfony\Component\Form\FormExtensionInterface}) nor themed. * * @author Bernhard Schussek */ diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php b/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php index bb144ed65a..2489cf371c 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php @@ -20,7 +20,7 @@ use Symfony\Component\HttpFoundation\Request; * @author Bernhard Schussek * * @deprecated Deprecated since version 2.3, to be removed in 3.0. Pass the - * Request instance to {@link Form::handleRequest()} instead. + * Request instance to {@link \Symfony\Component\Form\Form::handleRequest()} instead. */ class BindRequestListener implements EventSubscriberInterface { diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php index 8e406a9b08..c84f98a200 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php @@ -48,7 +48,7 @@ class ViolationPath implements \IteratorAggregate, PropertyPathInterface /** * Creates a new violation path from a string. * - * @param string $violationPath The property path of a {@link ConstraintViolation} + * @param string $violationPath The property path of a {@link \Symfony\Component\Validator\ConstraintViolation} * object. */ public function __construct($violationPath) diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index 7c883f12eb..c9a76858dd 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -159,7 +159,7 @@ class NativeRequestHandler implements RequestHandlerInterface * It's safe to pass an already converted array, in which case this method * just returns the original array unmodified. * - * This method is identical to {@link Symfony\Component\HttpFoundation\FileBag::fixPhpFilesArray} + * This method is identical to {@link \Symfony\Component\HttpFoundation\FileBag::fixPhpFilesArray} * and should be kept as such in order to port fixes quickly and easily. * * @param array $data diff --git a/src/Symfony/Component/Validator/DefaultTranslator.php b/src/Symfony/Component/Validator/DefaultTranslator.php index cb16c4dc04..46c2a453b2 100644 --- a/src/Symfony/Component/Validator/DefaultTranslator.php +++ b/src/Symfony/Component/Validator/DefaultTranslator.php @@ -88,7 +88,7 @@ class DefaultTranslator implements TranslatorInterface * have the same expressiveness. While Translator supports intervals in * message translations, which are needed for languages other than English, * this translator does not. You should use Translator or a custom - * implementation of {@link TranslatorInterface} if you need this or similar + * implementation of {@link \Symfony\Component\Translation\TranslatorInterface} if you need this or similar * functionality. * * Example usage: From a8e4c43080d71841531db63c4b35b81a8924dadf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 11 Apr 2015 08:34:27 +0200 Subject: [PATCH 5/5] renamed some confusing tests --- .../HttpKernel/Tests/HttpKernelTest.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php index 441b665a5f..ad949c7247 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php @@ -26,7 +26,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase /** * @expectedException \RuntimeException */ - public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue() + public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue() { $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); @@ -36,14 +36,14 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase /** * @expectedException \RuntimeException */ - public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered() + public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered() { $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false); } - public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse() + public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener() { $dispatcher = new EventDispatcher(); $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) { @@ -51,12 +51,26 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase }); $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); })); - $response = $kernel->handle(new Request()); + $response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true); $this->assertEquals('500', $response->getStatusCode()); $this->assertEquals('foo', $response->getContent()); } + /** + * @expectedException \RuntimeException + */ + public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener() + { + $dispatcher = new EventDispatcher(); + $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) { + // should set a response, but does not + }); + + $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); })); + $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true); + } + public function testHandleExceptionWithARedirectionResponse() { $dispatcher = new EventDispatcher();