ModelChoiceLoaderTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Form\ChoiceList;
  11. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
  12. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo;
  13. class ModelChoiceLoaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $modelManager = null;
  16. public function setUp()
  17. {
  18. if (false === interface_exists('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) {
  19. $this->markTestSkipped('Test only available for > SF2.7');
  20. }
  21. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  22. }
  23. public function testLoadFromEntityWithSamePropertyValues()
  24. {
  25. $fooA = new Foo();
  26. $fooA->setBar(1);
  27. $fooA->setBaz('baz');
  28. $fooB = new Foo();
  29. $fooB->setBar(2);
  30. $fooB->setBaz('baz');
  31. $this->modelManager->expects($this->once())
  32. ->method('findBy')
  33. ->will($this->returnValue(array($fooA, $fooB)));
  34. $this->modelManager->expects($this->any())
  35. ->method('getIdentifierValues')
  36. ->will($this->returnCallback(function (Foo $foo) {
  37. return array($foo->getBar());
  38. }));
  39. $modelChoiceLoader = new ModelChoiceLoader(
  40. $this->modelManager,
  41. 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
  42. 'baz'
  43. );
  44. $expectedChoices = array(
  45. 1 => 'baz (id: 1)',
  46. 2 => 'baz (id: 2)',
  47. );
  48. $this->assertSame($expectedChoices, $modelChoiceLoader->loadChoiceList()->getOriginalKeys());
  49. }
  50. }