Merge branch '2.3'

* 2.3:
  Fixed docblock in UserInterface::getSalt()
  [Process] Fix #8970 : read output once the process is finished, enable pipe tests on Windows
  [DoctrineBridge] Improved test coverage of EntityChoiceList
  [DoctrineBridge] Improved test coverage of EntityChoiceList
  [Form] Improved test coverage of ChoiceList classes
  [Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted
  [Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys
  [Form] Removed usage of the ChoiceList::getIndicesFor*() methods where they don't offer any performance benefit
  [Form] Improved test coverage of ChoiceList classes
  [Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted
  [Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys
  [Form] Removed usage of the ChoiceList::getIndicesFor*() methods where they don't offer any performance benefit
  Removed duplicate annotation
  [HttpKernel] made code more reliable
  [HttpFoundation] fixed regression in the way the request format is handled for duplicated requests (closes #8917)
  [HttpKernel] fixer HInclude src (closes #8951)
  Fixed escaping of service identifiers in configuration

Conflicts:
	src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/GenericEntityChoiceListTest.php
	src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
	src/Symfony/Component/HttpKernel/Tests/Fragment/HIncludeFragmentRendererTest.php
This commit is contained in:
Fabien Potencier 2013-09-10 22:30:47 +02:00
commit 95483e518d
63 changed files with 2196 additions and 903 deletions

View File

@ -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)));
}
}

View File

@ -0,0 +1,59 @@
<?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\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 <bschussek@gmail.com>
*/
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()
{
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -0,0 +1,41 @@
<?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\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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -0,0 +1,58 @@
<?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;
/**
* @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,58 @@
<?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;
/**
* @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,58 @@
<?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;
/**
* @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,100 @@
<?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\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 <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()
{
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());
}
}

View File

@ -1,354 +0,0 @@
<?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\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')));
}
}

View File

@ -0,0 +1,286 @@
<?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\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)));
}
}

View File

@ -0,0 +1,31 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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,31 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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,31 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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,23 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnloadedEntityChoiceListCompositeIdTest extends AbstractEntityChoiceListCompositeIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@ -0,0 +1,32 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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,23 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnloadedEntityChoiceListSingleIntIdTest extends AbstractEntityChoiceListSingleIntIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@ -0,0 +1,32 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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,23 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnloadedEntityChoiceListSingleStringIdTest extends AbstractEntityChoiceListSingleStringIdTest
{
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$this->markTestSkipped('Non-existing values are not detected for unloaded choice lists.');
}
}

View File

@ -0,0 +1,32 @@
<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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

@ -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();

View File

@ -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));

View File

@ -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'),
));
}
}

View File

@ -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;

View File

@ -82,7 +82,13 @@ class Configuration implements ConfigurationInterface
->prototype('array')
->beforeNormalization()
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
->then(function($v){
if (0 === strpos($v, '@@')) {
return substr($v, 1);
}
return array('id' => substr($v, 1), 'type' => 'service');
})
->end()
->beforeNormalization()
->ifTrue(function($v){

View File

@ -8,6 +8,7 @@ $container->loadFromExtension('twig', array(
),
'globals' => array(
'foo' => '@bar',
'baz' => '@@qux',
'pi' => 3.14,
'bad' => array('key' => 'foo'),
),

View File

@ -11,6 +11,7 @@
<twig:resource>MyBundle::form.html.twig</twig:resource>
</twig:form>
<twig:global key="foo" id="bar" type="service" />
<twig:global key="baz">@@qux</twig:global>
<twig:global key="pi">3.14</twig:global>
<twig:path>path1</twig:path>
<twig:path>path2</twig:path>

View File

@ -4,6 +4,7 @@ twig:
- MyBundle::form.html.twig
globals:
foo: "@bar"
baz: "@@qux"
pi: 3.14
bad: {key: foo}
auto_reload: true

View File

@ -63,13 +63,15 @@ class TwigExtensionTest extends TestCase
$this->assertEquals(new Reference('templating.globals'), $calls[0][1][1]);
$this->assertEquals('foo', $calls[1][1][0], '->load() registers services as Twig globals');
$this->assertEquals(new Reference('bar'), $calls[1][1][1], '->load() registers services as Twig globals');
$this->assertEquals('pi', $calls[2][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(3.14, $calls[2][1][1], '->load() registers variables as Twig globals');
$this->assertEquals('baz', $calls[2][1][0], '->load() registers variables as Twig globals');
$this->assertEquals('@qux', $calls[2][1][1], '->load() allows escaping of service identifiers');
$this->assertEquals('pi', $calls[3][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(3.14, $calls[3][1][1], '->load() registers variables as Twig globals');
// Yaml and Php specific configs
if (in_array($format, array('yml', 'php'))) {
$this->assertEquals('bad', $calls[3][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(array('key' => 'foo'), $calls[3][1][1], '->load() registers variables as Twig globals');
$this->assertEquals('bad', $calls[4][1][0], '->load() registers variables as Twig globals');
$this->assertEquals(array('key' => 'foo'), $calls[4][1][1], '->load() registers variables as Twig globals');
}
// Twig options

View File

@ -156,11 +156,11 @@ class ChoiceList implements ChoiceListInterface
$values = $this->fixValues($values);
$choices = array();
foreach ($values as $j => $givenValue) {
foreach ($this->values as $i => $value) {
foreach ($values as $i => $givenValue) {
foreach ($this->values as $j => $value) {
if ($value === $givenValue) {
$choices[] = $this->choices[$i];
unset($values[$j]);
$choices[$i] = $this->choices[$j];
unset($values[$i]);
if (0 === count($values)) {
break 2;
@ -180,11 +180,11 @@ class ChoiceList implements ChoiceListInterface
$choices = $this->fixChoices($choices);
$values = array();
foreach ($this->choices as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
foreach ($choices as $i => $givenChoice) {
foreach ($this->choices as $j => $choice) {
if ($choice === $givenChoice) {
$values[] = $this->values[$i];
unset($choices[$j]);
$values[$i] = $this->values[$j];
unset($choices[$i]);
if (0 === count($choices)) {
break 2;
@ -204,11 +204,11 @@ class ChoiceList implements ChoiceListInterface
$choices = $this->fixChoices($choices);
$indices = array();
foreach ($this->choices as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
foreach ($choices as $i => $givenChoice) {
foreach ($this->choices as $j => $choice) {
if ($choice === $givenChoice) {
$indices[] = $i;
unset($choices[$j]);
$indices[$i] = $j;
unset($choices[$i]);
if (0 === count($choices)) {
break 2;
@ -228,11 +228,11 @@ class ChoiceList implements ChoiceListInterface
$values = $this->fixValues($values);
$indices = array();
foreach ($this->values as $i => $value) {
foreach ($values as $j => $givenValue) {
foreach ($values as $i => $givenValue) {
foreach ($this->values as $j => $value) {
if ($value === $givenValue) {
$indices[] = $i;
unset($values[$j]);
$indices[$i] = $j;
unset($values[$i]);
if (0 === count($values)) {
break 2;

View File

@ -97,6 +97,9 @@ interface ChoiceListInterface
*
* The choices can have any data type.
*
* The choices must be returned with the same keys and in the same order
* as the corresponding values in the given array.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored
*
@ -109,6 +112,9 @@ interface ChoiceListInterface
*
* The values must be strings.
*
* The values must be returned with the same keys and in the same order
* as the corresponding choices in the given array.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored
*
@ -125,6 +131,9 @@ interface ChoiceListInterface
*
* The index "placeholder" is internally reserved.
*
* The indices must be returned with the same keys and in the same order
* as the corresponding choices in the given array.
*
* @param array $choices An array of choices. Not existing choices in this
* array are ignored
*
@ -140,6 +149,9 @@ interface ChoiceListInterface
*
* The index "placeholder" is internally reserved.
*
* The indices must be returned with the same keys and in the same order
* as the corresponding values in the given array.
*
* @param array $values An array of choice values. Not existing values in
* this array are ignored
*

View File

@ -49,15 +49,11 @@ class BooleanToStringTransformer implements DataTransformerInterface
*/
public function transform($value)
{
if (null === $value) {
return null;
}
if (!is_bool($value)) {
throw new TransformationFailedException('Expected a Boolean.');
}
return true === $value ? $this->trueValue : null;
return $value ? $this->trueValue : null;
}
/**

View File

@ -60,14 +60,14 @@ class ChoiceToBooleanArrayTransformer implements DataTransformerInterface
throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e);
}
$index = current($this->choiceList->getIndicesForChoices(array($choice)));
$valueMap = array_flip($this->choiceList->getValuesForChoices(array($choice)));
foreach ($values as $i => $value) {
$values[$i] = $i === $index;
$values[$i] = isset($valueMap[$value]);
}
if ($this->placeholderPresent) {
$values['placeholder'] = false === $index;
$values['placeholder'] = 0 === count($valueMap);
}
return $values;

View File

@ -58,10 +58,10 @@ class ChoicesToBooleanArrayTransformer implements DataTransformerInterface
throw new TransformationFailedException('Can not get the choice list', $e->getCode(), $e);
}
$indexMap = array_flip($this->choiceList->getIndicesForChoices($array));
$valueMap = array_flip($this->choiceList->getValuesForChoices($array));
foreach ($values as $i => $value) {
$values[$i] = isset($indexMap[$i]);
$values[$i] = isset($valueMap[$value]);
}
return $values;

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -38,10 +39,43 @@ class FixCheckboxInputListener implements EventSubscriberInterface
public function preSubmit(FormEvent $event)
{
$values = (array) $event->getData();
$indices = $this->choiceList->getIndicesForValues($values);
$data = $event->getData();
$event->setData(count($indices) > 0 ? array_combine($indices, $values) : array());
if (is_array($data)) {
// Flip the submitted values for faster lookup
// It's better to flip this array than $existingValues because
// $submittedValues is generally smaller.
$submittedValues = array_flip($data);
// Since expanded choice fields are completely loaded anyway, we
// can just as well get the values again without losing performance.
$existingValues = $this->choiceList->getValues();
// Clear the data array and fill it with correct indices
$data = array();
foreach ($existingValues as $index => $value) {
if (isset($submittedValues[$value])) {
// Value was submitted
$data[$index] = $value;
unset($submittedValues[$value]);
}
}
if (count($submittedValues) > 0) {
throw new TransformationFailedException(sprintf(
'The following choices were not found: "%s"',
implode('", "', array_keys($submittedValues))
));
}
} elseif ('' === $data || null === $data) {
// Empty values are always accepted.
$data = array();
}
// Else leave the data unchanged to provoke an error during submission
$event->setData($data);
}
/**

View File

@ -42,10 +42,22 @@ class FixRadioInputListener implements EventSubscriberInterface
public function preSubmit(FormEvent $event)
{
$value = $event->getData();
$index = current($this->choiceList->getIndicesForValues(array($value)));
$data = $event->getData();
$event->setData(false !== $index ? array($index => $value) : ($this->placeholderPresent ? array('placeholder' => '') : array())) ;
// Since expanded choice fields are completely loaded anyway, we
// can just as well get the values again without losing performance.
$existingValues = $this->choiceList->getValues();
if (false !== ($index = array_search($data, $existingValues, true))) {
$data = array($index => $data);
} elseif ('' === $data || null === $data) {
// Empty values are always accepted.
$data = $this->placeholderPresent ? array('placeholder' => '') : array();
}
// Else leave the data unchanged to provoke an error during submission
$event->setData($data);
}
/**

View File

@ -25,9 +25,14 @@ class CheckboxType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addViewTransformer(new BooleanToStringTransformer($options['value']))
;
// Unlike in other types, where the data is NULL by default, it
// needs to be a Boolean here. setData(null) is not acceptable
// for checkboxes and radio buttons (unless a custom model
// transformer handles this case).
// We cannot solve this case via overriding the "data" option, because
// doing so also calls setDataLocked(true).
$builder->setData(isset($options['data']) ? $options['data'] : false);
$builder->addViewTransformer(new BooleanToStringTransformer($options['value']));
}
/**
@ -46,8 +51,8 @@ class CheckboxType extends AbstractType
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$emptyData = function (FormInterface $form, $clientData) {
return $clientData;
$emptyData = function (FormInterface $form, $viewData) {
return $viewData;
};
$resolver->setDefaults(array(

View File

@ -57,7 +57,7 @@ class ChoiceType extends AbstractType
// Check if the choices already contain the empty value
// Only add the empty value option if this is not the case
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getIndicesForValues(array('')))) {
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
$placeholderView = new ChoiceView(null, '', $options['empty_value']);
// "placeholder" is a reserved index
@ -120,7 +120,7 @@ class ChoiceType extends AbstractType
// Check if the choices already contain the empty value
// Only add the empty value option if this is not the case
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getIndicesForValues(array('')))) {
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
$view->vars['empty_value'] = $options['empty_value'];
}

View File

@ -522,83 +522,82 @@ class Form implements \IteratorAggregate, FormInterface
$dispatcher = $this->config->getEventDispatcher();
// Hook to change content of the data submitted by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
$event = new FormEvent($this, $submittedData);
$dispatcher->dispatch(FormEvents::PRE_SUBMIT, $event);
$submittedData = $event->getData();
}
$modelData = null;
$normData = null;
$viewData = null;
// Check whether the form is compound.
// This check is preferable over checking the number of children,
// since forms without children may also be compound.
// (think of empty collection forms)
if ($this->config->getCompound()) {
if (!is_array($submittedData)) {
$submittedData = array();
try {
// Hook to change content of the data submitted by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
$event = new FormEvent($this, $submittedData);
$dispatcher->dispatch(FormEvents::PRE_SUBMIT, $event);
$submittedData = $event->getData();
}
foreach ($this->children as $name => $child) {
if (array_key_exists($name, $submittedData) || $clearMissing) {
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);
}
}
$this->extraData = $submittedData;
}
// Forms that inherit their parents' data also are not processed,
// because then it would be too difficult to merge the changes in
// the child and the parent form. Instead, the parent form also takes
// changes in the grandchildren (i.e. children of the form that inherits
// its parent's data) into account.
// (see InheritDataAwareIterator below)
if ($this->config->getInheritData()) {
$this->submitted = true;
// When POST_SUBMIT is reached, the data is not yet updated, so pass
// NULL to prevent hard-to-debug bugs.
$dataForPostSubmit = null;
} else {
// If the form is compound, the default data in view format
// is reused. The data of the children is merged into this
// default data using the data mapper.
// If the form is not compound, the submitted data is also the data in view format.
$viewData = $this->config->getCompound() ? $this->viewData : $submittedData;
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof \Closure) {
/* @var \Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
// Check whether the form is compound.
// This check is preferable over checking the number of children,
// since forms without children may also be compound.
// (think of empty collection forms)
if ($this->config->getCompound()) {
if (null === $submittedData) {
$submittedData = array();
}
$viewData = $emptyData;
if (!is_array($submittedData)) {
throw new TransformationFailedException('Compound forms expect an array or NULL on submission.');
}
foreach ($this->children as $name => $child) {
if (isset($submittedData[$name]) || $clearMissing) {
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);
}
}
$this->extraData = $submittedData;
}
// Merge form data from children into existing view data
// It is not necessary to invoke this method if the form has no children,
// even if it is compound.
if (count($this->children) > 0) {
// Use InheritDataAwareIterator to process children of
// descendants that inherit this form's data.
// These descendants will not be submitted normally (see the check
// for $this->config->getInheritData() above)
$iterator = new InheritDataAwareIterator($this->children);
$iterator = new \RecursiveIteratorIterator($iterator);
$this->config->getDataMapper()->mapFormsToData($iterator, $viewData);
}
// Forms that inherit their parents' data also are not processed,
// because then it would be too difficult to merge the changes in
// the child and the parent form. Instead, the parent form also takes
// changes in the grandchildren (i.e. children of the form that inherits
// its parent's data) into account.
// (see InheritDataAwareIterator below)
if (!$this->config->getInheritData()) {
// If the form is compound, the default data in view format
// is reused. The data of the children is merged into this
// default data using the data mapper.
// If the form is not compound, the submitted data is also the data in view format.
$viewData = $this->config->getCompound() ? $this->viewData : $submittedData;
$modelData = null;
$normData = null;
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof \Closure) {
/* @var \Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
}
$viewData = $emptyData;
}
// Merge form data from children into existing view data
// It is not necessary to invoke this method if the form has no children,
// even if it is compound.
if (count($this->children) > 0) {
// Use InheritDataAwareIterator to process children of
// descendants that inherit this form's data.
// These descendants will not be submitted normally (see the check
// for $this->config->getInheritData() above)
$childrenIterator = new InheritDataAwareIterator($this->children);
$childrenIterator = new \RecursiveIteratorIterator($childrenIterator);
$this->config->getDataMapper()->mapFormsToData($childrenIterator, $viewData);
}
try {
// Normalize data to unified representation
$normData = $this->viewToNorm($viewData);
// Hook to change content of the data into the normalized
// Hook to change content of the data in the normalized
// representation
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) {
$event = new FormEvent($this, $normData);
@ -609,20 +608,26 @@ class Form implements \IteratorAggregate, FormInterface
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
$viewData = $this->normToView($normData);
} catch (TransformationFailedException $e) {
$this->synchronized = false;
}
} catch (TransformationFailedException $e) {
$this->synchronized = false;
$this->submitted = true;
$this->modelData = $modelData;
$this->normData = $normData;
$this->viewData = $viewData;
$dataForPostSubmit = $viewData;
// If $viewData was not yet set, set it to $submittedData so that
// the erroneous data is accessible on the form.
// Forms that inherit data never set any data, because the getters
// forward to the parent form's getters anyway.
if (null === $viewData && !$this->config->getInheritData()) {
$viewData = $submittedData;
}
}
$this->submitted = true;
$this->modelData = $modelData;
$this->normData = $normData;
$this->viewData = $viewData;
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) {
$event = new FormEvent($this, $dataForPostSubmit);
$event = new FormEvent($this, $viewData);
$dispatcher->dispatch(FormEvents::POST_SUBMIT, $event);
}

View File

@ -0,0 +1,297 @@
<?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\Component\Form\Tests\Extension\Core\ChoiceList;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractChoiceListTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected $list;
/**
* @var array
*/
protected $choices;
/**
* @var array
*/
protected $values;
/**
* @var array
*/
protected $indices;
/**
* @var array
*/
protected $labels;
/**
* @var mixed
*/
protected $choice1;
/**
* @var mixed
*/
protected $choice2;
/**
* @var mixed
*/
protected $choice3;
/**
* @var mixed
*/
protected $choice4;
/**
* @var string
*/
protected $value1;
/**
* @var string
*/
protected $value2;
/**
* @var string
*/
protected $value3;
/**
* @var string
*/
protected $value4;
/**
* @var int|string
*/
protected $index1;
/**
* @var int|string
*/
protected $index2;
/**
* @var int|string
*/
protected $index3;
/**
* @var int|string
*/
protected $index4;
/**
* @var string
*/
protected $label1;
/**
* @var string
*/
protected $label2;
/**
* @var string
*/
protected $label3;
/**
* @var string
*/
protected $label4;
protected function setUp()
{
parent::setUp();
$this->list = $this->createChoiceList();
$this->choices = $this->getChoices();
$this->indices = $this->getIndices();
$this->values = $this->getValues();
$this->labels = $this->getLabels();
// allow access to the individual entries without relying on their indices
reset($this->choices);
reset($this->indices);
reset($this->values);
reset($this->labels);
for ($i = 1; $i <= 4; ++$i) {
$this->{'choice'.$i} = current($this->choices);
$this->{'index'.$i} = current($this->indices);
$this->{'value'.$i} = current($this->values);
$this->{'label'.$i} = current($this->labels);
next($this->choices);
next($this->indices);
next($this->values);
next($this->labels);
}
}
public function testGetChoices()
{
$this->assertSame($this->choices, $this->list->getChoices());
}
public function testGetValues()
{
$this->assertSame($this->values, $this->list->getValues());
}
public function testGetIndicesForChoices()
{
$choices = array($this->choice1, $this->choice2);
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => $this->choice1, 8 => $this->choice2);
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array($this->choice2, $this->choice1);
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->choice1, $this->choice2, 'foobar');
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesEmpty()
{
$this->assertSame(array(), $this->list->getIndicesForChoices(array()));
}
public function testGetIndicesForValues()
{
// values and indices are always the same
$values = array($this->value1, $this->value2);
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
// values and indices are always the same
$values = array(5 => $this->value1, 8 => $this->value2);
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
$values = array($this->value2, $this->value1);
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array($this->value1, $this->value2, 'foobar');
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesEmpty()
{
$this->assertSame(array(), $this->list->getIndicesForValues(array()));
}
public function testGetChoicesForValues()
{
$values = array($this->value1, $this->value2);
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => $this->value1, 8 => $this->value2);
$this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array($this->value2, $this->value1);
$this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array($this->value1, $this->value2, 'foobar');
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
// https://github.com/symfony/symfony/issues/3446
public function testGetChoicesForValuesEmpty()
{
$this->assertSame(array(), $this->list->getChoicesForValues(array()));
}
public function testGetValuesForChoices()
{
$choices = array($this->choice1, $this->choice2);
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => $this->choice1, 8 => $this->choice2);
$this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array($this->choice2, $this->choice1);
$this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->choice1, $this->choice2, 'foobar');
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesEmpty()
{
$this->assertSame(array(), $this->list->getValuesForChoices(array()));
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
abstract protected function createChoiceList();
abstract protected function getChoices();
abstract protected function getLabels();
abstract protected function getValues();
abstract protected function getIndices();
}

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class ChoiceListTest extends \PHPUnit_Framework_TestCase
class ChoiceListTest extends AbstractChoiceListTest
{
private $obj1;
@ -24,39 +24,14 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
private $obj4;
private $list;
protected function setUp()
{
parent::setUp();
$this->obj1 = new \stdClass();
$this->obj2 = new \stdClass();
$this->obj3 = new \stdClass();
$this->obj4 = new \stdClass();
$this->list = new ChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
array(
'Group 1' => array('A', 'B'),
'Group 2' => array('C', 'D'),
),
array($this->obj2, $this->obj3)
);
}
protected function tearDown()
{
parent::tearDown();
$this->obj1 = null;
$this->obj2 = null;
$this->obj3 = null;
$this->obj4 = null;
$this->list = null;
parent::setUp();
}
public function testInitArray()
@ -119,65 +94,10 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews());
}
public function testGetIndicesForChoices()
{
$choices = array($this->obj2, $this->obj3);
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForValues()
{
// values and indices are always the same
$values = array('1', '2');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('1', '2', '5');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetChoicesForValues()
{
$values = array('1', '2');
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesCorrectOrderingOfResult()
{
$values = array('2', '1');
$this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array('1', '2', '5');
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetValuesForChoices()
{
$choices = array($this->obj2, $this->obj3);
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testNonMatchingLabels()
public function testInitWithInsufficientLabels()
{
$this->list = new ChoiceList(
array($this->obj1, $this->obj2),
@ -185,7 +105,7 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
);
}
public function testLabelsContainingNull()
public function testInitWithLabelsContainingNull()
{
$this->list = new ChoiceList(
array($this->obj1, $this->obj2),
@ -197,4 +117,42 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->list->getRemainingViews()
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new ChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
array(
'Group 1' => array('A', 'B'),
'Group 2' => array('C', 'D'),
),
array($this->obj2, $this->obj3)
);
}
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

@ -29,7 +29,7 @@ class ObjectChoiceListTest_EntityWithToString
}
}
class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
class ObjectChoiceListTest extends AbstractChoiceListTest
{
private $obj1;
@ -39,39 +39,14 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
private $obj4;
/**
* @var ObjectChoiceList
*/
private $list;
protected function setUp()
{
parent::setUp();
$this->obj1 = (object) array('name' => 'A');
$this->obj2 = (object) array('name' => 'B');
$this->obj3 = (object) array('name' => 'C');
$this->obj4 = (object) array('name' => 'D');
$this->list = new ObjectChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
'name',
array($this->obj2, $this->obj3)
);
}
protected function tearDown()
{
parent::tearDown();
$this->obj1 = null;
$this->obj2 = null;
$this->obj3 = null;
$this->obj4 = null;
$this->list = null;
parent::setUp();
}
public function testInitArray()
@ -209,4 +184,39 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
array($this->obj1, $this->obj2, $this->obj3, $this->obj4)
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new ObjectChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
'name',
array($this->obj2, $this->obj3)
);
}
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

@ -11,43 +11,11 @@
namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
class SimpleChoiceListTest extends AbstractChoiceListTest
{
private $list;
private $numericList;
protected function setUp()
{
parent::setUp();
$choices = array(
'Group 1' => array('a' => 'A', 'b' => 'B'),
'Group 2' => array('c' => 'C', 'd' => 'D'),
);
$numericChoices = array(
'Group 1' => array(0 => 'A', 1 => 'B'),
'Group 2' => array(2 => 'C', 3 => 'D'),
);
$this->list = new SimpleChoiceList($choices, array('b', 'c'));
// Use COPY_CHOICE strategy to test for the various associated problems
$this->numericList = new SimpleChoiceList($numericChoices, array(1, 2));
}
protected function tearDown()
{
parent::tearDown();
$this->list = null;
$this->numericList = null;
}
public function testInitArray()
{
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
@ -73,83 +41,6 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews());
}
public function testGetIndicesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array('b', 'c', 'foobar');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesDealsWithNumericChoices()
{
// Pass choices as strings although they are integers
$choices = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getIndicesForChoices($choices));
}
public function testGetIndicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getIndicesForValues($values));
}
public function testGetChoicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getChoicesForValues($values));
}
public function testGetValuesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingValues()
{
$choices = array('b', 'c', 'foobar');
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array('0', '1'), $this->numericList->getValuesForChoices($values));
}
/**
* @dataProvider dirtyValuesProvider
*/
@ -164,7 +55,6 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
'foo10' => 'Foo 10',
);
// use COPY_CHOICE strategy to test the problems
$this->list = new SimpleChoiceList($choices, array());
$this->assertSame(array($value), $this->list->getValuesForChoices(array($choice)));
@ -185,4 +75,35 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
array('foo10', 'foo10'),
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new SimpleChoiceList(array(
'Group 1' => array('a' => 'A', 'b' => 'B'),
'Group 2' => array('c' => 'C', 'd' => 'D'),
), array('b', 'c'));
}
protected function getChoices()
{
return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
}
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,78 @@
<?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\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class SimpleNumericChoiceListTest extends AbstractChoiceListTest
{
public function testGetIndicesForChoicesDealsWithNumericChoices()
{
// Pass choices as strings although they are integers
$choices = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getIndicesForValues($values));
}
public function testGetChoicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getChoicesForValues($values));
}
public function testGetValuesForChoicesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array('0', '1'), $this->list->getValuesForChoices($values));
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new SimpleChoiceList(array(
'Group 1' => array(0 => 'A', 1 => 'B'),
'Group 2' => array(2 => 'C', 3 => 'D'),
), array(1, 2));
}
protected function getChoices()
{
return array(0 => 0, 1 => 1, 2 => 2, 3 => 3);
}
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

@ -17,6 +17,9 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
{
const TRUE_VALUE = '1';
/**
* @var BooleanToStringTransformer
*/
protected $transformer;
protected function setUp()
@ -33,20 +36,29 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(self::TRUE_VALUE, $this->transformer->transform(true));
$this->assertNull($this->transformer->transform(false));
$this->assertNull($this->transformer->transform(null));
}
public function testTransformExpectsBoolean()
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testTransformFailsIfNull()
{
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
$this->transformer->transform(null);
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testTransformFailsIfString()
{
$this->transformer->transform('1');
}
public function testReverseTransformExpectsString()
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformFailsIfInteger()
{
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
$this->transformer->reverseTransform(1);
}

View File

@ -15,6 +15,15 @@ use Symfony\Component\Form\CallbackTransformer;
class CheckboxTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
{
public function testDataIsFalseByDefault()
{
$form = $this->factory->create('checkbox');
$this->assertFalse($form->getData());
$this->assertFalse($form->getNormData());
$this->assertNull($form->getViewData());
}
public function testPassValueToView()
{
$form = $this->factory->create('checkbox', null, array('value' => 'foobar'));
@ -105,58 +114,60 @@ class CheckboxTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form->getViewData());
}
public function testBindWithEmptyValueAndFalseUnchecked()
public function testSubmitWithEmptyValueAndFalseUnchecked()
{
$form = $this->factory->create('checkbox', null, array(
'value' => '',
));
$form->bind(false);
$form->submit(false);
$this->assertFalse($form->getData());
$this->assertNull($form->getViewData());
}
public function testBindWithEmptyValueAndTrueChecked()
public function testSubmitWithEmptyValueAndTrueChecked()
{
$form = $this->factory->create('checkbox', null, array(
'value' => '',
));
$form->bind(true);
$form->submit(true);
$this->assertTrue($form->getData());
$this->assertSame('', $form->getViewData());
}
/**
* @dataProvider provideTransformedData
* @dataProvider provideCustomModelTransformerData
*/
public function testTransformedData($data, $expected)
public function testCustomModelTransformer($data, $checked)
{
// present a binary status field as a checkbox
$transformer = new CallbackTransformer(
function ($value) {
return 'expedited' == $value;
return 'checked' == $value;
},
function ($value) {
return $value ? 'expedited' : 'standard';
return $value ? 'checked' : 'unchecked';
}
);
$form = $this->builder
->create('expedited_shipping', 'checkbox')
$form = $this->factory->createBuilder('checkbox')
->addModelTransformer($transformer)
->getForm();
$form->setData($data);
$view = $form->createView();
$this->assertEquals($expected, $view->vars['checked']);
$this->assertSame($data, $form->getData());
$this->assertSame($checked, $form->getNormData());
$this->assertEquals($checked, $view->vars['checked']);
}
public function provideTransformedData()
public function provideCustomModelTransformerData()
{
return array(
array('expedited', true),
array('standard', false),
array('checked', true),
array('unchecked', false),
);
}
}

View File

@ -231,6 +231,21 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertEquals('b', $form->getViewData());
}
public function testSubmitSingleNonExpandedInvalidChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => false,
'choices' => $this->choices,
));
$form->submit('foobar');
$this->assertNull($form->getData());
$this->assertEquals('foobar', $form->getViewData());
$this->assertFalse($form->isSynchronized());
}
public function testSubmitSingleNonExpandedObjectChoices()
{
$form = $this->factory->create('choice', null, array(
@ -268,6 +283,36 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertEquals(array('a', 'b'), $form->getViewData());
}
public function testSubmitMultipleNonExpandedInvalidScalarChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
));
$form->submit('foobar');
$this->assertNull($form->getData());
$this->assertEquals('foobar', $form->getViewData());
$this->assertFalse($form->isSynchronized());
}
public function testSubmitMultipleNonExpandedInvalidArrayChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => true,
'expanded' => false,
'choices' => $this->choices,
));
$form->submit(array('a', 'foobar'));
$this->assertNull($form->getData());
$this->assertEquals(array('a', 'foobar'), $form->getViewData());
$this->assertFalse($form->isSynchronized());
}
public function testSubmitMultipleNonExpandedObjectChoices()
{
$form = $this->factory->create('choice', null, array(
@ -309,6 +354,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
3 => false,
4 => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertTrue($form[1]->getData());
@ -322,6 +369,34 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedRequiredInvalidChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => true,
'required' => true,
'choices' => $this->choices,
));
$form->submit('foobar');
$this->assertSame(null, $form->getData());
$this->assertSame('foobar', $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertFalse($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedNonRequired()
{
$form = $this->factory->create('choice', null, array(
@ -342,6 +417,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
4 => false,
'placeholder' => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form['placeholder']->getData());
$this->assertFalse($form[0]->getData());
@ -357,7 +434,35 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedRequiredNothingChecked()
public function testSubmitSingleExpandedNonRequiredInvalidChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => true,
'required' => false,
'choices' => $this->choices,
));
$form->submit('foobar');
$this->assertSame(null, $form->getData());
$this->assertSame('foobar', $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertFalse($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedRequiredNull()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
@ -376,6 +481,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
3 => false,
4 => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
@ -389,7 +496,75 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedNonRequiredNothingChecked()
public function testSubmitSingleExpandedRequiredEmpty()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => true,
'required' => true,
'choices' => $this->choices,
));
$form->submit('');
$this->assertNull($form->getData());
$this->assertSame(array(
0 => false,
1 => false,
2 => false,
3 => false,
4 => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedRequiredFalse()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => true,
'required' => true,
'choices' => $this->choices,
));
$form->submit(false);
$this->assertNull($form->getData());
$this->assertSame(array(
0 => false,
1 => false,
2 => false,
3 => false,
4 => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedNonRequiredNull()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
@ -409,6 +584,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
4 => false,
'placeholder' => true,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form['placeholder']->getData());
$this->assertFalse($form[0]->getData());
@ -424,22 +601,44 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form[4]->getViewData());
}
public function testSubmitFalseToSingleExpandedRequiredDoesNotProduceExtraChildrenError()
public function testSubmitSingleExpandedNonRequiredEmpty()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
'expanded' => true,
'required' => true,
'required' => false,
'choices' => $this->choices,
));
$form->submit(false);
$form->submit('');
$this->assertEmpty($form->getExtraData());
$this->assertNull($form->getData());
$this->assertSame(array(
0 => false,
1 => false,
2 => false,
3 => false,
4 => false,
'placeholder' => true,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form['placeholder']->getData());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertSame('', $form['placeholder']->getViewData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitFalseToSingleExpandedNonRequiredDoesNotProduceExtraChildrenError()
public function testSubmitSingleExpandedNonRequiredFalse()
{
$form = $this->factory->create('choice', null, array(
'multiple' => false,
@ -450,8 +649,30 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$form->submit(false);
$this->assertEmpty($form->getExtraData());
$this->assertNull($form->getData());
$this->assertSame(array(
0 => false,
1 => false,
2 => false,
3 => false,
4 => false,
'placeholder' => true,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form['placeholder']->getData());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertSame('', $form['placeholder']->getViewData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitSingleExpandedWithEmptyChild()
@ -539,6 +760,16 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$form->submit(array('a', 'c'));
$this->assertSame(array('a', 'c'), $form->getData());
$this->assertSame(array(
0 => true,
1 => false,
2 => true,
3 => false,
4 => false,
), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertTrue($form->isSynchronized());
$this->assertTrue($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertTrue($form[2]->getData());
@ -551,6 +782,60 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form[4]->getViewData());
}
public function testSubmitMultipleExpandedInvalidScalarChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => true,
'expanded' => true,
'choices' => $this->choices,
));
$form->submit('foobar');
$this->assertNull($form->getData());
$this->assertSame('foobar', $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertFalse($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitMultipleExpandedInvalidArrayChoice()
{
$form = $this->factory->create('choice', null, array(
'multiple' => true,
'expanded' => true,
'choices' => $this->choices,
));
$form->submit(array('a', 'foobar'));
$this->assertNull($form->getData());
$this->assertSame(array('a', 'foobar'), $form->getViewData());
$this->assertEmpty($form->getExtraData());
$this->assertFalse($form->isSynchronized());
$this->assertFalse($form[0]->getData());
$this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertNull($form[0]->getViewData());
$this->assertNull($form[1]->getViewData());
$this->assertNull($form[2]->getViewData());
$this->assertNull($form[3]->getViewData());
$this->assertNull($form[4]->getViewData());
}
public function testSubmitMultipleExpandedEmpty()
{
$form = $this->factory->create('choice', null, array(

View File

@ -412,7 +412,8 @@ class Request
$dup->format = null;
if (!$dup->get('_format')) {
$dup->setRequestFormat($this->getRequestFormat());
// we set the request format to null if the current request is not known
$dup->setRequestFormat($this->getRequestFormat(null));
}
return $dup;

View File

@ -91,7 +91,8 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
}
$uri = $this->signer->sign($this->generateFragmentUri($uri, $request));
// we need to sign the absolute URI, but want to return the path only.
$uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), strlen($request->getSchemeAndHttpHost()));
}
// We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.

View File

@ -31,7 +31,7 @@ class HIncludeFragmentRendererTest extends \PHPUnit_Framework_TestCase
{
$strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));
$this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=2RweanrYElMFCPCuRjoIUqaG2vpMpjtGqvqj9pUFLxA%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
$this->assertEquals('<hx:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller&amp;_hash=BP%2BOzCD5MRUI%2BHJpgPDOmoju00FnzLhP3TGcSHbbBLs%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
}
public function testRenderWithUri()

View File

@ -307,7 +307,18 @@ class Process
$this->updateStatus(true);
}
$this->updateStatus(false);
$this->processPipes->close();
while ($this->processPipes->hasOpenHandles()) {
usleep(100);
foreach ($this->processPipes->readAndCloseHandles(true) as $type => $data) {
if (3 == $type) {
$this->fallbackExitcode = (int) $data;
} else {
call_user_func($this->callback, $type === self::STDOUT ? self::OUT : self::ERR, $data);
}
}
}
$this->close();
if ($this->processInformation['signaled']) {
if ($this->isSigchildEnabled()) {

View File

@ -121,6 +121,32 @@ class ProcessPipes
return array_replace($this->readStreams($blocking), $this->readFileHandles());
}
/**
* Reads data in file handles and pipes, closes them if EOF is reached.
*
* @param Boolean $blocking Whether to use blocking calls or not.
*
* @return array An array of read data indexed by their fd.
*/
public function readAndCloseHandles($blocking)
{
return array_replace($this->readStreams($blocking, true), $this->readFileHandles(true));
}
/**
* Returns if the current state has open file handles or pipes.
*
* @return Boolean
*/
public function hasOpenHandles()
{
if ($this->useFiles) {
return (Boolean) $this->fileHandles;
}
return (Boolean) $this->pipes;
}
/**
* Writes stdin data.
*
@ -177,20 +203,29 @@ class ProcessPipes
*
* @return array An array of read data indexed by their fd.
*/
private function readFileHandles()
private function readFileHandles($close = false)
{
$read = array();
foreach ($this->fileHandles as $type => $fileHandle) {
fseek($fileHandle, $this->readBytes[$type]);
$fh = $this->fileHandles;
foreach ($fh as $type => $fileHandle) {
if (0 !== fseek($fileHandle, $this->readBytes[$type])) {
continue;
}
$data = '';
while (!feof($fileHandle)) {
$data .= fread($fileHandle, 8192);
if (false !== $dataread = fread($fileHandle, 16392)) {
$data .= $dataread;
}
}
if (0 < $length = strlen($data)) {
$this->readBytes[$type] += $length;
$read[$type] = $data;
}
if (true === $close && feof($fileHandle)) {
fclose($this->fileHandles[$type]);
unset($this->fileHandles[$type]);
}
}
return $read;
@ -203,7 +238,7 @@ class ProcessPipes
*
* @return array An array of read data indexed by their fd.
*/
private function readStreams($blocking)
private function readStreams($blocking, $close = false)
{
$read = array();
@ -234,6 +269,11 @@ class ProcessPipes
if (strlen($data) > 0) {
$read[$type] = $data;
}
if (true === $close && feof($pipe)) {
fclose($this->pipes[$type]);
unset($this->pipes[$type]);
}
}
return $read;

View File

@ -102,10 +102,6 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
*/
public function testProcessPipes($code, $size)
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->markTestSkipped('Test hangs on Windows & PHP due to https://bugs.php.net/bug.php?id=60120 and https://bugs.php.net/bug.php?id=51800');
}
$expected = str_repeat(str_repeat('*', 1024), $size) . '!';
$expectedLength = (1024 * $size) + 1;
@ -638,11 +634,18 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
{
$variations = array(
'fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);',
'include \''.__DIR__.'/ProcessTestHelper.php\';',
'include \''.__DIR__.'/PipeStdinInStdoutStdErrStreamSelect.php\';',
);
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
// Avoid XL buffers on Windows because of https://bugs.php.net/bug.php?id=65650
$sizes = array(1, 2, 4, 8);
} else {
$sizes = array(1, 16, 64, 1024, 4096);
}
$codes = array();
foreach (array(1, 16, 64, 1024, 4096) as $size) {
foreach ($sizes as $size) {
foreach ($variations as $code) {
$codes[] = array($code, $size);
}

View File

@ -64,7 +64,7 @@ interface UserInterface
*
* This can return null if the password was not encoded using a salt.
*
* @return string The salt
* @return string|null The salt
*/
public function getSalt();

View File

@ -15,8 +15,6 @@ use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api