Merge branch '2.3' into 2.4
* 2.3: [Translator] Use quote to surround invalid locale Optimize assertLocale regexp [Validator] Backported #11410 to 2.3: Object initializers are called only once per object [Translator][FrameworkBundle] Added @ to the list of allowed chars in Translator [Process] Reduce I/O load on Windows platform [Form] Check if IntlDateFormatter constructor returned a valid object before using it
This commit is contained in:
commit
b07a6b6201
@ -45,7 +45,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$translator = $this->getTranslator($this->getLoader());
|
$translator = $this->getTranslator($this->getLoader());
|
||||||
$translator->setLocale('fr');
|
$translator->setLocale('fr');
|
||||||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8'));
|
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
|
||||||
|
|
||||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||||
@ -55,6 +55,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
||||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
||||||
|
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTransWithCaching()
|
public function testTransWithCaching()
|
||||||
@ -62,7 +63,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
// prime the cache
|
// prime the cache
|
||||||
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
|
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
|
||||||
$translator->setLocale('fr');
|
$translator->setLocale('fr');
|
||||||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8'));
|
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
|
||||||
|
|
||||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||||
@ -72,12 +73,13 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
||||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
||||||
|
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
|
||||||
|
|
||||||
// do it another time as the cache is primed now
|
// do it another time as the cache is primed now
|
||||||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
||||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
|
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
|
||||||
$translator->setLocale('fr');
|
$translator->setLocale('fr');
|
||||||
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8'));
|
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
|
||||||
|
|
||||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||||
@ -87,6 +89,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
|
||||||
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
|
||||||
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
|
||||||
|
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLocale()
|
public function testGetLocale()
|
||||||
@ -185,6 +188,13 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foobarbaz' => 'foobarbaz (fr.UTF-8)',
|
'foobarbaz' => 'foobarbaz (fr.UTF-8)',
|
||||||
))))
|
))))
|
||||||
;
|
;
|
||||||
|
$loader
|
||||||
|
->expects($this->at(6))
|
||||||
|
->method('load')
|
||||||
|
->will($this->returnValue($this->getCatalogue('sr@latin', array(
|
||||||
|
'foobarbax' => 'foobarbax (sr@latin)',
|
||||||
|
))))
|
||||||
|
;
|
||||||
|
|
||||||
return $loader;
|
return $loader;
|
||||||
}
|
}
|
||||||
@ -216,6 +226,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
|
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
|
||||||
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
|
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
|
||||||
$translator->addResource('loader', 'foo', 'fr.UTF-8');
|
$translator->addResource('loader', 'foo', 'fr.UTF-8');
|
||||||
|
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
|
||||||
|
|
||||||
return $translator;
|
return $translator;
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
|||||||
* Returns a preconfigured IntlDateFormatter instance
|
* Returns a preconfigured IntlDateFormatter instance
|
||||||
*
|
*
|
||||||
* @return \IntlDateFormatter
|
* @return \IntlDateFormatter
|
||||||
|
*
|
||||||
|
* @throws TransformationFailedException in case the date formatter can not be constructed.
|
||||||
*/
|
*/
|
||||||
protected function getIntlDateFormatter()
|
protected function getIntlDateFormatter()
|
||||||
{
|
{
|
||||||
@ -162,6 +164,12 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
|
|||||||
$pattern = $this->pattern;
|
$pattern = $this->pattern;
|
||||||
|
|
||||||
$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
|
$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
|
||||||
|
|
||||||
|
// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
|
||||||
|
if (!$intlDateFormatter) {
|
||||||
|
throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
|
||||||
|
}
|
||||||
|
|
||||||
$intlDateFormatter->setLenient(false);
|
$intlDateFormatter->setLenient(false);
|
||||||
|
|
||||||
return $intlDateFormatter;
|
return $intlDateFormatter;
|
||||||
|
@ -77,6 +77,12 @@ class DateType extends AbstractType
|
|||||||
$calendar,
|
$calendar,
|
||||||
$pattern
|
$pattern
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
|
||||||
|
if (!$formatter) {
|
||||||
|
throw new InvalidOptionsException(intl_get_error_message(), intl_get_error_code());
|
||||||
|
}
|
||||||
|
|
||||||
$formatter->setLenient(false);
|
$formatter->setLenient(false);
|
||||||
|
|
||||||
if ('choice' === $options['widget']) {
|
if ('choice' === $options['widget']) {
|
||||||
|
@ -284,6 +284,8 @@ class ProcessPipes
|
|||||||
private function readStreams($blocking, $close = false)
|
private function readStreams($blocking, $close = false)
|
||||||
{
|
{
|
||||||
if (empty($this->pipes)) {
|
if (empty($this->pipes)) {
|
||||||
|
usleep(Process::TIMEOUT_PRECISION * 1E4);
|
||||||
|
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,7 +615,14 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
$duration = microtime(true) - $start;
|
$duration = microtime(true) - $start;
|
||||||
|
|
||||||
$this->assertLessThan($timeout + Process::TIMEOUT_PRECISION, $duration);
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
||||||
|
// Windows is a bit slower as it read file handles, then allow twice the precision
|
||||||
|
$maxDuration = $timeout + 2 * Process::TIMEOUT_PRECISION;
|
||||||
|
} else {
|
||||||
|
$maxDuration = $timeout + Process::TIMEOUT_PRECISION;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertLessThan($maxDuration, $duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCheckTimeoutOnNonStartedProcess()
|
public function testCheckTimeoutOnNonStartedProcess()
|
||||||
|
@ -103,7 +103,6 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('bar (fr)', $translator->trans('bar'));
|
$this->assertEquals('bar (fr)', $translator->trans('bar'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getInvalidLocalesTests
|
* @dataProvider getInvalidLocalesTests
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
@ -329,7 +328,6 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
// no assertion. this method just asserts that no exception is thrown
|
// no assertion. this method just asserts that no exception is thrown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getTransFileTests()
|
public function getTransFileTests()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
@ -430,6 +428,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('fr_FR'),
|
array('fr_FR'),
|
||||||
array('fr.FR'),
|
array('fr.FR'),
|
||||||
array('fr-FR.UTF8'),
|
array('fr-FR.UTF8'),
|
||||||
|
array('sr@latin'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,8 +318,8 @@ class Translator implements TranslatorInterface
|
|||||||
*/
|
*/
|
||||||
private function assertValidLocale($locale)
|
private function assertValidLocale($locale)
|
||||||
{
|
{
|
||||||
if (0 !== preg_match('/[^a-z0-9_\\.\\-]+/i', $locale, $match)) {
|
if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
|
||||||
throw new \InvalidArgumentException(sprintf('Invalid locale: %s.', $locale));
|
throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class Entity extends EntityParent implements EntityInterface
|
|||||||
public $reference;
|
public $reference;
|
||||||
private $internal;
|
private $internal;
|
||||||
public $data = 'Overridden data';
|
public $data = 'Overridden data';
|
||||||
|
public $initialized = false;
|
||||||
|
|
||||||
public function __construct($internal = null)
|
public function __construct($internal = null)
|
||||||
{
|
{
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Validator\Tests;
|
namespace Symfony\Component\Validator\Tests;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraints\Callback;
|
||||||
|
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
|
use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
|
||||||
use Symfony\Component\Validator\Constraints\Valid;
|
use Symfony\Component\Validator\Constraints\Valid;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\Reference;
|
use Symfony\Component\Validator\Tests\Fixtures\Reference;
|
||||||
@ -561,4 +563,50 @@ class ValidationVisitorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->visitor->validate($entity, 'Default', '');
|
$this->visitor->validate($entity, 'Default', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInitializeObjectsOnFirstValidation()
|
||||||
|
{
|
||||||
|
$test = $this;
|
||||||
|
$entity = new Entity();
|
||||||
|
$entity->initialized = false;
|
||||||
|
|
||||||
|
// prepare initializers that set "initialized" to true
|
||||||
|
$initializer1 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
|
||||||
|
$initializer2 = $this->getMock('Symfony\\Component\\Validator\\ObjectInitializerInterface');
|
||||||
|
|
||||||
|
$initializer1->expects($this->once())
|
||||||
|
->method('initialize')
|
||||||
|
->with($entity)
|
||||||
|
->will($this->returnCallback(function ($object) {
|
||||||
|
$object->initialized = true;
|
||||||
|
}));
|
||||||
|
|
||||||
|
$initializer2->expects($this->once())
|
||||||
|
->method('initialize')
|
||||||
|
->with($entity);
|
||||||
|
|
||||||
|
$this->visitor = new ValidationVisitor('Root', $this->metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator(), null, array(
|
||||||
|
$initializer1,
|
||||||
|
$initializer2
|
||||||
|
));
|
||||||
|
|
||||||
|
// prepare constraint which
|
||||||
|
// * checks that "initialized" is set to true
|
||||||
|
// * validates the object again
|
||||||
|
$callback = function ($object, ExecutionContextInterface $context) use ($test) {
|
||||||
|
$test->assertTrue($object->initialized);
|
||||||
|
|
||||||
|
// validate again in same group
|
||||||
|
$context->validate($object);
|
||||||
|
|
||||||
|
// validate again in other group
|
||||||
|
$context->validate($object, '', 'SomeGroup');
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->metadata->addConstraint(new Callback(array($callback)));
|
||||||
|
|
||||||
|
$this->visitor->validate($entity, 'Default', '');
|
||||||
|
|
||||||
|
$this->assertTrue($entity->initialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,10 +127,8 @@ class ValidationVisitor implements ValidationVisitorInterface, GlobalExecutionCo
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remember validating this object before starting and possibly
|
// Initialize if the object wasn't initialized before
|
||||||
// traversing the object graph
|
if (!isset($this->validatedObjects[$hash])) {
|
||||||
$this->validatedObjects[$hash][$group] = true;
|
|
||||||
|
|
||||||
foreach ($this->objectInitializers as $initializer) {
|
foreach ($this->objectInitializers as $initializer) {
|
||||||
if (!$initializer instanceof ObjectInitializerInterface) {
|
if (!$initializer instanceof ObjectInitializerInterface) {
|
||||||
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
|
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
|
||||||
@ -139,6 +137,11 @@ class ValidationVisitor implements ValidationVisitorInterface, GlobalExecutionCo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remember validating this object before starting and possibly
|
||||||
|
// traversing the object graph
|
||||||
|
$this->validatedObjects[$hash][$group] = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Validate arrays recursively by default, otherwise every driver needs
|
// Validate arrays recursively by default, otherwise every driver needs
|
||||||
// to implement special handling for arrays.
|
// to implement special handling for arrays.
|
||||||
// https://github.com/symfony/symfony/issues/6246
|
// https://github.com/symfony/symfony/issues/6246
|
||||||
|
Reference in New Issue
Block a user