123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- <?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\Tests\Component\Form;
- require_once __DIR__.'/DoctrineOrmTestCase.php';
- require_once __DIR__.'/Fixtures/SingleIdentEntity.php';
- require_once __DIR__.'/Fixtures/CompositeIdentEntity.php';
- use Symfony\Component\Form\EntityChoiceField;
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- use Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity;
- use Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity;
- use Doctrine\ORM\Tools\SchemaTool;
- use Doctrine\Common\Collections\ArrayCollection;
- class EntityChoiceFieldTest extends DoctrineOrmTestCase
- {
- const SINGLE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity';
- const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity';
- /**
- * @var EntityManager
- */
- private $em;
- protected function setUp()
- {
- parent::setUp();
- $this->em = $this->createTestEntityManager();
- $schemaTool = new SchemaTool($this->em);
- $classes = array(
- $this->em->getClassMetadata(self::SINGLE_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 persist(array $entities)
- {
- foreach ($entities as $entity) {
- $this->em->persist($entity);
- }
- $this->em->flush();
- // no clear, because entities managed by the choice field must
- // be managed!
- }
- public function testNonRequiredContainsEmptyField()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $this->persist(array($entity1, $entity2));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'required' => false,
- 'property' => 'name'
- ));
- $this->assertEquals(array('' => '', 1 => 'Foo', 2 => 'Bar'), $field->getOtherChoices());
- }
- // public function testSetDataToUninitializedEntityWithNonRequired()
- // {
- // $entity1 = new SingleIdentEntity(1, 'Foo');
- // $entity2 = new SingleIdentEntity(2, 'Bar');
- //
- // $this->persist(array($entity1, $entity2));
- //
- // $field = new EntityChoiceField('name', array(
- // 'em' => $this->em,
- // 'class' => self::SINGLE_IDENT_CLASS,
- // 'required' => false,
- // 'property' => 'name'
- // ));
- //
- // $this->assertEquals(array('' => '', 1 => 'Foo', 2 => 'Bar'), $field->getOtherChoices());
- //
- // }
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
- */
- public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
- {
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'query_builder' => new \stdClass(),
- ));
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
- */
- public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
- {
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'query_builder' => function () {
- return new \stdClass();
- },
- ));
- $field->submit('2');
- }
- /**
- * @expectedException Symfony\Component\Form\Exception\FormException
- */
- public function testChoicesMustBeManaged()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- // no persist here!
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'choices' => array($entity1, $entity2),
- 'property' => 'name',
- ));
- }
- public function testSetDataSingle_null()
- {
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- ));
- $field->setData(null);
- $this->assertEquals(null, $field->getData());
- $this->assertEquals('', $field->getDisplayedData());
- }
- public function testSetDataMultiple_null()
- {
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- ));
- $field->setData(null);
- $this->assertEquals(null, $field->getData());
- $this->assertEquals(array(), $field->getDisplayedData());
- }
- public function testSubmitSingle_null()
- {
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- ));
- $field->submit(null);
- $this->assertEquals(null, $field->getData());
- $this->assertEquals('', $field->getDisplayedData());
- }
- public function testSubmitMultiple_null()
- {
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- ));
- $field->submit(null);
- $this->assertEquals(new ArrayCollection(), $field->getData());
- $this->assertEquals(array(), $field->getDisplayedData());
- }
- public function testSubmitSingleNonExpanded_singleIdentifier()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $this->persist(array($entity1, $entity2));
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $field->submit('2');
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($entity2, $field->getData());
- $this->assertEquals(2, $field->getDisplayedData());
- }
- public function testSubmitSingleNonExpanded_compositeIdentifier()
- {
- $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
- $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
- $this->persist(array($entity1, $entity2));
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::COMPOSITE_IDENT_CLASS,
- 'property' => 'name',
- ));
- // the collection key is used here
- $field->submit('1');
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($entity2, $field->getData());
- $this->assertEquals(1, $field->getDisplayedData());
- }
- public function testSubmitMultipleNonExpanded_singleIdentifier()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $field->submit(array('1', '3'));
- $expected = new ArrayCollection(array($entity1, $entity3));
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($expected, $field->getData());
- $this->assertEquals(array(1, 3), $field->getDisplayedData());
- }
- public function testSubmitMultipleNonExpanded_singleIdentifier_existingData()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $existing = new ArrayCollection(array($entity2));
- $field->setData($existing);
- $field->submit(array('1', '3'));
- // entry with index 0 was removed
- $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($expected, $field->getData());
- // same object still, useful if it is a PersistentCollection
- $this->assertSame($existing, $field->getData());
- $this->assertEquals(array(1, 3), $field->getDisplayedData());
- }
- public function testSubmitMultipleNonExpanded_compositeIdentifier()
- {
- $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
- $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
- $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::COMPOSITE_IDENT_CLASS,
- 'property' => 'name',
- ));
- // because of the composite key collection keys are used
- $field->submit(array('0', '2'));
- $expected = new ArrayCollection(array($entity1, $entity3));
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($expected, $field->getData());
- $this->assertEquals(array(0, 2), $field->getDisplayedData());
- }
- public function testSubmitMultipleNonExpanded_compositeIdentifier_existingData()
- {
- $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
- $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
- $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => false,
- 'em' => $this->em,
- 'class' => self::COMPOSITE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $existing = new ArrayCollection(array($entity2));
- $field->setData($existing);
- $field->submit(array('0', '2'));
- // entry with index 0 was removed
- $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($expected, $field->getData());
- // same object still, useful if it is a PersistentCollection
- $this->assertSame($existing, $field->getData());
- $this->assertEquals(array(0, 2), $field->getDisplayedData());
- }
- public function testSubmitSingleExpanded()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $this->persist(array($entity1, $entity2));
- $field = new EntityChoiceField('name', array(
- 'multiple' => false,
- 'expanded' => true,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $field->submit('2');
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($entity2, $field->getData());
- $this->assertSame(false, $field['1']->getData());
- $this->assertSame(true, $field['2']->getData());
- $this->assertSame('', $field['1']->getDisplayedData());
- $this->assertSame('1', $field['2']->getDisplayedData());
- $this->assertSame(array('1' => '', '2' => '1'), $field->getDisplayedData());
- }
- public function testSubmitMultipleExpanded()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Bar');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'multiple' => true,
- 'expanded' => true,
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'property' => 'name',
- ));
- $field->submit(array('1' => '1', '3' => '3'));
- $expected = new ArrayCollection(array($entity1, $entity3));
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($expected, $field->getData());
- $this->assertSame(true, $field['1']->getData());
- $this->assertSame(false, $field['2']->getData());
- $this->assertSame(true, $field['3']->getData());
- $this->assertSame('1', $field['1']->getDisplayedData());
- $this->assertSame('', $field['2']->getDisplayedData());
- $this->assertSame('1', $field['3']->getDisplayedData());
- $this->assertSame(array('1' => '1', '2' => '', '3' => '1'), $field->getDisplayedData());
- }
- public function testOverrideChoices()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- // not all persisted entities should be displayed
- 'choices' => array($entity1, $entity2),
- 'property' => 'name',
- ));
- $field->submit('2');
- $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->getOtherChoices());
- $this->assertTrue($field->isTransformationSuccessful());
- $this->assertEquals($entity2, $field->getData());
- $this->assertEquals(2, $field->getDisplayedData());
- }
- public function testDisallowChoicesThatAreNotIncluded_choices_singleIdentifier()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'choices' => array($entity1, $entity2),
- 'property' => 'name',
- ));
- $field->submit('3');
- $this->assertFalse($field->isTransformationSuccessful());
- $this->assertNull($field->getData());
- }
- public function testDisallowChoicesThatAreNotIncluded_choices_compositeIdentifier()
- {
- $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
- $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
- $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::COMPOSITE_IDENT_CLASS,
- 'choices' => array($entity1, $entity2),
- 'property' => 'name',
- ));
- $field->submit('2');
- $this->assertFalse($field->isTransformationSuccessful());
- $this->assertNull($field->getData());
- }
- public function testDisallowChoicesThatAreNotIncluded_queryBuilder_singleIdentifier()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'query_builder' => $repository->createQueryBuilder('e')
- ->where('e.id IN (1, 2)'),
- 'property' => 'name',
- ));
- $field->submit('3');
- $this->assertFalse($field->isTransformationSuccessful());
- $this->assertNull($field->getData());
- }
- public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_singleIdentifier()
- {
- $entity1 = new SingleIdentEntity(1, 'Foo');
- $entity2 = new SingleIdentEntity(2, 'Bar');
- $entity3 = new SingleIdentEntity(3, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::SINGLE_IDENT_CLASS,
- 'query_builder' => function ($repository) {
- return $repository->createQueryBuilder('e')
- ->where('e.id IN (1, 2)');
- },
- 'property' => 'name',
- ));
- $field->submit('3');
- $this->assertFalse($field->isTransformationSuccessful());
- $this->assertNull($field->getData());
- }
- public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_compositeIdentifier()
- {
- $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
- $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
- $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
- $this->persist(array($entity1, $entity2, $entity3));
- $field = new EntityChoiceField('name', array(
- 'em' => $this->em,
- 'class' => self::COMPOSITE_IDENT_CLASS,
- 'query_builder' => function ($repository) {
- return $repository->createQueryBuilder('e')
- ->where('e.id1 IN (10, 50)');
- },
- 'property' => 'name',
- ));
- $field->submit('2');
- $this->assertFalse($field->isTransformationSuccessful());
- $this->assertNull($field->getData());
- }
- }
|