diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php index 74042a7b41..a4f69eefe6 100644 --- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php +++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php @@ -208,11 +208,26 @@ class EntityChoiceList extends ObjectChoiceList // Optimize performance in case we have an entity loader and // a single-field identifier if ($this->idAsValue && $this->entityLoader) { - if (empty($values)) { - return array(); + $unorderedEntities = $this->entityLoader->getEntitiesByIds($this->idField, $values); + $entitiesByValue = array(); + $entities = array(); + + // Maintain order and indices from the given $values + // An alternative approach to the following loop is to add the + // "INDEX BY" clause to the Doctrine query in the loader, + // but I'm not sure whether that's doable in a generic fashion. + foreach ($unorderedEntities as $entity) { + $value = $this->fixValue(current($this->getIdentifierValues($entity))); + $entitiesByValue[$value] = $entity; } - return $this->entityLoader->getEntitiesByIds($this->idField, $values); + foreach ($values as $i => $value) { + if (isset($entitiesByValue[$value])) { + $entities[$i] = $entitiesByValue[$value]; + } + } + + return $entities; } $this->load(); @@ -240,10 +255,10 @@ class EntityChoiceList extends ObjectChoiceList if ($this->idAsValue) { $values = array(); - foreach ($entities as $entity) { + foreach ($entities as $i => $entity) { if ($entity instanceof $this->class) { // Make sure to convert to the right format - $values[] = $this->fixValue(current($this->getIdentifierValues($entity))); + $values[$i] = $this->fixValue(current($this->getIdentifierValues($entity))); } } @@ -275,10 +290,10 @@ class EntityChoiceList extends ObjectChoiceList if ($this->idAsIndex) { $indices = array(); - foreach ($entities as $entity) { + foreach ($entities as $i => $entity) { if ($entity instanceof $this->class) { // Make sure to convert to the right format - $indices[] = $this->fixIndex(current($this->getIdentifierValues($entity))); + $indices[$i] = $this->fixIndex(current($this->getIdentifierValues($entity))); } } diff --git a/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php b/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php new file mode 100644 index 0000000000..c763653ad9 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Test; + +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; +use Doctrine\ORM\EntityManager; + +/** + * Provides utility functions needed in tests. + * + * @author Bernhard Schussek + */ +class DoctrineTestHelper +{ + /** + * Returns an entity manager for testing. + * + * @return EntityManager + */ + public static function createTestEntityManager() + { + if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) { + \PHPUnit_Framework_TestCase::markTestSkipped('This test requires SQLite support in your environment'); + } + + $config = new \Doctrine\ORM\Configuration(); + $config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures')); + $config->setAutoGenerateProxyClasses(true); + $config->setProxyDir(\sys_get_temp_dir()); + $config->setProxyNamespace('SymfonyTests\Doctrine'); + $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader())); + $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + + $params = array( + 'driver' => 'pdo_sqlite', + 'memory' => true, + ); + + return EntityManager::create($params, $config); + } + + /** + * This class cannot be instantiated. + */ + private function __construct() + { + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php b/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php index 228d34f4d9..577d198599 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php +++ b/src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php @@ -11,34 +11,21 @@ namespace Symfony\Bridge\Doctrine\Tests; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\ORM\Mapping\Driver\AnnotationDriver; -use Doctrine\ORM\EntityManager; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +/** + * Class DoctrineOrmTestCase + * + * @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0. + * Use {@link DoctrineTestHelper} instead. + */ abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase { /** - * @return EntityManager + * @return \Doctrine\ORM\EntityManager */ - public static function createTestEntityManager($paths = array()) + public static function createTestEntityManager() { - if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) { - self::markTestSkipped('This test requires SQLite support in your environment'); - } - $config = new \Doctrine\ORM\Configuration(); - $config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures')); - $config->setAutoGenerateProxyClasses(true); - $config->setProxyDir(\sys_get_temp_dir()); - $config->setProxyNamespace('SymfonyTests\Doctrine'); - $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader())); - $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); - $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); - - $params = array( - 'driver' => 'pdo_sqlite', - 'memory' => true, - ); - - return EntityManager::create($params, $config); + return DoctrineTestHelper::createTestEntityManager(); } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php index 0fe3d145a0..9a33435ff2 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/AssociationEntity.php @@ -26,18 +26,18 @@ class AssociationEntity private $id; /** - * @ORM\ManyToOne(targetEntity="SingleIdentEntity") - * @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\SingleIdentEntity + * @ORM\ManyToOne(targetEntity="SingleIntIdEntity") + * @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity */ public $single; /** - * @ORM\ManyToOne(targetEntity="CompositeIdentEntity") + * @ORM\ManyToOne(targetEntity="CompositeIntIdEntity") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"), * @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2") * }) - * @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\CompositeIdentEntity + * @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity */ public $composite; } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php new file mode 100644 index 0000000000..740a4f55f4 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIntIdEntity.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Fixtures; + +use Doctrine\ORM\Mapping\Id; +use Doctrine\ORM\Mapping\Column; +use Doctrine\ORM\Mapping\Entity; + +/** @Entity */ +class CompositeIntIdEntity +{ + /** @Id @Column(type="integer") */ + protected $id1; + + /** @Id @Column(type="integer") */ + protected $id2; + + /** @Column(type="string") */ + public $name; + + public function __construct($id1, $id2, $name) + { + $this->id1 = $id1; + $this->id2 = $id2; + $this->name = $name; + } + + public function __toString() + { + return $this->name; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php similarity index 87% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php index 43c71f6e80..10e083a8f4 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeStringIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class CompositeStringIdentEntity +class CompositeStringIdEntity { /** @Id @Column(type="string") */ protected $id1; @@ -33,4 +33,9 @@ class CompositeStringIdentEntity $this->id2 = $id2; $this->name = $name; } + + public function __toString() + { + return $this->name; + } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php similarity index 96% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php index 2ac1ad3a85..cfb8e8b666 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoubleNameEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class DoubleIdentEntity +class DoubleNameEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php similarity index 97% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php index 04d2ddfde9..2e36204bdf 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/ItemGroupEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/GroupableEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class ItemGroupEntity +class GroupableEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php similarity index 96% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php index 09ee18a2fc..44630a1fc5 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class SingleIdentEntity +class SingleIntIdEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php similarity index 94% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php index a5ecb3da27..bcbe7a5f7b 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/NoToStringSingleIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleIntIdNoToStringEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class NoToStringSingleIdentEntity +class SingleIntIdNoToStringEntity { /** @Id @Column(type="integer") */ protected $id; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php similarity index 86% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php index 50f53b790e..258c5a6515 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/SingleStringIdEntity.php @@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; /** @Entity */ -class SingleStringIdentEntity +class SingleStringIdEntity { /** @Id @Column(type="string") */ protected $id; @@ -29,4 +29,9 @@ class SingleStringIdentEntity $this->id = $id; $this->name = $name; } + + public function __toString() + { + return $this->name; + } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php similarity index 95% rename from src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php rename to src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php index 9d263141f7..e59e32c27e 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/CompositeIdentEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/User.php @@ -17,7 +17,7 @@ use Doctrine\ORM\Mapping\Entity; use Symfony\Component\Security\Core\User\UserInterface; /** @Entity */ -class CompositeIdentEntity implements UserInterface +class User implements UserInterface { /** @Id @Column(type="integer") */ protected $id1; diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000..5980d9c734 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListCompositeIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new CompositeIntIdEntity(10, 11, 'A'), + new CompositeIntIdEntity(20, 21, 'B'), + new CompositeIntIdEntity(30, 31, 'C'), + new CompositeIntIdEntity(40, 41, 'D'), + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000..74af66db36 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new SingleIntIdEntity(-10, 'A'), + new SingleIntIdEntity(10, 'B'), + new SingleIntIdEntity(20, 'C'), + new SingleIntIdEntity(30, 'D'), + ); + } + + protected function getChoices() + { + return array('_10' => $this->obj1, 10 => $this->obj2, 20 => $this->obj3, 30 => $this->obj4); + } + + protected function getLabels() + { + return array('_10' => 'A', 10 => 'B', 20 => 'C', 30 => 'D'); + } + + protected function getValues() + { + return array('_10' => '-10', 10 => '10', 20 => '20', 30 => '30'); + } + + protected function getIndices() + { + return array('_10', 10, 20, 30); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000..56b4c21319 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListTest +{ + protected function getEntityClass() + { + return 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + } + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createObjects() + { + return array( + new SingleStringIdEntity('a', 'A'), + new SingleStringIdEntity('b', 'B'), + new SingleStringIdEntity('c', 'C'), + new SingleStringIdEntity('d', 'D'), + ); + } + + protected function getChoices() + { + return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4); + } + + protected function getLabels() + { + return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'); + } + + protected function getValues() + { + return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'); + } + + protected function getIndices() + { + return array(0, 1, 2, 3); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php new file mode 100644 index 0000000000..a56123a2a1 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/AbstractEntityChoiceListTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Doctrine\ORM\Tools\SchemaTool; +use Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest; + +/** + * @author Bernhard Schussek + */ +abstract class AbstractEntityChoiceListTest extends AbstractChoiceListTest +{ + /** + * @var \Doctrine\ORM\EntityManager + */ + protected $em; + + protected $obj1; + + protected $obj2; + + protected $obj3; + + protected $obj4; + + protected function setUp() + { + if (!class_exists('Symfony\Component\Form\Form')) { + $this->markTestSkipped('The "Form" component is not available'); + } + + if (!class_exists('Doctrine\DBAL\Platforms\MySqlPlatform')) { + $this->markTestSkipped('Doctrine DBAL is not available.'); + } + + if (!class_exists('Doctrine\Common\Version')) { + $this->markTestSkipped('Doctrine Common is not available.'); + } + + if (!class_exists('Doctrine\ORM\EntityManager')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $this->em = DoctrineTestHelper::createTestEntityManager(); + + $schemaTool = new SchemaTool($this->em); + $classes = array($this->em->getClassMetadata($this->getEntityClass())); + + try { + $schemaTool->dropSchema($classes); + } catch (\Exception $e) { + } + + try { + $schemaTool->createSchema($classes); + } catch (\Exception $e) { + } + + list($this->obj1, $this->obj2, $this->obj3, $this->obj4) = $this->createObjects(); + + $this->em->persist($this->obj1); + $this->em->persist($this->obj2); + $this->em->persist($this->obj3); + $this->em->persist($this->obj4); + $this->em->flush(); + + parent::setUp(); + } + + protected function tearDown() + { + parent::tearDown(); + + $this->em = null; + } + + abstract protected function getEntityClass(); + + abstract protected function createObjects(); + + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + return new EntityChoiceList($this->em, $this->getEntityClass()); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php deleted file mode 100644 index 3f9c573da5..0000000000 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php +++ /dev/null @@ -1,354 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; - -use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\NoToStringSingleIdentEntity; -use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; -use Symfony\Component\Form\Extension\Core\View\ChoiceView; -use Doctrine\ORM\Tools\SchemaTool; - -class EntityChoiceListTest extends DoctrineOrmTestCase -{ - const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity'; - - const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; - - const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity'; - - const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'; - - private $em; - - protected function setUp() - { - parent::setUp(); - - $this->em = $this->createTestEntityManager(); - - $schemaTool = new SchemaTool($this->em); - $classes = array( - $this->em->getClassMetadata(self::ITEM_GROUP_CLASS), - $this->em->getClassMetadata(self::SINGLE_IDENT_CLASS), - $this->em->getClassMetadata(self::SINGLE_STRING_IDENT_CLASS), - $this->em->getClassMetadata(self::COMPOSITE_IDENT_CLASS), - ); - - try { - $schemaTool->dropSchema($classes); - } catch (\Exception $e) { - } - - try { - $schemaTool->createSchema($classes); - } catch (\Exception $e) { - } - } - - protected function tearDown() - { - parent::tearDown(); - - $this->em = null; - } - - /** - * @expectedException \Symfony\Component\Form\Exception\StringCastException - * @expectedMessage Entity "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). - */ - public function testEntitiesMustHaveAToStringMethod() - { - $entity1 = new NoToStringSingleIdentEntity(1, 'Foo'); - $entity2 = new NoToStringSingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - null, - null, - array( - $entity1, - $entity2, - ) - ); - - $choiceList->getValues(); - } - - /** - * @expectedException \Symfony\Component\Form\Exception\RuntimeException - */ - public function testChoicesMustBeManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // no persist here! - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - $entity1, - $entity2, - ) - ); - - // triggers loading -> exception - $choiceList->getChoices(); - } - - public function testFlattenedChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - $entity1, - $entity2, - ) - ); - - $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); - } - - public function testEmptyChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array() - ); - - $this->assertSame(array(), $choiceList->getChoices()); - } - - public function testNestedChoicesAreManaged() - { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - - // Oh yeah, we're persisting with fire now! - $this->em->persist($entity1); - $this->em->persist($entity2); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name', - null, - array( - 'group1' => array($entity1), - 'group2' => array($entity2), - ), - array() - ); - - $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); - $this->assertEquals(array( - 'group1' => array(1 => new ChoiceView($entity1, '1', 'Foo')), - 'group2' => array(2 => new ChoiceView($entity2, '2', 'Bar')) - ), $choiceList->getRemainingViews()); - } - - public function testGroupBySupportsString() - { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - $item3 = new ItemGroupEntity(3, 'Baz', 'Group2'); - $item4 = new ItemGroupEntity(4, 'Boo!', null); - - $this->em->persist($item1); - $this->em->persist($item2); - $this->em->persist($item3); - $this->em->persist($item4); - - $choiceList = new EntityChoiceList( - $this->em, - self::ITEM_GROUP_CLASS, - 'name', - null, - array( - $item1, - $item2, - $item3, - $item4, - ), - array(), - 'groupName' - ); - - $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices()); - $this->assertEquals(array( - 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')), - 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')), - 4 => new ChoiceView($item4, '4', 'Boo!') - ), $choiceList->getRemainingViews()); - } - - public function testGroupByInvalidPropertyPathReturnsFlatChoices() - { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - - $this->em->persist($item1); - $this->em->persist($item2); - - $choiceList = new EntityChoiceList( - $this->em, - self::ITEM_GROUP_CLASS, - 'name', - null, - array( - $item1, - $item2, - ), - array(), - 'child.that.does.not.exist' - ); - - $this->assertEquals(array( - 1 => $item1, - 2 => $item2 - ), $choiceList->getChoices()); - } - - public function testPossibleToProvideShorthandEntityName() - { - $shorthandName = 'SymfonyTestsDoctrine:SingleIdentEntity'; - - $item1 = new SingleIdentEntity(1, 'Foo'); - $item2 = new SingleIdentEntity(2, 'Bar'); - - $this->em->persist($item1); - $this->em->persist($item2); - - $choiceList = new EntityChoiceList( - $this->em, - $shorthandName, - null, - null, - null, - array(), - null - ); - - $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); - $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); - } - - // Ticket #3446 - public function testGetEmptyArrayChoicesForEmptyValues() - { - $qb = $this->em->createQueryBuilder()->select('s')->from(self::SINGLE_IDENT_CLASS, 's'); - $entityLoader = new ORMQueryBuilderLoader($qb); - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - null, - $entityLoader - ); - - $this->assertEquals(array(), $choiceList->getChoicesForValues(array())); - } - - // https://github.com/symfony/symfony/issues/3635 - public function testSingleNonIntIdFallsBackToGeneration() - { - $entity1 = new SingleStringIdentEntity('Id 1', 'Foo'); - $entity2 = new SingleStringIdentEntity('Id 2', 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_STRING_IDENT_CLASS, - 'name' - ); - - $this->assertSame(array(0 => $entity1, 1 => $entity2), $choiceList->getChoices()); - } - - public function testMinusReplacedByUnderscoreInNegativeIntIds() - { - $entity1 = new SingleIdentEntity(-1, 'Foo'); - $entity2 = new SingleIdentEntity(1, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name' - ); - - $this->assertSame(array('_1' => $entity1, 1 => $entity2), $choiceList->getChoices()); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); - } - - public function testMinusReplacedByUnderscoreIfNotLoaded() - { - $entity1 = new SingleIdentEntity(-1, 'Foo'); - $entity2 = new SingleIdentEntity(1, 'Bar'); - - // Persist for managed state - $this->em->persist($entity1); - $this->em->persist($entity2); - $this->em->flush(); - - $choiceList = new EntityChoiceList( - $this->em, - self::SINGLE_IDENT_CLASS, - 'name' - ); - - // no getChoices()! - - $this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2))); - $this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1'))); - } -} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php new file mode 100644 index 0000000000..c5910195ca --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php @@ -0,0 +1,286 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity; +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Component\Form\Extension\Core\View\ChoiceView; +use Doctrine\ORM\Tools\SchemaTool; + +class GenericEntityChoiceListTest extends \PHPUnit_Framework_TestCase +{ + const SINGLE_INT_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + + const SINGLE_STRING_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + + const COMPOSITE_ID_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + + const GROUPABLE_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity'; + + /** + * @var \Doctrine\ORM\EntityManager + */ + private $em; + + protected function setUp() + { + if (!class_exists('Symfony\Component\Form\Form')) { + $this->markTestSkipped('The "Form" component is not available'); + } + + if (!class_exists('Doctrine\DBAL\Platforms\MySqlPlatform')) { + $this->markTestSkipped('Doctrine DBAL is not available.'); + } + + if (!class_exists('Doctrine\Common\Version')) { + $this->markTestSkipped('Doctrine Common is not available.'); + } + + if (!class_exists('Doctrine\ORM\EntityManager')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $this->em = DoctrineTestHelper::createTestEntityManager(); + + $schemaTool = new SchemaTool($this->em); + $classes = array( + $this->em->getClassMetadata(self::SINGLE_INT_ID_CLASS), + $this->em->getClassMetadata(self::SINGLE_STRING_ID_CLASS), + $this->em->getClassMetadata(self::COMPOSITE_ID_CLASS), + $this->em->getClassMetadata(self::GROUPABLE_CLASS), + ); + + try { + $schemaTool->dropSchema($classes); + } catch (\Exception $e) { + } + + try { + $schemaTool->createSchema($classes); + } catch (\Exception $e) { + } + + parent::setUp(); + } + + protected function tearDown() + { + parent::tearDown(); + + $this->em = null; + } + + /** + * @expectedException \Symfony\Component\Form\Exception\StringCastException + * @expectedMessage Entity "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). + */ + public function testEntitiesMustHaveAToStringMethod() + { + $entity1 = new SingleIntIdNoToStringEntity(1, 'Foo'); + $entity2 = new SingleIntIdNoToStringEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + null, + null, + array( + $entity1, + $entity2, + ) + ); + + $choiceList->getValues(); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\RuntimeException + */ + public function testChoicesMustBeManaged() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // no persist here! + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + $entity1, + $entity2, + ) + ); + + // triggers loading -> exception + $choiceList->getChoices(); + } + + public function testInitExplicitChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + $entity1, + $entity2, + ) + ); + + $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); + } + + public function testInitEmptyChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array() + ); + + $this->assertSame(array(), $choiceList->getChoices()); + } + + public function testInitNestedChoices() + { + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + + // Oh yeah, we're persisting with fire now! + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_INT_ID_CLASS, + 'name', + null, + array( + 'group1' => array($entity1), + 'group2' => array($entity2), + ), + array() + ); + + $this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices()); + $this->assertEquals(array( + 'group1' => array(1 => new ChoiceView($entity1, '1', 'Foo')), + 'group2' => array(2 => new ChoiceView($entity2, '2', 'Bar')) + ), $choiceList->getRemainingViews()); + } + + public function testGroupByPropertyPath() + { + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + $item3 = new GroupableEntity(3, 'Baz', 'Group2'); + $item4 = new GroupableEntity(4, 'Boo!', null); + + $this->em->persist($item1); + $this->em->persist($item2); + $this->em->persist($item3); + $this->em->persist($item4); + + $choiceList = new EntityChoiceList( + $this->em, + self::GROUPABLE_CLASS, + 'name', + null, + array( + $item1, + $item2, + $item3, + $item4, + ), + array(), + 'groupName' + ); + + $this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices()); + $this->assertEquals(array( + 'Group1' => array(1 => new ChoiceView($item1, '1', 'Foo'), 2 => new ChoiceView($item2, '2', 'Bar')), + 'Group2' => array(3 => new ChoiceView($item3, '3', 'Baz')), + 4 => new ChoiceView($item4, '4', 'Boo!') + ), $choiceList->getRemainingViews()); + } + + public function testGroupByInvalidPropertyPathReturnsFlatChoices() + { + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + + $this->em->persist($item1); + $this->em->persist($item2); + + $choiceList = new EntityChoiceList( + $this->em, + self::GROUPABLE_CLASS, + 'name', + null, + array( + $item1, + $item2, + ), + array(), + 'child.that.does.not.exist' + ); + + $this->assertEquals(array( + 1 => $item1, + 2 => $item2 + ), $choiceList->getChoices()); + } + + public function testInitShorthandEntityName() + { + $item1 = new SingleIntIdEntity(1, 'Foo'); + $item2 = new SingleIntIdEntity(2, 'Bar'); + + $this->em->persist($item1); + $this->em->persist($item2); + + $choiceList = new EntityChoiceList( + $this->em, + 'SymfonyTestsDoctrine:SingleIntIdEntity' + ); + + $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); + $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000..90cbf1d7c8 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListCompositeIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000..52d04c3879 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000..690d4b3d23 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/LoadedEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class LoadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $list = parent::createChoiceList(); + + // load list + $list->getChoices(); + + return $list; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php new file mode 100644 index 0000000000..5740a2ff94 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php new file mode 100644 index 0000000000..9c72ccccd9 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListCompositeIdWithQueryBuilderTest extends UnloadedEntityChoiceListCompositeIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php new file mode 100644 index 0000000000..a876878415 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php new file mode 100644 index 0000000000..fa5bb80ae7 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleIntIdWithQueryBuilderTest extends UnloadedEntityChoiceListSingleIntIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php new file mode 100644 index 0000000000..5b25b49a71 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest +{ + public function testGetIndicesForValuesIgnoresNonExistingValues() + { + $this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.'); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php new file mode 100644 index 0000000000..9fba5b9295 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; + +use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; + +/** + * @author Bernhard Schussek + */ +class UnloadedEntityChoiceListSingleStringIdWithQueryBuilderTest extends UnloadedEntityChoiceListSingleStringIdTest +{ + /** + * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface + */ + protected function createChoiceList() + { + $qb = $this->em->createQueryBuilder()->select('s')->from($this->getEntityClass(), 's'); + $loader = new ORMQueryBuilderLoader($qb); + + return new EntityChoiceList($this->em, $this->getEntityClass(), null, $loader); + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php index c187608bc2..562bb8ee0e 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php @@ -73,7 +73,7 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase foreach ($ids as $id) { $name = 65 + chr($id % 57); - $this->em->persist(new SingleIdentEntity($id, $name)); + $this->em->persist(new SingleIntIdEntity($id, $name)); } $this->em->flush(); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php index f09c23ede9..b124cae6d5 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php @@ -11,14 +11,14 @@ namespace Symfony\Bridge\Doctrine\Tests\Form\Type; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity; +use Symfony\Component\Form\Test\TypeTestCase; +use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity; use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; use Doctrine\ORM\Tools\SchemaTool; use Doctrine\Common\Collections\ArrayCollection; @@ -26,11 +26,11 @@ use Symfony\Component\Form\Extension\Core\View\ChoiceView; class EntityTypeTest extends TypeTestCase { - const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity'; - const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; - const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdentEntity'; - const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'; - const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity'; + const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity'; + const SINGLE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; + const SINGLE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity'; + const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'; + const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdEntity'; /** * @var \Doctrine\ORM\EntityManager @@ -44,7 +44,23 @@ class EntityTypeTest extends TypeTestCase protected function setUp() { - $this->em = DoctrineOrmTestCase::createTestEntityManager(); + if (!class_exists('Symfony\Component\Form\Form')) { + $this->markTestSkipped('The "Form" component is not available'); + } + + if (!class_exists('Doctrine\DBAL\Platforms\MySqlPlatform')) { + $this->markTestSkipped('Doctrine DBAL is not available.'); + } + + if (!class_exists('Doctrine\Common\Version')) { + $this->markTestSkipped('Doctrine Common is not available.'); + } + + if (!class_exists('Doctrine\ORM\EntityManager')) { + $this->markTestSkipped('Doctrine ORM is not available.'); + } + + $this->em = DoctrineTestHelper::createTestEntityManager(); $this->emRegistry = $this->createRegistryMock('default', $this->em); parent::setUp(); @@ -105,8 +121,8 @@ class EntityTypeTest extends TypeTestCase public function testSetDataToUninitializedEntityWithNonRequired() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -122,8 +138,8 @@ class EntityTypeTest extends TypeTestCase public function testSetDataToUninitializedEntityWithNonRequiredToString() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -138,8 +154,8 @@ class EntityTypeTest extends TypeTestCase public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); $qb = $this->em->createQueryBuilder()->select('e')->from(self::SINGLE_IDENT_CLASS, 'e'); @@ -267,8 +283,8 @@ class EntityTypeTest extends TypeTestCase public function testSubmitSingleNonExpandedSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -289,8 +305,8 @@ class EntityTypeTest extends TypeTestCase public function testSubmitSingleNonExpandedCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -312,9 +328,9 @@ class EntityTypeTest extends TypeTestCase public function testSubmitMultipleNonExpandedSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -337,9 +353,9 @@ class EntityTypeTest extends TypeTestCase public function testSubmitMultipleNonExpandedSingleIdentifierForExistingData() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -368,9 +384,9 @@ class EntityTypeTest extends TypeTestCase public function testSubmitMultipleNonExpandedCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -394,9 +410,9 @@ class EntityTypeTest extends TypeTestCase public function testSubmitMultipleNonExpandedCompositeIdentifierExistingData() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -425,8 +441,8 @@ class EntityTypeTest extends TypeTestCase public function testSubmitSingleExpanded() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); $this->persist(array($entity1, $entity2)); @@ -450,9 +466,9 @@ class EntityTypeTest extends TypeTestCase public function testSubmitMultipleExpanded() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Bar'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Bar'); $this->persist(array($entity1, $entity2, $entity3)); @@ -480,9 +496,9 @@ class EntityTypeTest extends TypeTestCase public function testOverrideChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -504,10 +520,10 @@ class EntityTypeTest extends TypeTestCase public function testGroupByChoices() { - $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); - $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); - $item3 = new ItemGroupEntity(3, 'Baz', 'Group2'); - $item4 = new ItemGroupEntity(4, 'Boo!', null); + $item1 = new GroupableEntity(1, 'Foo', 'Group1'); + $item2 = new GroupableEntity(2, 'Bar', 'Group1'); + $item3 = new GroupableEntity(3, 'Baz', 'Group2'); + $item4 = new GroupableEntity(4, 'Boo!', null); $this->persist(array($item1, $item2, $item3, $item4)); @@ -531,9 +547,9 @@ class EntityTypeTest extends TypeTestCase public function testPreferredChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -550,9 +566,9 @@ class EntityTypeTest extends TypeTestCase public function testOverrideChoicesWithPreferredChoices() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -570,9 +586,9 @@ class EntityTypeTest extends TypeTestCase public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -591,9 +607,9 @@ class EntityTypeTest extends TypeTestCase public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -612,9 +628,9 @@ class EntityTypeTest extends TypeTestCase public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -636,9 +652,9 @@ class EntityTypeTest extends TypeTestCase public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingleIdentifier() { - $entity1 = new SingleIdentEntity(1, 'Foo'); - $entity2 = new SingleIdentEntity(2, 'Bar'); - $entity3 = new SingleIdentEntity(3, 'Baz'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Bar'); + $entity3 = new SingleIntIdEntity(3, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -660,9 +676,9 @@ class EntityTypeTest extends TypeTestCase public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompositeIdentifier() { - $entity1 = new CompositeIdentEntity(10, 20, 'Foo'); - $entity2 = new CompositeIdentEntity(30, 40, 'Bar'); - $entity3 = new CompositeIdentEntity(50, 60, 'Baz'); + $entity1 = new CompositeIntIdEntity(10, 20, 'Foo'); + $entity2 = new CompositeIntIdEntity(30, 40, 'Bar'); + $entity3 = new CompositeIntIdEntity(50, 60, 'Baz'); $this->persist(array($entity1, $entity2, $entity3)); @@ -684,7 +700,7 @@ class EntityTypeTest extends TypeTestCase public function testSubmitSingleStringIdentifier() { - $entity1 = new SingleStringIdentEntity('foo', 'Foo'); + $entity1 = new SingleStringIdEntity('foo', 'Foo'); $this->persist(array($entity1)); @@ -705,7 +721,7 @@ class EntityTypeTest extends TypeTestCase public function testSubmitCompositeStringIdentifier() { - $entity1 = new CompositeStringIdentEntity('foo1', 'foo2', 'Foo'); + $entity1 = new CompositeStringIdEntity('foo1', 'foo2', 'Foo'); $this->persist(array($entity1)); diff --git a/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php index 8b60f5e340..8c179cd31f 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php @@ -11,26 +11,26 @@ namespace Symfony\Bridge\Doctrine\Tests\Security\User; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\User; use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider; use Doctrine\ORM\Tools\SchemaTool; -class EntityUserProviderTest extends DoctrineOrmTestCase +class EntityUserProviderTest extends \PHPUnit_Framework_TestCase { public function testRefreshUserGetsUserByPrimaryKey() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); - $user2 = new CompositeIdentEntity(1, 2, 'user2'); + $user1 = new User(1, 1, 'user1'); + $user2 = new User(1, 2, 'user2'); $em->persist($user1); $em->persist($user2); $em->flush(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); // try to change the user identity $user1->name = 'user2'; @@ -40,10 +40,10 @@ class EntityUserProviderTest extends DoctrineOrmTestCase public function testRefreshUserRequiresId() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); - $user1 = new CompositeIdentEntity(null, null, 'user1'); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $user1 = new User(null, null, 'user1'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); $this->setExpectedException( 'InvalidArgumentException', @@ -54,17 +54,17 @@ class EntityUserProviderTest extends DoctrineOrmTestCase public function testRefreshInvalidUser() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); + $user1 = new User(1, 1, 'user1'); $em->persist($user1); $em->flush(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); - $user2 = new CompositeIdentEntity(1, 2, 'user2'); + $user2 = new User(1, 2, 'user2'); $this->setExpectedException( 'Symfony\Component\Security\Core\Exception\UsernameNotFoundException', 'User with id {"id1":1,"id2":2} not found' @@ -74,18 +74,18 @@ class EntityUserProviderTest extends DoctrineOrmTestCase public function testSupportProxy() { - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); - $user1 = new CompositeIdentEntity(1, 1, 'user1'); + $user1 = new User(1, 1, 'user1'); $em->persist($user1); $em->flush(); $em->clear(); - $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', 'name'); + $provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name'); - $user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity', array('id1' => 1, 'id2' => 1)); + $user2 = $em->getReference('Symfony\Bridge\Doctrine\Tests\Fixtures\User', array('id1' => 1, 'id2' => 1)); $this->assertTrue($provider->supportsClass(get_class($user2))); } @@ -104,7 +104,7 @@ class EntityUserProviderTest extends DoctrineOrmTestCase { $schemaTool = new SchemaTool($em); $schemaTool->createSchema(array( - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\User'), )); } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php index f7748258ad..55e8d5f7b6 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php @@ -11,12 +11,12 @@ namespace Symfony\Bridge\Doctrine\Tests\Validator\Constraints; -use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; +use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; +use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity; use Symfony\Component\Validator\DefaultTranslator; use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; -use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity; -use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity; use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; @@ -24,7 +24,7 @@ use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Validator; use Doctrine\ORM\Tools\SchemaTool; -class UniqueValidatorTest extends DoctrineOrmTestCase +class UniqueValidatorTest extends \PHPUnit_Framework_TestCase { protected function createRegistryMock($entityManagerName, $em) { @@ -96,7 +96,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy', $ignoreNull = true) { if (!$validateClass) { - $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'; + $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'; } if (!$uniqueFields) { $uniqueFields = array('name'); @@ -127,9 +127,9 @@ class UniqueValidatorTest extends DoctrineOrmTestCase { $schemaTool = new SchemaTool($em); $schemaTool->createSchema(array( - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'), - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity'), - $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'), + $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'), $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'), )); } @@ -140,11 +140,11 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateUniqueness() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database."); @@ -154,7 +154,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database."); - $entity2 = new SingleIdentEntity(2, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Foo'); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -168,16 +168,16 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateCustomErrorPath() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, null, null, 'bar'); - $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity1 = new SingleIntIdEntity(1, 'Foo'); $em->persist($entity1); $em->flush(); - $entity2 = new SingleIdentEntity(2, 'Foo'); + $entity2 = new SingleIntIdEntity(2, 'Foo'); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -191,12 +191,12 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateUniquenessWithNull() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, null); - $entity2 = new SingleIdentEntity(2, null); + $entity1 = new SingleIntIdEntity(1, null); + $entity2 = new SingleIntIdEntity(2, null); $em->persist($entity1); $em->persist($entity2); @@ -209,12 +209,12 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateUniquenessWithIgnoreNull() { $entityManagerName = "foo"; - $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity'; - $em = $this->createTestEntityManager(); + $validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'; + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, $validateClass, array('name', 'name2'), 'bar', 'findby', false); - $entity1 = new DoubleIdentEntity(1, 'Foo', null); + $entity1 = new DoubleNameEntity(1, 'Foo', null); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database."); @@ -224,7 +224,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database."); - $entity2 = new DoubleIdentEntity(2, 'Foo', null); + $entity2 = new DoubleNameEntity(2, 'Foo', null); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database."); @@ -238,12 +238,12 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateUniquenessAfterConsideringMultipleQueryResults() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em); - $entity1 = new SingleIdentEntity(1, 'foo'); - $entity2 = new SingleIdentEntity(2, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); + $entity2 = new SingleIntIdEntity(2, 'foo'); $em->persist($entity1); $em->persist($entity2); @@ -267,7 +267,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase $em = $this->createEntityManagerMock($repository); $validator = $this->createValidator($entityManagerName, $em, null, array(), null, 'findByCustom'); - $entity1 = new SingleIdentEntity(1, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), 'Violation is using custom repository method.'); @@ -275,7 +275,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testValidateUniquenessWithUnrewoundArray() { - $entity = new SingleIdentEntity(1, 'foo'); + $entity = new SingleIntIdEntity(1, 'foo'); $entityManagerName = 'foo'; $repository = $this->createRepositoryMock(); @@ -305,11 +305,11 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testAssociatedEntity() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity', array('single')); - $entity1 = new SingleIdentEntity(1, 'foo'); + $entity1 = new SingleIntIdEntity(1, 'foo'); $associated = new AssociationEntity(); $associated->single = $entity1; @@ -336,11 +336,11 @@ class UniqueValidatorTest extends DoctrineOrmTestCase public function testAssociatedCompositeEntity() { $entityManagerName = "foo"; - $em = $this->createTestEntityManager(); + $em = DoctrineTestHelper::createTestEntityManager(); $this->createSchema($em); $validator = $this->createValidator($entityManagerName, $em, 'Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity', array('composite')); - $composite = new CompositeIdentEntity(1, 1, "test"); + $composite = new CompositeIntIdEntity(1, 1, "test"); $associated = new AssociationEntity(); $associated->composite = $composite;