Skip to content

Instantly share code, notes, and snippets.

@yitznewton
Created January 23, 2012 18:57
Show Gist options
  • Save yitznewton/1664874 to your computer and use it in GitHub Desktop.
Save yitznewton/1664874 to your computer and use it in GitHub Desktop.
symfony 1.4 widget and validator for Doctrine ENUM type
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) Jonathan H. Wage <jonwage@gmail.com>
* (c) Kevin Cyster <kcyster@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfWidgetFormDoctrineEnum represents a choice widget for an ENUM column in a model.
*
* @see http://forum.symfony-project.org/viewtopic.php?f=22&t=26873
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Kevin Cyster <kcyster@gmail.com>
*/
class sfWidgetFormDoctrineEnum extends sfWidgetFormChoice
{
/**
* Constructs the current widget.
*
* @see sfWidget
* @param array $options An array of options
* @param array $attributes An array of error messages
* @return VOID
*/
public function __construct($options = array(), $attributes = array())
{
$options['choices'] = array(); // sfWidgetFormChoice requires to be set
parent::__construct($options, $attributes);
}
/**
* Configures the current widget.
*
* Available options:
*
* * table: The Doctrine_Table object (required)
* * column: The ENUM column of the model class (required)
* * add_empty: Whether to add a first empty value or not (false by default)
* If the option is not a Boolean, the value will be used as the text value
* * remove An array of values to remove from the dropdown
*
* @see sfWidgetFormSelect
* @param array $options An array of options
* @param array $attributes An array of attributes
* @return VOID
*/
protected function configure($options = array(), $attributes = array())
{
$this->addRequiredOption('table');
$this->addRequiredOption('column');
$this->addOption('remove_choices', array());
parent::configure($options, $attributes);
}
/**
* Returns the choices associated to ENUM column of the model.
*
* @return array An array of choices
*/
public function getChoices()
{
$columnName = $this->getOption('column');
$columnDefinition = $this->getOption('table')->getColumnDefinition(
$columnName);
$enumValues = $this->getOption('table')->getEnumValues($columnName);
$choices = array();
if (
!isset($columnDefinition['notnull'])
|| $columnDefinition['notnull'] === false
) {
$choices[''] = '';
}
foreach ($this->getOption('remove_choices') as $v) {
$key = array_search($v, $enumValues);
if ($key !== false) {
unset($enumValues[$key]);
}
}
return array_merge($choices, array_combine($enumValues, $enumValues));
}
}
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) Kevin Cyster <kcyster@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfValidatorDoctrineEnum validates than the value is one of the model's column ENUM values.
*
* @package symfony
* @subpackage validator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Kevin Cyster@gmail.com
*/
class sfValidatorDoctrineEnum extends sfValidatorChoice
{
/**
* Configures the current validator.
*
* Available options:
*
* Available options:
*
* * table: The Doctrine_Table object (required)
* * column: The ENUM column of the model class (required)
*
* @see sfValidatorBase
* @see http://forum.symfony-project.org/viewtopic.php?f=22&t=26873
* @param array $options An array of options
* @param array $messages An array of error messages
* @return VOID
*/
protected function configure($options = array(), $messages = array())
{
$this->addRequiredOption('table');
$this->addRequiredOption('column');
$columnDefinition = $options['table']
->getColumnDefinition($options['column']);
if (
isset($columnDefinition['notnull'])
&& $columnDefinition['notnull'] === true
) {
$this->options['required'] = true;
}
else {
$this->options['required'] = false;
}
}
/**
* Method to return the enum choices of a column
*
* @return array $choices The enum values of the column
*/
public function getChoices()
{
$choices = array();
$enumValues = $this->getOption('table')
->getEnumValues($this->getOption('column'));
$choices = array_combine($enumValues, $enumValues);
return $choices;
}
}
<?php
require_once dirname(__FILE__).'/../../bootstrap/unit.php';
class unit_sfWidgetFormDoctrineEnumTest extends sfPHPUnitBaseTestCase
{
public function setUp()
{
$this->table = $this->getMockBuilder('Doctrine_Table')
->disableOriginalConstructor()
->getMock();
}
public function testGetChoices_NotNullTrue_ExpectedChoiceCount()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => true,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertCount(3, $widget->getChoices());
}
public function testGetChoices_NotNullTrue_NoEmptyChoice()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => true,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertFalse(array_search('', $widget->getChoices()));
}
public function testGetChoices_NotNullFalse_ExpectedChoiceCount()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => false,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertCount(4, $widget->getChoices());
}
public function testGetChoices_NotNullFalse_EmptyChoiceFirst()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => false,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$choices = $widget->getChoices();
$this->assertEquals('', array_shift($choices));
}
public function testGetChoices_NotNullUnspecified_ExpectedChoiceCount()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertCount(4, $widget->getChoices());
}
public function testGetChoices_NotNullUnspecified_EmptyChoiceFirst()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$widget = new sfWidgetFormDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$choices = $widget->getChoices();
$this->assertEquals('', array_shift($choices));
}
}
<?php
require_once dirname(__FILE__).'/../../bootstrap/unit.php';
class unit_sfValidatorDoctrineEnumTest extends sfPHPUnitBaseTestCase
{
public function setUp()
{
$this->table = $this->getMockBuilder('Doctrine_Table')
->disableOriginalConstructor()
->getMock();
}
/**
* @expectedException sfValidatorError
*/
public function testClean_InvalidOption_ThrowsException()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$validator = new sfValidatorDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$validator->clean('4');
}
public function testClean_ValidOption_ReturnsValue()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$validator = new sfValidatorDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertEquals('3', $validator->clean('3'));
}
/**
* @expectedException sfValidatorError
*/
public function testClean_EmptyStringNotNullTrue_ThrowsException()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => true,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$validator = new sfValidatorDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$validator->clean('');
}
public function testClean_EmptyStringNotNullFalse_ReturnsValue()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
'notnull' => false,
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$validator = new sfValidatorDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertEquals('', $validator->clean(''));
}
public function testClean_EmptyStringNotNullUnspecified_ReturnsValue()
{
$this->table->expects($this->any())
->method('getColumnDefinition')
->will($this->returnValue(array(
)));
$this->table->expects($this->any())
->method('getEnumValues')
->will($this->returnValue(array('1', '2', '3')));
$validator = new sfValidatorDoctrineEnum(array(
'table' => $this->table,
'column' => 'column',
));
$this->assertEquals('', $validator->clean(''));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment