Skip to content

Commit 832686b

Browse files
committed
Merge branch '3.1'
* 3.1: fixed CS Properly format value in UniqueEntityValidator [Translation][fallback] add missing resources in parent catalogues. removed a deprecation notice [Form] Fix show float values as choices values in ChoiceType Remove double use Statement Improved the design of the metrics in the profiler [Yaml] set arguments depending on the PHP version [Console] Fix infinite loop on missing input [HttpFoundation][Session] memcached connection should not be closed
2 parents 2dd77a6 + 0255cb1 commit 832686b

File tree

18 files changed

+255
-43
lines changed

18 files changed

+255
-43
lines changed

src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\Test;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Doctrine\Common\Cache\ArrayCache;
16+
use Doctrine\ORM\Configuration;
1517
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
1618
use Doctrine\ORM\EntityManager;
1719

@@ -25,22 +27,19 @@ class DoctrineTestHelper
2527
/**
2628
* Returns an entity manager for testing.
2729
*
30+
* @param Configuration|null $config
31+
*
2832
* @return EntityManager
2933
*/
30-
public static function createTestEntityManager()
34+
public static function createTestEntityManager(Configuration $config = null)
3135
{
3236
if (!extension_loaded('pdo_sqlite')) {
3337
\PHPUnit_Framework_TestCase::markTestSkipped('Extension pdo_sqlite is required.');
3438
}
3539

36-
$config = new \Doctrine\ORM\Configuration();
37-
$config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'));
38-
$config->setAutoGenerateProxyClasses(true);
39-
$config->setProxyDir(\sys_get_temp_dir());
40-
$config->setProxyNamespace('SymfonyTests\Doctrine');
41-
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
42-
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
43-
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
40+
if (null === $config) {
41+
$config = self::createTestConfiguration();
42+
}
4443

4544
$params = array(
4645
'driver' => 'pdo_sqlite',
@@ -50,6 +49,23 @@ public static function createTestEntityManager()
5049
return EntityManager::create($params, $config);
5150
}
5251

52+
/**
53+
* @return Configuration
54+
*/
55+
public static function createTestConfiguration()
56+
{
57+
$config = new Configuration();
58+
$config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'));
59+
$config->setAutoGenerateProxyClasses(true);
60+
$config->setProxyDir(\sys_get_temp_dir());
61+
$config->setProxyNamespace('SymfonyTests\Doctrine');
62+
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
63+
$config->setQueryCacheImpl(new ArrayCache());
64+
$config->setMetadataCacheImpl(new ArrayCache());
65+
66+
return $config;
67+
}
68+
5369
/**
5470
* This class cannot be instantiated.
5571
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Test;
13+
14+
use Doctrine\Common\Persistence\ObjectRepository;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Doctrine\ORM\Mapping\ClassMetadata;
17+
use Doctrine\ORM\Repository\RepositoryFactory;
18+
19+
/**
20+
* @author Andreas Braun <[email protected]>
21+
*/
22+
final class TestRepositoryFactory implements RepositoryFactory
23+
{
24+
/**
25+
* @var ObjectRepository[]
26+
*/
27+
private $repositoryList = array();
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function getRepository(EntityManagerInterface $entityManager, $entityName)
33+
{
34+
$repositoryHash = $this->getRepositoryHash($entityManager, $entityName);
35+
36+
if (isset($this->repositoryList[$repositoryHash])) {
37+
return $this->repositoryList[$repositoryHash];
38+
}
39+
40+
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
41+
}
42+
43+
public function setRepository(EntityManagerInterface $entityManager, $entityName, ObjectRepository $repository)
44+
{
45+
$repositoryHash = $this->getRepositoryHash($entityManager, $entityName);
46+
47+
$this->repositoryList[$repositoryHash] = $repository;
48+
}
49+
50+
/**
51+
* @return ObjectRepository
52+
*/
53+
private function createRepository(EntityManagerInterface $entityManager, $entityName)
54+
{
55+
/* @var $metadata ClassMetadata */
56+
$metadata = $entityManager->getClassMetadata($entityName);
57+
$repositoryClassName = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
58+
59+
return new $repositoryClassName($entityManager, $metadata);
60+
}
61+
62+
private function getRepositoryHash(EntityManagerInterface $entityManager, $entityName)
63+
{
64+
return $entityManager->getClassMetadata($entityName)->getName().spl_object_hash($entityManager);
65+
}
66+
}

src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class SingleIntIdEntity
2424
/** @Column(type="string", nullable=true) */
2525
public $name;
2626

27+
/** @Column(type="array", nullable=true) */
28+
public $phoneNumbers = array();
29+
2730
public function __construct($id, $name)
2831
{
2932
$this->id = $id;

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Common\Persistence\ObjectManager;
1717
use Doctrine\Common\Persistence\ObjectRepository;
1818
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
19+
use Symfony\Bridge\Doctrine\Test\TestRepositoryFactory;
1920
use Symfony\Bridge\Doctrine\Tests\Fixtures\Employee;
2021
use Symfony\Bridge\Doctrine\Tests\Fixtures\Person;
2122
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
@@ -52,9 +53,16 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
5253
*/
5354
protected $repository;
5455

56+
protected $repositoryFactory;
57+
5558
protected function setUp()
5659
{
57-
$this->em = DoctrineTestHelper::createTestEntityManager();
60+
$this->repositoryFactory = new TestRepositoryFactory();
61+
62+
$config = DoctrineTestHelper::createTestConfiguration();
63+
$config->setRepositoryFactory($this->repositoryFactory);
64+
65+
$this->em = DoctrineTestHelper::createTestEntityManager($config);
5866
$this->registry = $this->createRegistryMock($this->em);
5967
$this->createSchema($this->em);
6068

@@ -170,7 +178,7 @@ public function testValidateUniqueness()
170178

171179
$this->buildViolation('myMessage')
172180
->atPath('property.path.name')
173-
->setParameter('{{ value }}', 'Foo')
181+
->setParameter('{{ value }}', '"Foo"')
174182
->setInvalidValue('Foo')
175183
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
176184
->assertRaised();
@@ -195,7 +203,7 @@ public function testValidateCustomErrorPath()
195203

196204
$this->buildViolation('myMessage')
197205
->atPath('property.path.bar')
198-
->setParameter('{{ value }}', 'Foo')
206+
->setParameter('{{ value }}', '"Foo"')
199207
->setInvalidValue('Foo')
200208
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
201209
->assertRaised();
@@ -248,7 +256,7 @@ public function testValidateUniquenessWithIgnoreNull()
248256

249257
$this->buildViolation('myMessage')
250258
->atPath('property.path.name')
251-
->setParameter('{{ value }}', 'Foo')
259+
->setParameter('{{ value }}', '"Foo"')
252260
->setInvalidValue('Foo')
253261
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
254262
->assertRaised();
@@ -281,7 +289,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
281289

282290
$this->buildViolation('myMessage')
283291
->atPath('property.path.name2')
284-
->setParameter('{{ value }}', 'Bar')
292+
->setParameter('{{ value }}', '"Bar"')
285293
->setInvalidValue('Bar')
286294
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
287295
->assertRaised();
@@ -452,7 +460,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
452460

453461
$this->buildViolation('myMessage')
454462
->atPath('property.path.single')
455-
->setParameter('{{ value }}', $expectedValue)
463+
->setParameter('{{ value }}', '"'.$expectedValue.'"')
456464
->setInvalidValue($expectedValue)
457465
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
458466
->assertRaised();
@@ -478,6 +486,44 @@ public function testAssociatedEntityWithNull()
478486
$this->assertNoViolation();
479487
}
480488

489+
public function testValidateUniquenessWithArrayValue()
490+
{
491+
$repository = $this->createRepositoryMock();
492+
$this->repositoryFactory->setRepository($this->em, 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity', $repository);
493+
494+
$constraint = new UniqueEntity(array(
495+
'message' => 'myMessage',
496+
'fields' => array('phoneNumbers'),
497+
'em' => self::EM_NAME,
498+
'repositoryMethod' => 'findByCustom',
499+
));
500+
501+
$entity1 = new SingleIntIdEntity(1, 'foo');
502+
$entity1->phoneNumbers[] = 123;
503+
504+
$repository->expects($this->once())
505+
->method('findByCustom')
506+
->will($this->returnValue(array($entity1)))
507+
;
508+
509+
$this->em->persist($entity1);
510+
$this->em->flush();
511+
512+
$entity2 = new SingleIntIdEntity(2, 'bar');
513+
$entity2->phoneNumbers[] = 123;
514+
$this->em->persist($entity2);
515+
$this->em->flush();
516+
517+
$this->validator->validate($entity2, $constraint);
518+
519+
$this->buildViolation('myMessage')
520+
->atPath('property.path.phoneNumbers')
521+
->setParameter('{{ value }}', 'array')
522+
->setInvalidValue(array(123))
523+
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
524+
->assertRaised();
525+
}
526+
481527
/**
482528
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
483529
* @expectedExceptionMessage Object manager "foo" does not exist.

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function validate($entity, Constraint $constraint)
147147

148148
$this->context->buildViolation($constraint->message)
149149
->atPath($errorPath)
150-
->setParameter('{{ value }}', $invalidValue)
150+
->setParameter('{{ value }}', $this->formatValue($invalidValue, static::OBJECT_TO_STRING | static::PRETTY_DATE))
151151
->setInvalidValue($invalidValue)
152152
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
153153
->addViolation();

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
4949

5050
return $node;
5151
} else {
52-
$var = $env->getParser()->getVarName();
52+
$var = $this->getVarName();
5353
$name = new \Twig_Node_Expression_AssignName($var, $node->getTemplateLine());
5454
$this->scope->set('domain', new \Twig_Node_Expression_Name($var, $node->getTemplateLine()));
5555

@@ -123,4 +123,9 @@ private function isNamedArguments($arguments)
123123

124124
return false;
125125
}
126+
127+
private function getVarName()
128+
{
129+
return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
130+
}
126131
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,19 @@
6060
<span class="label">Symfony initialization</span>
6161
</div>
6262

63+
{% if profile.collectors.memory %}
64+
<div class="metric">
65+
<span class="value">{{ '%.2f'|format(profile.collectors.memory.memory / 1024 / 1024) }} <span class="unit">MB</span></span>
66+
<span class="label">Peak memory usage</span>
67+
</div>
68+
{% endif %}
69+
6370
{% if profile.children|length > 0 %}
71+
<div class="metric-divider"></div>
72+
6473
<div class="metric">
6574
<span class="value">{{ profile.children|length }}</span>
66-
<span class="label">Sub-Requests</span>
75+
<span class="label">Sub-Request{{ profile.children|length > 1 ? 's' }}</span>
6776
</div>
6877

6978
{% set subrequests_time = 0 %}
@@ -73,14 +82,7 @@
7382

7483
<div class="metric">
7584
<span class="value">{{ subrequests_time }} <span class="unit">ms</span></span>
76-
<span class="label">Sub-Requests time</span>
77-
</div>
78-
{% endif %}
79-
80-
{% if profile.collectors.memory %}
81-
<div class="metric">
82-
<span class="value">{{ '%.2f'|format(profile.collectors.memory.memory / 1024 / 1024) }} <span class="unit">MB</span></span>
83-
<span class="label">Peak memory usage</span>
85+
<span class="label">Sub-Request{{ profile.children|length > 1 ? 's' }} time</span>
8486
</div>
8587
{% endif %}
8688
</div>

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ table tbody ul {
257257
{# Metrics
258258
------------------------------------------------------------------------- #}
259259
.metrics {
260-
margin: 1em 0;
260+
margin: 1em 0 0;
261261
overflow: auto;
262262
}
263263
.metrics .metric {
264264
float: left;
265-
margin-right: 1em;
265+
margin: 0 1em 1em 0;
266266
}
267267

268268
.metric {
@@ -317,6 +317,12 @@ table tbody ul {
317317
vertical-align: middle;
318318
}
319319

320+
.metric-divider {
321+
float: left;
322+
margin: 0 1em;
323+
min-height: 1px; {# required to apply 'margin' to an empty 'div' #}
324+
}
325+
320326
{# Cards
321327
------------------------------------------------------------------------- #}
322328
.card {

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private function doAsk(OutputInterface $output, Question $question)
149149
if (false === $ret) {
150150
$ret = fgets($inputStream, 4096);
151151
if (false === $ret) {
152-
throw new \RuntimeException('Aborted');
152+
throw new RuntimeException('Aborted');
153153
}
154154
$ret = trim($ret);
155155
}
@@ -412,6 +412,8 @@ private function validateAttempts(callable $interviewer, OutputInterface $output
412412

413413
try {
414414
return call_user_func($question->getValidator(), $interviewer());
415+
} catch (RuntimeException $e) {
416+
throw $e;
415417
} catch (\Exception $error) {
416418
}
417419
}

0 commit comments

Comments
 (0)