This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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);
}
}

View File

@@ -0,0 +1,91 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Tests\Fixtures\SingleAssociationToIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* Test choices generated from an entity with a primary foreign key.
*
* @author Premi Giorgio <giosh94mhz@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
abstract class AbstractEntityChoiceListSingleAssociationToIntIdTest extends AbstractEntityChoiceListTest
{
protected function getEntityClass()
{
return 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleAssociationToIntIdEntity';
}
protected function getClassesMetadata()
{
return array(
$this->em->getClassMetadata($this->getEntityClass()),
$this->em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity'),
);
}
protected function createChoiceList()
{
return new EntityChoiceList($this->em, $this->getEntityClass(), 'name');
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createObjects()
{
$innerA = new SingleIntIdNoToStringEntity(-10, 'inner_A');
$innerB = new SingleIntIdNoToStringEntity(10, 'inner_B');
$innerC = new SingleIntIdNoToStringEntity(20, 'inner_C');
$innerD = new SingleIntIdNoToStringEntity(30, 'inner_D');
$this->em->persist($innerA);
$this->em->persist($innerB);
$this->em->persist($innerC);
$this->em->persist($innerD);
return array(
new SingleAssociationToIntIdEntity($innerA, 'A'),
new SingleAssociationToIntIdEntity($innerB, 'B'),
new SingleAssociationToIntIdEntity($innerC, 'C'),
new SingleAssociationToIntIdEntity($innerD, '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);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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);
}
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractEntityChoiceListTest extends AbstractChoiceListTest
{
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $em;
protected $obj1;
protected $obj2;
protected $obj3;
protected $obj4;
protected function setUp()
{
$this->em = DoctrineTestHelper::createTestEntityManager();
$schemaTool = new SchemaTool($this->em);
$classes = $this->getClassesMetadata();
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();
protected function getClassesMetadata()
{
return array($this->em->getClassMetadata($this->getEntityClass()));
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new EntityChoiceList($this->em, $this->getEntityClass());
}
}

View File

@@ -0,0 +1,460 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class DoctrineChoiceLoaderTest extends TestCase
{
/**
* @var ChoiceListFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $factory;
/**
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $om;
/**
* @var ObjectRepository|\PHPUnit_Framework_MockObject_MockObject
*/
private $repository;
/**
* @var string
*/
private $class;
/**
* @var IdReader|\PHPUnit_Framework_MockObject_MockObject
*/
private $idReader;
/**
* @var EntityLoaderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $objectLoader;
/**
* @var \stdClass
*/
private $obj1;
/**
* @var \stdClass
*/
private $obj2;
/**
* @var \stdClass
*/
private $obj3;
protected function setUp()
{
$this->factory = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface')->getMock();
$this->om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock();
$this->repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')->getMock();
$this->class = 'stdClass';
$this->idReader = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader')
->disableOriginalConstructor()
->getMock();
$this->objectLoader = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface')->getMock();
$this->obj1 = (object) array('name' => 'A');
$this->obj2 = (object) array('name' => 'B');
$this->obj3 = (object) array('name' => 'C');
$this->om->expects($this->any())
->method('getRepository')
->with($this->class)
->willReturn($this->repository);
$this->om->expects($this->any())
->method('getClassMetadata')
->with($this->class)
->willReturn(new ClassMetadata($this->class));
}
public function testLoadChoiceList()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array());
$value = function () {};
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame($choiceList, $loader->loadChoiceList($value));
// no further loads on subsequent calls
$this->assertSame($choiceList, $loader->loadChoiceList($value));
}
public function testLoadChoiceListUsesObjectLoaderIfAvailable()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
$this->objectLoader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array());
$this->repository->expects($this->never())
->method('findAll');
$this->objectLoader->expects($this->once())
->method('getEntities')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame($choiceList, $loader->loadChoiceList());
// no further loads on subsequent calls
$this->assertSame($choiceList, $loader->loadChoiceList());
}
public function testLoadValuesForChoices()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame(array('1', '2'), $loader->loadValuesForChoices(array($this->obj2, $this->obj3)));
// no further loads on subsequent calls
$this->assertSame(array('1', '2'), $loader->loadValuesForChoices(array($this->obj2, $this->obj3)));
}
public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->assertSame(array(), $loader->loadValuesForChoices(array()));
}
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->idReader->expects($this->any())
->method('getIdValue')
->with($this->obj2)
->willReturn('2');
$this->assertSame(array('2'), $loader->loadValuesForChoices(array($this->obj2)));
}
public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame(array('B'), $loader->loadValuesForChoices(
array($this->obj2),
$value
));
}
public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$value = array($this->idReader, 'getIdValue');
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->idReader->expects($this->any())
->method('getIdValue')
->with($this->obj2)
->willReturn('2');
$this->assertSame(array('2'), $loader->loadValuesForChoices(
array($this->obj2),
$value
));
}
public function testLoadChoicesForValues()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame(array($this->obj2, $this->obj3), $loader->loadChoicesForValues(array('1', '2')));
// no further loads on subsequent calls
$this->assertSame(array($this->obj2, $this->obj3), $loader->loadChoicesForValues(array('1', '2')));
}
public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->assertSame(array(), $loader->loadChoicesForValues(array()));
}
public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
$this->objectLoader
);
$choices = array($this->obj2, $this->obj3);
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->idReader->expects($this->any())
->method('getIdField')
->willReturn('idField');
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->objectLoader->expects($this->once())
->method('getEntitiesByIds')
->with('idField', array(4 => '3', 7 => '2'))
->willReturn($choices);
$this->idReader->expects($this->any())
->method('getIdValue')
->willReturnMap(array(
array($this->obj2, '2'),
array($this->obj3, '3'),
));
$this->assertSame(
array(4 => $this->obj3, 7 => $this->obj2),
$loader->loadChoicesForValues(array(4 => '3', 7 => '2')
));
}
public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame(array($this->obj2), $loader->loadChoicesForValues(
array('B'),
$value
));
}
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
$this->objectLoader
);
$choices = array($this->obj2, $this->obj3);
$value = array($this->idReader, 'getIdValue');
$this->idReader->expects($this->any())
->method('isSingleId')
->willReturn(true);
$this->idReader->expects($this->any())
->method('getIdField')
->willReturn('idField');
$this->repository->expects($this->never())
->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->objectLoader->expects($this->once())
->method('getEntitiesByIds')
->with('idField', array('2'))
->willReturn($choices);
$this->idReader->expects($this->any())
->method('getIdValue')
->willReturnMap(array(
array($this->obj2, '2'),
array($this->obj3, '3'),
));
$this->assertSame(array($this->obj2), $loader->loadChoicesForValues(array('2'), $value));
}
}

View File

@@ -0,0 +1,289 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
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\Component\Form\Extension\Core\View\ChoiceView;
/**
* @group legacy
*/
class GenericEntityChoiceListTest extends 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()
{
$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)));
}
public function testInitShorthandEntityName2()
{
$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->getIndicesForChoices(array($item1, $item2)));
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LoadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest
{
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
$list = parent::createChoiceList();
// load list
$list->getChoices();
return $list;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Premi Giorgio <giosh94mhz@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LoadedEntityChoiceListSingleAssociationToIntIdTest extends AbstractEntityChoiceListSingleAssociationToIntIdTest
{
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
$list = parent::createChoiceList();
// load list
$list->getChoices();
return $list;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LoadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest
{
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
$list = parent::createChoiceList();
// load list
$list->getChoices();
return $list;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LoadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest
{
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
$list = parent::createChoiceList();
// load list
$list->getChoices();
return $list;
}
}

View File

@@ -0,0 +1,183 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 Doctrine\DBAL\Connection;
use Doctrine\ORM\Version;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
class ORMQueryBuilderLoaderTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @group legacy
*/
public function testItOnlyWorksWithQueryBuilderOrClosure()
{
new ORMQueryBuilderLoader(new \stdClass());
}
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @group legacy
*/
public function testClosureRequiresTheEntityManager()
{
$closure = function () {};
new ORMQueryBuilderLoader($closure);
}
public function testIdentifierTypeIsStringArray()
{
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity', Connection::PARAM_STR_ARRAY);
}
public function testIdentifierTypeIsIntegerArray()
{
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity', Connection::PARAM_INT_ARRAY);
}
protected function checkIdentifierType($classname, $expectedType)
{
$em = DoctrineTestHelper::createTestEntityManager();
$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
->getMock();
$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array(1, 2), $expectedType)
->willReturn($query);
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
->setMethods(array('getQuery'))
->getMock();
$qb->expects($this->once())
->method('getQuery')
->willReturn($query);
$qb->select('e')
->from($classname, 'e');
$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', array(1, 2));
}
public function testFilterNonIntegerValues()
{
$em = DoctrineTestHelper::createTestEntityManager();
$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
->getMock();
$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array(1, 2, 3, '9223372036854775808'), Connection::PARAM_INT_ARRAY)
->willReturn($query);
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
->setMethods(array('getQuery'))
->getMock();
$qb->expects($this->once())
->method('getQuery')
->willReturn($query);
$qb->select('e')
->from('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity', 'e');
$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', array(1, '', 2, 3, 'foo', '9223372036854775808'));
}
/**
* @dataProvider provideGuidEntityClasses
*/
public function testFilterEmptyUuids($entityClass)
{
$em = DoctrineTestHelper::createTestEntityManager();
$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
->getMock();
$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array('71c5fd46-3f16-4abb-bad7-90ac1e654a2d', 'b98e8e11-2897-44df-ad24-d2627eb7f499'), Connection::PARAM_STR_ARRAY)
->willReturn($query);
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
->setMethods(array('getQuery'))
->getMock();
$qb->expects($this->once())
->method('getQuery')
->willReturn($query);
$qb->select('e')
->from($entityClass, 'e');
$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', array('71c5fd46-3f16-4abb-bad7-90ac1e654a2d', '', 'b98e8e11-2897-44df-ad24-d2627eb7f499'));
}
public function testEmbeddedIdentifierName()
{
if (Version::compare('2.5.0') > 0) {
$this->markTestSkipped('Applicable only for Doctrine >= 2.5.0');
return;
}
$em = DoctrineTestHelper::createTestEntityManager();
$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
->getMock();
$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id_value', array(1, 2, 3), Connection::PARAM_INT_ARRAY)
->willReturn($query);
$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
->setMethods(array('getQuery'))
->getMock();
$qb->expects($this->once())
->method('getQuery')
->willReturn($query);
$qb->select('e')
->from('Symfony\Bridge\Doctrine\Tests\Fixtures\EmbeddedIdentifierEntity', 'e');
$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id.value', array(1, '', 2, 3, 'foo'));
}
public function provideGuidEntityClasses()
{
return array(
array('Symfony\Bridge\Doctrine\Tests\Fixtures\GuidIdEntity'),
array('Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'),
);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class UnloadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
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);
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Premi Giorgio <giosh94mhz@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class UnloadedEntityChoiceListSingleAssociationToIntIdTest extends AbstractEntityChoiceListSingleAssociationToIntIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
/**
* @group legacy
*/
public function testLegacyGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Premi Giorgio <giosh94mhz@gmail.com>
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class UnloadedEntityChoiceListSingleAssociationToIntIdWithQueryBuilderTest extends UnloadedEntityChoiceListSingleAssociationToIntIdTest
{
/**
* @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);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class UnloadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
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);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class UnloadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
if (!class_exists('Symfony\Component\Form\Tests\Extension\Core\ChoiceList\AbstractChoiceListTest')) {
return;
}
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
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);
}
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\DataTransformer;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class CollectionToArrayTransformerTest extends TestCase
{
/**
* @var CollectionToArrayTransformer
*/
private $transformer;
protected function setUp()
{
$this->transformer = new CollectionToArrayTransformer();
}
public function testTransform()
{
$array = array(
2 => 'foo',
3 => 'bar',
);
$this->assertSame($array, $this->transformer->transform(new ArrayCollection($array)));
}
/**
* This test is needed for cases when getXxxs() in the entity returns the
* result of $collection->toArray(), in order to prevent modifications of
* the inner collection.
*
* See https://github.com/symfony/symfony/pull/9308
*/
public function testTransformArray()
{
$array = array(
2 => 'foo',
3 => 'bar',
);
$this->assertSame($array, $this->transformer->transform($array));
}
public function testTransformNull()
{
$this->assertSame(array(), $this->transformer->transform(null));
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testTransformExpectsArrayOrCollection()
{
$this->transformer->transform('Foo');
}
public function testReverseTransform()
{
$array = array(
2 => 'foo',
3 => 'bar',
);
$this->assertEquals(new ArrayCollection($array), $this->transformer->reverseTransform($array));
}
public function testReverseTransformEmpty()
{
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(''));
}
public function testReverseTransformNull()
{
$this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(null));
}
}

View File

@@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\ValueGuess;
class DoctrineOrmTypeGuesserTest extends TestCase
{
/**
* @dataProvider requiredProvider
*/
public function testRequiredGuesser($classMetadata, $expected)
{
$this->assertEquals($expected, $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}
public function requiredProvider()
{
$return = array();
// Simple field, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(false));
$return[] = array($classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE));
// Simple field, nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(true));
$return[] = array($classMetadata, new ValueGuess(false, Guess::MEDIUM_CONFIDENCE));
// One-to-one, nullable (by default)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$mapping = array('joinColumns' => array(array()));
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->will($this->returnValue($mapping));
$return[] = array($classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE));
// One-to-one, nullable (explicit)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$mapping = array('joinColumns' => array(array('nullable' => true)));
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->will($this->returnValue($mapping));
$return[] = array($classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE));
// One-to-one, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$mapping = array('joinColumns' => array(array('nullable' => false)));
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->will($this->returnValue($mapping));
$return[] = array($classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE));
// One-to-many, no clue
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(false));
$return[] = array($classMetadata, null);
return $return;
}
private function getGuesser(ClassMetadata $classMetadata)
{
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock();
$em->expects($this->once())->method('getClassMetaData')->with('TestEntity')->will($this->returnValue($classMetadata));
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
$registry->expects($this->once())->method('getManagers')->will($this->returnValue(array($em)));
return new DoctrineOrmTypeGuesser($registry);
}
}

View File

@@ -0,0 +1,139 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Type;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Test\FormPerformanceTestCase;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class EntityTypePerformanceTest extends FormPerformanceTestCase
{
const ENTITY_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity';
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
protected function getExtensions()
{
$manager = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
$manager->expects($this->any())
->method('getManager')
->will($this->returnValue($this->em));
$manager->expects($this->any())
->method('getManagerForClass')
->will($this->returnValue($this->em));
return array(
new CoreExtension(),
new DoctrineOrmExtension($manager),
);
}
protected function setUp()
{
$this->em = DoctrineTestHelper::createTestEntityManager();
parent::setUp();
$schemaTool = new SchemaTool($this->em);
$classes = array(
$this->em->getClassMetadata(self::ENTITY_CLASS),
);
try {
$schemaTool->dropSchema($classes);
} catch (\Exception $e) {
}
try {
$schemaTool->createSchema($classes);
} catch (\Exception $e) {
}
$ids = range(1, 300);
foreach ($ids as $id) {
$name = 65 + (int) \chr($id % 57);
$this->em->persist(new SingleIntIdEntity($id, $name));
}
$this->em->flush();
}
/**
* This test case is realistic in collection forms where each
* row contains the same entity field.
*
* @group benchmark
*/
public function testCollapsedEntityField()
{
$this->setMaxRunningTime(1);
for ($i = 0; $i < 40; ++$i) {
$form = $this->factory->create('Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'class' => self::ENTITY_CLASS,
));
// force loading of the choice list
$form->createView();
}
}
/**
* @group benchmark
*/
public function testCollapsedEntityFieldWithChoices()
{
$choices = $this->em->createQuery('SELECT c FROM '.self::ENTITY_CLASS.' c')->getResult();
$this->setMaxRunningTime(1);
for ($i = 0; $i < 40; ++$i) {
$form = $this->factory->create('Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'class' => self::ENTITY_CLASS,
'choices' => $choices,
));
// force loading of the choice list
$form->createView();
}
}
/**
* @group benchmark
*/
public function testCollapsedEntityFieldWithPreferredChoices()
{
$choices = $this->em->createQuery('SELECT c FROM '.self::ENTITY_CLASS.' c')->getResult();
$this->setMaxRunningTime(1);
for ($i = 0; $i < 40; ++$i) {
$form = $this->factory->create('Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'class' => self::ENTITY_CLASS,
'preferred_choices' => $choices,
));
// force loading of the choice list
$form->createView();
}
}
}

File diff suppressed because it is too large Load Diff