ModelChoiceListTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\ModelChoiceList;
  12. use Sonata\CoreBundle\Tests\Fixtures\Bundle\Entity\Foo;
  13. class ModelChoiceListTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $modelManager = null;
  16. public function setUp()
  17. {
  18. if (false === interface_exists('Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList')) {
  19. $this->markTestSkipped('Test only available for < SF3.0');
  20. }
  21. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  22. $this->modelManager->expects($this->any())
  23. ->method('getIdentifierFieldNames')
  24. ->will($this->returnValue(array('foo', 'bar')));
  25. }
  26. public function testLoadFromEntity()
  27. {
  28. // Get choices From Entity, count($this->identifier) > 1
  29. $fooA = new Foo();
  30. $fooA->setBar(1);
  31. $fooB = new Foo();
  32. $fooB->setBar(2);
  33. $result = array($fooA, $fooB);
  34. $this->modelManager->expects($this->once())
  35. ->method('findBy')
  36. ->will($this->returnValue($result));
  37. $modelChoice = new ModelChoiceList(
  38. $this->modelManager,
  39. 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
  40. 'bar'
  41. );
  42. $this->assertSame(array_keys($result), $modelChoice->getChoices());
  43. }
  44. public function testLoadFromCustomQuery()
  45. {
  46. // Get choices From Custom Query, count($this->identifier) > 1
  47. $result = array(1, 2);
  48. $this->modelManager->expects($this->any())
  49. ->method('executeQuery')
  50. ->will($this->returnValue($result));
  51. $modelChoice = new ModelChoiceList(
  52. $this->modelManager,
  53. 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
  54. null,
  55. 'SELECT foo, baz from foo'
  56. );
  57. $this->assertSame(array_keys($result), $modelChoice->getChoices());
  58. }
  59. public function testLoadArrayOfChoices()
  60. {
  61. // Get choices from Array of choices, count($this->identifier) > 1
  62. $result = array(1, 2);
  63. $modelChoice = new ModelChoiceList(
  64. $this->modelManager,
  65. 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
  66. null,
  67. null,
  68. $result
  69. );
  70. $this->assertSame(array_keys($result), $modelChoice->getChoices());
  71. }
  72. }