Pārlūkot izejas kodu

[ISSUE-3453]: defaults choices must be an array sf2.8

Joao Albuquerque 9 gadi atpakaļ
vecāks
revīzija
2cefe04cd7

+ 5 - 1
Form/ChoiceList/ModelChoiceList.php

@@ -128,7 +128,7 @@ class ModelChoiceList extends SimpleChoiceList
      */
      */
     protected function load($choices)
     protected function load($choices)
     {
     {
-        if (is_array($choices)) {
+        if (is_array($choices) && count($choices) > 0) {
             $entities = $choices;
             $entities = $choices;
         } elseif ($this->query) {
         } elseif ($this->query) {
             $entities = $this->modelManager->executeQuery($this->query);
             $entities = $this->modelManager->executeQuery($this->query);
@@ -136,6 +136,10 @@ class ModelChoiceList extends SimpleChoiceList
             $entities = $this->modelManager->findBy($this->class);
             $entities = $this->modelManager->findBy($this->class);
         }
         }
 
 
+        if (null === $entities) {
+            return array();
+        }
+
         $choices = array();
         $choices = array();
         $this->entities = array();
         $this->entities = array();
 
 

+ 1 - 1
Form/Type/ModelType.php

@@ -141,7 +141,7 @@ class ModelType extends AbstractType
             'class'             => null,
             'class'             => null,
             'property'          => null,
             'property'          => null,
             'query'             => null,
             'query'             => null,
-            'choices'           => null,
+            'choices'           => array(),
             'preferred_choices' => array(),
             'preferred_choices' => array(),
             'btn_add'           => 'link_add',
             'btn_add'           => 'link_add',
             'btn_list'          => 'link_list',
             'btn_list'          => 'link_list',

+ 90 - 0
Tests/Form/ChoiceList/ModelChoiceListTest.php

@@ -0,0 +1,90 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Form\ChoiceList;
+
+use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
+use Sonata\CoreBundle\Tests\Fixtures\Bundle\Entity\Foo;
+
+class ModelChoiceListTest extends \PHPUnit_Framework_TestCase
+{
+    private $modelManager = null;
+
+    public function setUp()
+    {
+        if (false === interface_exists('Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList')) {
+            $this->markTestSkipped('Test only available for < SF3.0');
+        }
+
+        $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        $this->modelManager->expects($this->any())
+            ->method('getIdentifierFieldNames')
+            ->will($this->returnValue(array('foo', 'bar')));
+    }
+
+    public function testLoadFromEntity()
+    {
+        // Get choices From Entity, count($this->identifier) > 1
+        $fooA = new Foo();
+        $fooA->setBar(1);
+        $fooB = new Foo();
+        $fooB->setBar(2);
+
+        $result = array($fooA, $fooB);
+
+        $this->modelManager->expects($this->once())
+            ->method('findBy')
+            ->will($this->returnValue($result));
+
+        $modelChoice = new ModelChoiceList(
+            $this->modelManager,
+            'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
+            'bar'
+        );
+
+        $this->assertSame(array_keys($result), $modelChoice->getChoices());
+    }
+
+    public function testLoadFromCustomQuery()
+    {
+        // Get choices From Custom Query, count($this->identifier) > 1
+        $result = array(1, 2);
+
+        $this->modelManager->expects($this->any())
+            ->method('executeQuery')
+            ->will($this->returnValue($result));
+
+        $modelChoice = new ModelChoiceList(
+            $this->modelManager,
+            'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
+            null,
+            'SELECT foo, baz from foo'
+        );
+
+        $this->assertSame(array_keys($result), $modelChoice->getChoices());
+    }
+
+    public function testLoadArrayOfChoices()
+    {
+        // Get choices from Array of choices, count($this->identifier) > 1
+        $result = array(1, 2);
+        $modelChoice = new ModelChoiceList(
+            $this->modelManager,
+            'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo',
+            null,
+            null,
+            $result
+        );
+
+        $this->assertSame(array_keys($result), $modelChoice->getChoices());
+    }
+}