Explorar el Código

Add a StatusType

Thomas Rabaix hace 11 años
padre
commit
7c3a40ef36
Se han modificado 2 ficheros con 109 adiciones y 0 borrados
  1. 62 0
      Form/Type/StatusType.php
  2. 47 0
      Tests/Form/Type/StatusTypeTest.php

+ 62 - 0
Form/Type/StatusType.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the Sonata 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\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+class StatusType extends AbstractType
+{
+    protected $class;
+
+    protected $getter;
+
+    protected $name;
+
+    /**
+     * @param string $class
+     * @param string $getter
+     * @param string $name
+     */
+    public function __construct($class, $getter, $name)
+    {
+        $this->class  = $class;
+        $this->getter = $getter;
+        $this->name   = $name;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getParent()
+    {
+        return 'choice';
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setDefaultOptions(OptionsResolverInterface $resolver)
+    {
+        $resolver->setDefaults(array(
+            'choice' => call_user_func(array($this->class, $this->getter))
+        ));
+    }
+}

+ 47 - 0
Tests/Form/Type/StatusTypeTest.php

@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the Sonata 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\Type;
+
+use Sonata\AdminBundle\Form\Type\StatusType;
+
+use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class Choice
+{
+    public static function getList()
+    {
+        return array(
+            1 => 'salut'
+        );
+    }
+}
+
+class StatusTypeTest extends TypeTestCase
+{
+    public function testGetDefaultOptions()
+    {
+        $type = new StatusType('Sonata\AdminBundle\Tests\Form\Type\Choice', 'getList', 'choice_type');
+
+        $this->assertEquals('choice_type', $type->getName());
+        $this->assertEquals('choice', $type->getParent());
+
+        $optionResolver = new OptionsResolver();
+
+        $type->setDefaultOptions($optionResolver);
+
+        $options = $optionResolver->resolve(array());
+
+        $this->assertArrayHasKey('choice', $options);
+        $this->assertEquals($options['choice'], array(1 => 'salut'));
+    }
+}