Last active
December 26, 2018 16:15
-
-
Save fsevestre/abfefe0b66e5f5b24c60 to your computer and use it in GitHub Desktop.
Boolean form type for Symfony2 + tests (library: https://github.com/fsevestre/BooleanFormType)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form\Type; | |
use AppBundle\Form\DataTransformer\BooleanTypeToBooleanTransformer; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class BooleanType extends AbstractType | |
{ | |
const VALUE_FALSE = 0; | |
const VALUE_TRUE = 1; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->addModelTransformer(new BooleanTypeToBooleanTransformer()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'compound' => false, | |
)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'boolean'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Tests\Form\Type; | |
use AppBundle\Form\Type\BooleanType; | |
use Symfony\Component\Form\Test\TypeTestCase; | |
class BooleanTypeTest extends TypeTestCase | |
{ | |
/** | |
* @dataProvider getTestData | |
*/ | |
public function testFormType($value, $expected) | |
{ | |
$type = new BooleanType(); | |
$form = $this->factory->create($type); | |
$form->submit($value); | |
$this->assertTrue($form->isSynchronized()); | |
$this->assertEquals($expected, $form->getData()); | |
} | |
public function getTestData() | |
{ | |
return array( | |
array('1', true), | |
array(1, true), | |
array(true, true), | |
array('0', false), | |
array(0, false), | |
array(false, false), | |
array('yes', false), | |
array('no', false), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form\DataTransformer; | |
use AppBundle\Form\Type\BooleanType; | |
use Symfony\Component\Form\DataTransformerInterface; | |
class BooleanTypeToBooleanTransformer implements DataTransformerInterface | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function transform($value) | |
{ | |
if (true === $value || BooleanType::VALUE_TRUE === (int) $value) { | |
return BooleanType::VALUE_TRUE; | |
} | |
return BooleanType::VALUE_FALSE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function reverseTransform($value) | |
{ | |
if (BooleanType::VALUE_TRUE === (int) $value) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Tests\Form\DataTransformer; | |
use AppBundle\Form\DataTransformer\BooleanTypeToBooleanTransformer; | |
use AppBundle\Form\Type\BooleanType; | |
class BooleanTypeToBooleanTransformerTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @dataProvider getTransformData | |
*/ | |
public function testTransform($value, $expected) | |
{ | |
$transformer = new BooleanTypeToBooleanTransformer(); | |
$this->assertEquals($expected, $transformer->transform($value)); | |
} | |
public function getTransformData() | |
{ | |
return array( | |
array(true, BooleanType::VALUE_TRUE), | |
array(false, BooleanType::VALUE_FALSE), | |
array('no', BooleanType::VALUE_FALSE), | |
array('1', BooleanType::VALUE_TRUE), | |
array('0', BooleanType::VALUE_FALSE), | |
array(1, BooleanType::VALUE_TRUE), | |
array(0, BooleanType::VALUE_FALSE), | |
); | |
} | |
/** | |
* @dataProvider getReverseTransformData | |
*/ | |
public function testReverseTransform($value, $expected) | |
{ | |
$transformer = new BooleanTypeToBooleanTransformer(); | |
$this->assertEquals($expected, $transformer->reverseTransform($value)); | |
} | |
public function getReverseTransformData() | |
{ | |
return array( | |
array(BooleanType::VALUE_TRUE, true), | |
array(1, true), | |
array('1', true), | |
array(true, true), | |
array('yes', false), | |
array(BooleanType::VALUE_FALSE, false), | |
array(0, false), | |
array('0', false), | |
array(false, false), | |
array('no', false), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.form.type.boolean: | |
class: AppBundle\Form\Type\BooleanType | |
tags: | |
- { name: form.type, alias: boolean } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment