8000 [DoctrineBridge] Improved test coverage of EntityChoiceList · symfony/symfony@7879f07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7879f07

Browse files
committed
[DoctrineBridge] Improved test coverage of EntityChoiceList
1 parent 58e7c10 commit 7879f07

30 files changed

+868
-296
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,26 @@ public function getChoicesForValues(array $values)
208208
// Optimize performance in case we have an entity loader and
209209
// a single-field identifier
210210
if ($this->idAsValue && $this->entityLoader) {
211-
if (empty($values)) {
212-
return array();
211+
$unorderedEntities = $this->entityLoader->getEntitiesByIds($this->idField, $values);
212+
$entitiesByValue = array();
9E81 213+
$entities = array();
214+
215+
// Maintain order and indices from the given $values
216+
// An alternative approach to the following loop is to add the
217+
// "INDEX BY" clause to the Doctrine query in the loader,
218+
// but I'm not sure whether that's doable in a generic fashion.
219+
foreach ($unorderedEntities as $entity) {
220+
$value = $this->fixValue(current($this->getIdentifierValues($entity)));
221+
$entitiesByValue[$value] = $entity;
213222
}
214223

215-
return $this->entityLoader->getEntitiesByIds($this->idField, $values);
224+
foreach ($values as $i => $value) {
225+
if (isset($entitiesByValue[$value])) {
226+
$entities[$i] = $entitiesByValue[$value];
227+
}
228+
}
229+
230+
return $entities;
216231
}
217232

218233
$this->load();
@@ -240,10 +255,10 @@ public function getValuesForChoices(array $entities)
240255
if ($this->idAsValue) {
241256
$values = array();
242257

243-
foreach ($entities as $entity) {
258+
foreach ($entities as $i => $entity) {
244259
if ($entity instanceof $this->class) {
245260
// Make sure to convert to the right format
246-
$values[] = $this->fixValue(current($this->getIdentifierValues($entity)));
261+
$values[$i] = $this->fixValue(current($this->getIdentifierValues($entity)));
247262
}
248263
}
249264

@@ -275,10 +290,10 @@ public function getIndicesForChoices(array $entities)
275290
if ($this->idAsIndex) {
276291
$indices = array();
277292

278-
foreach ($entities as $entity) {
293+
foreach ($entities as $i => $entity) {
279294
if ($entity instanceof $this->class) {
280295
// Make sure to convert to the right format
281-
$indices[] = $this->fixIndex(current($this->getIdentifierValues($entity)));
296+
$indices[$i] = $this->fixIndex(current($this->getIdentifierValues($entity)));
282297
}
283298
}
284299

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Annotations\AnnotationReader;
15+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
16+
use Doctrine\ORM\EntityManager;
17+
18+
/**
19+
* Provides utility functions needed in tests.
20+
*
21+
* @author Bernhard Schussek <bschussek@gmail.com>
22+
*/
23+
class DoctrineTestHelper
24+
{
25+
/**
26+
* Returns an entity manager for testing.
27+
*
28+
* @return EntityManager
29+
*/
30+
public static function createTestEntityManager()
31+
{
32+
if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
33+
\PHPUnit_Framework_TestCase::markTestSkipped('This test requires SQLite support in your environment');
34+
}
35+
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());
44+
45+
$params = array(
46+
'driver' => 'pdo_sqlite',
47+
'memory' => true,
48+
);
49+
50+
return EntityManager::create($params, $config);
51+
}
52+
53+
/**
54+
* This class cannot be instantiated.
55+
*/
56+
private function __construct()
57+
{
58+
}
59+
}

src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Tests;
1313

14-
use Doctrine\Common\Annotations\AnnotationReader;
15-
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
16-
use Doctrine\ORM\EntityManager;
14+
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
1715

16+
/**
17+
* Class DoctrineOrmTestCase
18+
*
19+
* @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0.
20+
* Use {@link DoctrineTestHelper} instead.
21+
*/
1822
abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase
1923
{
2024
protected function setUp()
@@ -33,27 +37,10 @@ protected function setUp()
3337
}
3438

3539
/**
36-
* @return EntityManager
40+
* @return \Doctrine\ORM\EntityManager
3741
*/
38-
public static function createTestEntityManager($paths = array())
42+
public static function createTestEntityManager()
3943
{
40-
if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
41-
self::markTestSkipped('This test requires SQLite support in your environment');
42-
}
43-
$config = new \Doctrine\ORM\Configuration();
44-
$config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'));
45-
$config->setAutoGenerateProxyClasses(true);
46-
$config->setProxyDir(\sys_get_temp_dir());
47-
$config->setProxyNamespace('SymfonyTests\Doctrine');
48-
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
49-
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
50-
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
51-
52-
$params = array(
53-
'driver' => 'pdo_sqlite',
54-
'memory' => true,
55-
);
56-
57-
return EntityManager::create($params, $config);
44+
return DoctrineTestHelper::createTestEntityManager();
5845
}
5946
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ class AssociationEntity
2626
private $id;
2727

2828
/**
29-
* @ORM\ManyToOne(targetEntity="SingleIdentEntity")
30-
* @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\SingleIdentEntity
29+
* @ORM\ManyToOne(targetEntity="SingleIntIdEntity")
30+
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity
3131
*/
3232
public $single;
3333

3434
/**
35-
* @ORM\ManyToOne(targetEntity="CompositeIdentEntity")
35+
* @ORM\ManyToOne(targetEntity="CompositeIntIdEntity")
3636
* @ORM\JoinColumns({
3737
* @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"),
3838
* @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2")
3939
* })
40-
* @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\CompositeIdentEntity
40+
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity
4141
*/
4242
public $composite;
4343
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/** @Entity */
19+
class CompositeIntIdEntity
20+
{
21+
/** @Id @Column(type="integer") */
22+
protected $id1;
23+
24+
/** @Id @Column(type="integer") */
25+
protected $id2;
26+
27+
/** @Column(type="string") */
28+
public $name;
29+
30+
public function __construct($id1, $id2, $name)
31+
{
32+
$this->id1 = $id1;
33+
$this->id2 = $id2;
34+
$this->name = $name;
35+
}
36+
37+
public function __toString()
38+
{
39+
return $this->name;
40+
}
41+
}

src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class CompositeStringIdentEntity
19+
class CompositeStringIdEntity
2020
{
2121
/** @Id @Column(type="string") */
2222
protected $id1;
@@ -33,4 +33,9 @@ public function __construct($id1, $id2, $name)
3333
$this->id2 = $id2;
3434
$this->name = $name;
3535
}
36+
37+
public function __toString()
38+
{
39+
return $this->name;
40+
}
3641
}

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class DoubleIdentEntity
19+
class DoubleNameEntity
2020
{
2121
/** @Id @Column(type="integer") */
2222
protected $id;

src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class ItemGroupEntity
19+
class GroupableEntity
2020
{
2121
/** @Id @Column(type="integer") */
2222
protected $id;

src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class SingleIdentEntity
19+
class SingleIntIdEntity
2020
{
2121
/** @Id @Column(type="integer") */
2222
protected $id;

src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class NoToStringSingleIdentEntity
19+
class SingleIntIdNoToStringEntity
2020
{
2121
/** @Id @Column(type="integer") */
2222
protected $id;

src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Doctrine\ORM\Mapping\Entity;
1717

1818
/** @Entity */
19-
class SingleStringIdentEntity
19+
class SingleStringIdEntity
2020
{
2121
/** @Id @Column(type="string") */
2222
protected $id;
@@ -29,4 +29,9 @@ public function __construct($id, $name)
2929
$this->id = $id;
3030
$this->name = $name;
3131
}
32+
33+
public function __toString()
34+
{
35+
return $this->name;
36+
}
3237
}

src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php renamed to src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Security\Core\User\UserInterface;
1818

1919
/** @Entity */
20-
class CompositeIdentEntity implements UserInterface
20+
class User implements UserInterface
2121
{
2222
/** @Id @Column(type="integer") */
2323
protected $id1;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Form\ChoiceList;
13+
14+
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity;
15+
16+
/**
17+
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*/
19+
abstract class AbstractEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListTest
20+
{
21+
protected function getEntityClass()
22+
{
23+
return 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity';
24+
}
25+
26+
/**
27+
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
28+
*/
29+
protected function createObjects()
30+
{
31+
return array(
32+
new CompositeIntIdEntity(10, 11, 'A'),
33+
new CompositeIntIdEntity(20, 21, 'B'),
34+
new CompositeIntIdEntity(30, 31, 'C'),
35+
new CompositeIntIdEntity(40, 41, 'D'),
36+
);
37+
}
38+
39+
protected function getChoices()
40+
{
41+
return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4);
42+
}
43+
44+
protected function getLabels()
45+
{
46+
return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
47+
}
48+
49+
protected function getValues()
50+
{
51+
return array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
52+
}
53+
54+
protected function getIndices()
55+
{
56+
return array(0, 1, 2, 3);
57+
}
58+
}

0 commit comments

Comments
 (0)
0