Skip to content

[DoctrineBridge] fixed DoctrineChoiceLoaderTest by removing deprecated factory #19222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class DoctrineChoiceLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ChoiceListFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $factory;

/**
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -98,44 +92,68 @@ protected function setUp()
public function testLoadChoiceList()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array());
$value = function () {};
$choiceList = new ArrayChoiceList($choices, $value);

$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);
$this->assertEquals($choiceList, $loader->loadChoiceList($value));

$this->assertSame($choiceList, $loader->loadChoiceList($value));
// no further loads on subsequent calls

$this->assertEquals($choiceList, $loader->loadChoiceList($value));
}

/**
* @group legacy
*/
public function testLegacyLoadChoiceList()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @group legacy annotation (can you please rebase while fixing?)

{
$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($choiceList, $loader->loadChoiceList($value));
$this->assertSame($loaded, $loader->loadChoiceList($value));
}

public function testLoadChoiceListUsesObjectLoaderIfAvailable()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
$this->objectLoader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList(array());
$choiceList = new ArrayChoiceList($choices);

$this->repository->expects($this->never())
->method('findAll');
Expand All @@ -144,39 +162,27 @@ public function testLoadChoiceListUsesObjectLoaderIfAvailable()
->method('getEntities')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);

$this->assertSame($choiceList, $loader->loadChoiceList());
$this->assertEquals($choiceList, $loaded = $loader->loadChoiceList());

// no further loads on subsequent calls

$this->assertSame($choiceList, $loader->loadChoiceList());
$this->assertSame($loaded, $loader->loadChoiceList());
}

public function testLoadValuesForChoices()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);

$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);

$this->assertSame(array('1', '2'), $loader->loadValuesForChoices(array($this->obj2, $this->obj3)));

// no further loads on subsequent calls
Expand All @@ -187,7 +193,6 @@ public function testLoadValuesForChoices()
public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
Expand All @@ -196,16 +201,12 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->assertSame(array(), $loader->loadValuesForChoices(array()));
}

public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
Expand All @@ -218,9 +219,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->idReader->expects($this->any())
->method('getIdValue')
->with($this->obj2)
Expand All @@ -232,15 +230,13 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);

$this->idReader->expects($this->any())
->method('isSingleId')
Expand All @@ -250,11 +246,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
->method('findAll')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);

$this->assertSame(array('B'), $loader->loadValuesForChoices(
array($this->obj2),
$value
Expand All @@ -264,7 +255,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
Expand All @@ -279,9 +269,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->idReader->expects($this->any())
->method('getIdValue')
->with($this->obj2)
Expand All @@ -296,24 +283,17 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
public function testLoadChoicesForValues()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$choiceList = new ArrayChoiceList($choices);

$this->repository->expects($this->once())
->method('findAll')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices)
->willReturn($choiceList);

$this->assertSame(array($this->obj2, $this->obj3), $loader->loadChoicesForValues(array('1', '2')));

// no further loads on subsequent calls
Expand All @@ -324,7 +304,6 @@ public function testLoadChoicesForValues()
public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
Expand All @@ -333,16 +312,12 @@ public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->assertSame(array(), $loader->loadChoicesForValues(array()));
}

public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
Expand All @@ -362,9 +337,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->objectLoader->expects($this->once())
->method('getEntitiesByIds')
->with('idField', array(4 => '3', 7 => '2'))
Expand All @@ -386,15 +358,13 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader
);

$choices = array($this->obj1, $this->obj2, $this->obj3);
$value = function (\stdClass $object) { return $object->name; };
$choiceList = new ArrayChoiceList($choices, $value);

$this->idReader->expects($this->any())
->method('isSingleId')
Expand All @@ -404,11 +374,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
->method('findAll')
->willReturn($choices);

$this->factory->expects($this->once())
->method('createListFromChoices')
->with($choices, $value)
->willReturn($choiceList);

$this->assertSame(array($this->obj2), $loader->loadChoicesForValues(
array('B'),
$value
Expand All @@ -418,7 +383,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
{
$loader = new DoctrineChoiceLoader(
$this->factory,
$this->om,
$this->class,
$this->idReader,
Expand All @@ -439,9 +403,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
$this->repository->expects($this->never())
->method('findAll');

$this->factory->expects($this->never())
->method('createListFromChoices');

$this->objectLoader->expects($this->once())
->method('getEntitiesByIds')
->with('idField', array('2'))
Expand Down