[DoctrineBridge] fixed DoctrineChoiceLoaderTest by removing deprecated factory

This commit is contained in:
Jules Pietri 2016-06-29 12:52:49 +02:00
parent 0439837729
commit 3f86eaece1

View File

@ -18,18 +18,12 @@ use Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader; use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
/** /**
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
*/ */
class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var ChoiceListFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $factory;
/** /**
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject * @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
*/ */
@ -98,36 +92,60 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadChoiceList() public function testLoadChoiceList()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
); );
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array());
$value = function () {}; $value = function () {};
$choiceList = new ArrayChoiceList($choices, $value);
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('findAll') ->method('findAll')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once()) $this->assertEquals($choiceList, $loader->loadChoiceList($value));
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame($choiceList, $loader->loadChoiceList($value));
// no further loads on subsequent calls // no further loads on subsequent calls
$this->assertSame($choiceList, $loader->loadChoiceList($value)); $this->assertEquals($choiceList, $loader->loadChoiceList($value));
}
/**
* @group legacy
*/
public function testLegacyLoadChoiceList()
{
$factory = $this->getMock('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface');
$loader = new DoctrineChoiceLoader(
$factory,
$this->om,
$this->class,
$this->idReader
);
$choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function () {};
$choiceList = new ArrayChoiceList($choices, $value);
$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);
$factory->expects($this->never())
->method('createListFromChoices');
$this->assertEquals($choiceList, $loaded = $loader->loadChoiceList($value));
// no further loads on subsequent calls
$this->assertSame($loaded, $loader->loadChoiceList($value));
} }
public function testLoadChoiceListUsesObjectLoaderIfAvailable() public function testLoadChoiceListUsesObjectLoaderIfAvailable()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader, $this->idReader,
@ -135,7 +153,7 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
); );
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array()); $choiceList = new ArrayChoiceList($choices);
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
@ -144,39 +162,27 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
->method('getEntities') ->method('getEntities')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once()) $this->assertEquals($choiceList, $loaded = $loader->loadChoiceList());
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame($choiceList, $loader->loadChoiceList());
// no further loads on subsequent calls // no further loads on subsequent calls
$this->assertSame($choiceList, $loader->loadChoiceList()); $this->assertSame($loaded, $loader->loadChoiceList());
} }
public function testLoadValuesForChoices() public function testLoadValuesForChoices()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
); );
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('findAll') ->method('findAll')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame(array('1', '2'), $loader->loadValuesForChoices(array($this->obj2, $this->obj3))); $this->assertSame(array('1', '2'), $loader->loadValuesForChoices(array($this->obj2, $this->obj3)));
// no further loads on subsequent calls // no further loads on subsequent calls
@ -187,7 +193,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices() public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -196,16 +201,12 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->assertSame(array(), $loader->loadValuesForChoices(array())); $this->assertSame(array(), $loader->loadValuesForChoices(array()));
} }
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId() public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -218,9 +219,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->idReader->expects($this->any()) $this->idReader->expects($this->any())
->method('getIdValue') ->method('getIdValue')
->with($this->obj2) ->with($this->obj2)
@ -232,7 +230,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven() public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -240,7 +237,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; }; $value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);
$this->idReader->expects($this->any()) $this->idReader->expects($this->any())
->method('isSingleId') ->method('isSingleId')
@ -250,11 +246,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
->method('findAll') ->method('findAll')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame(array('B'), $loader->loadValuesForChoices( $this->assertSame(array('B'), $loader->loadValuesForChoices(
array($this->obj2), array($this->obj2),
$value $value
@ -264,7 +255,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader() public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -279,9 +269,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->idReader->expects($this->any()) $this->idReader->expects($this->any())
->method('getIdValue') ->method('getIdValue')
->with($this->obj2) ->with($this->obj2)
@ -296,24 +283,17 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadChoicesForValues() public function testLoadChoicesForValues()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
); );
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('findAll') ->method('findAll')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);
$this->assertSame(array($this->obj2, $this->obj3), $loader->loadChoicesForValues(array('1', '2'))); $this->assertSame(array($this->obj2, $this->obj3), $loader->loadChoicesForValues(array('1', '2')));
// no further loads on subsequent calls // no further loads on subsequent calls
@ -324,7 +304,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues() public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -333,16 +312,12 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->assertSame(array(), $loader->loadChoicesForValues(array())); $this->assertSame(array(), $loader->loadChoicesForValues(array()));
} }
public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId() public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader, $this->idReader,
@ -362,9 +337,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->objectLoader->expects($this->once()) $this->objectLoader->expects($this->once())
->method('getEntitiesByIds') ->method('getEntitiesByIds')
->with('idField', array(4 => '3', 7 => '2')) ->with('idField', array(4 => '3', 7 => '2'))
@ -386,7 +358,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven() public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader $this->idReader
@ -394,7 +365,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$choices = array($this->obj1, $this->obj2, $this->obj3); $choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; }; $value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);
$this->idReader->expects($this->any()) $this->idReader->expects($this->any())
->method('isSingleId') ->method('isSingleId')
@ -404,11 +374,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
->method('findAll') ->method('findAll')
->willReturn($choices); ->willReturn($choices);
$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertSame(array($this->obj2), $loader->loadChoicesForValues( $this->assertSame(array($this->obj2), $loader->loadChoicesForValues(
array('B'), array('B'),
$value $value
@ -418,7 +383,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader() public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
{ {
$loader = new DoctrineChoiceLoader( $loader = new DoctrineChoiceLoader(
$this->factory,
$this->om, $this->om,
$this->class, $this->class,
$this->idReader, $this->idReader,
@ -439,9 +403,6 @@ class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
$this->repository->expects($this->never()) $this->repository->expects($this->never())
->method('findAll'); ->method('findAll');
$this->factory->expects($this->never())
->method('createListFromChoices');
$this->objectLoader->expects($this->once()) $this->objectLoader->expects($this->once())
->method('getEntitiesByIds') ->method('getEntitiesByIds')
->with('idField', array('2')) ->with('idField', array('2'))