[Form] Changed value transformers to throw UnexpectedTypeException instances

This commit is contained in:
Bernhard Schussek 2011-01-03 17:26:18 +01:00 committed by Fabien Potencier
parent 48af2fc86e
commit acdd5c06de
22 changed files with 84 additions and 46 deletions

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form;
* with this source code in the file LICENSE.
*/
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\InvalidOptionsException;
/**
* Lets the user select between different choices
@ -39,11 +39,11 @@ class ChoiceField extends HybridField
$this->addOption('empty_value', '');
if (!is_array($this->getOption('choices'))) {
throw new UnexpectedTypeException('The choices option must be an array');
throw new InvalidOptionsException('The choices option must be an array', array('choices'));
}
if (!is_array($this->getOption('preferred_choices'))) {
throw new UnexpectedTypeException('The preferred_choices option must be an array');
throw new InvalidOptionsException('The preferred_choices option must be an array', array('preferred_choices'));
}
if (count($this->getOption('preferred_choices')) > 0) {

View File

@ -60,7 +60,7 @@ class CollectionField extends FieldGroup
public function setData($collection)
{
if (!is_array($collection) && !$collection instanceof \Traversable) {
throw new UnexpectedTypeException('The data passed to the CollectionField must be an array or a Traversable');
throw new UnexpectedTypeException($collection, 'array or \Traversable');
}
foreach ($this as $name => $field) {

View File

@ -13,4 +13,8 @@ namespace Symfony\Component\Form\Exception;
class UnexpectedTypeException extends FormException
{
public function __construct($value, $expectedType)
{
parent::__construct(sprintf('Expected argument of type %s, %s given', $expectedType, gettype($value)));
}
}

View File

@ -281,7 +281,7 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
}
if (!is_array($taintedData)) {
throw new UnexpectedTypeException('You must pass an array parameter to the bind() method');
throw new UnexpectedTypeException($taintedData, 'array');
}
foreach ($this->fields as $key => $field) {

View File

@ -11,6 +11,8 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a boolean and a string.
*
@ -32,7 +34,7 @@ class BooleanToStringTransformer extends BaseValueTransformer
}
if (!is_bool($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type boolean but got %s.', gettype($value)));
throw new UnexpectedTypeException($value, 'boolean');
}
return true === $value ? '1' : '';
@ -47,7 +49,7 @@ class BooleanToStringTransformer extends BaseValueTransformer
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string but got %s.', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}
return '' !== $value;

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a normalized time and a localized time string/array.
@ -61,7 +61,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
}
if (!$dateTime instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($dateTime, '\DateTime');
}
$inputTimezone = $this->getOption('input_timezone');
@ -106,7 +106,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
$outputTimezone = $this->getOption('output_timezone');
if (!is_array($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type array, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'array');
}
if (implode('', $value) === '') {

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a normalized time and a localized time string
@ -62,7 +62,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
}
if (!$dateTime instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($dateTime, '\DateTime');
}
$inputTimezone = $this->getOption('input_timezone');
@ -92,7 +92,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$inputTimezone = $this->getOption('input_timezone');
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}
if ('' === $value) {

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a date string and a DateTime object
@ -47,7 +47,7 @@ class DateTimeToStringTransformer extends BaseValueTransformer
}
if (!$value instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($value, '\DateTime');
}
$value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));
@ -63,10 +63,14 @@ class DateTimeToStringTransformer extends BaseValueTransformer
*/
public function reverseTransform($value, $originalValue)
{
if ('' === $value) {
if (empty($value)) {
return null;
}
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}
$outputTimezone = $this->getOption('output_timezone');
$inputTimezone = $this->getOption('input_timezone');

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a timestamp and a DateTime object
@ -45,7 +45,7 @@ class DateTimeToTimestampTransformer extends BaseValueTransformer
}
if (!$value instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($value, '\DateTime');
}
$value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));
@ -65,6 +65,10 @@ class DateTimeToTimestampTransformer extends BaseValueTransformer
return null;
}
if (!is_numeric($value)) {
throw new UnexpectedTypeException($value, 'numeric');
}
$outputTimezone = $this->getOption('output_timezone');
$inputTimezone = $this->getOption('input_timezone');

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a normalized format and a localized money string.
@ -43,7 +43,7 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
{
if (null !== $value) {
if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}
$value /= $this->getOption('divisor');

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a number type and a localized number with grouping
@ -55,7 +55,7 @@ class NumberToLocalizedStringTransformer extends BaseValueTransformer
}
if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}
$formatter = $this->getNumberFormatter();
@ -76,7 +76,7 @@ class NumberToLocalizedStringTransformer extends BaseValueTransformer
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}
if ('' === $value) {

View File

@ -11,7 +11,7 @@ namespace Symfony\Component\Form\ValueTransformer;
* with this source code in the file LICENSE.
*/
use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* Transforms between a normalized format (integer or float) and a percentage value.
@ -57,7 +57,7 @@ class PercentToLocalizedStringTransformer extends BaseValueTransformer
}
if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}
if (self::FRACTIONAL == $this->getOption('type')) {
@ -84,7 +84,7 @@ class PercentToLocalizedStringTransformer extends BaseValueTransformer
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}
if ('' === $value) {

View File

@ -43,7 +43,7 @@ interface ValueTransformerInterface extends Localizable
*
* @param mixed $value The value in the original representation
* @return mixed The value in the transformed representation
* @throws InvalidArgumentException when the argument is no string
* @throws UnexpectedTypeException when the argument is no string
* @throws ValueTransformerException when the transformation fails
*/
function transform($value);
@ -69,7 +69,7 @@ interface ValueTransformerInterface extends Localizable
* @param mixed $value The value in the transformed representation
* @param mixed $originalValue The original value from the datasource that is about to be overwritten by the new value.
* @return mixed The value in the original representation
* @throws InvalidArgumentException when the argument is not of the
* @throws UnexpectedTypeException when the argument is not of the
* expected type
* @throws ValueTransformerException when the transformation fails
*/

View File

@ -38,7 +38,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
);
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
*/
public function testConfigureChoicesWithNonArray()
{
@ -48,7 +48,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
*/
public function testConfigurePreferredChoicesWithNonArray()
{

View File

@ -22,14 +22,14 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
public function testTransformExpectsBoolean()
{
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->transformer->transform('1');
}
public function testReverseTransformExpectsString()
{
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$this->transformer->reverseTransform(1, null);
}

View File

@ -115,7 +115,7 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToArrayTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform('12345', null);
}
@ -212,7 +212,7 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToArrayTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform('12345', null);
}

View File

@ -155,7 +155,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('2010-01-01');
}
@ -294,7 +294,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform(12345, null);
}

View File

@ -49,7 +49,8 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('1234');
}
@ -87,11 +88,21 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
$this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input, null));
}
public function testReverseTransformExpectsValidString()
public function testReverseTransformExpectsString()
{
$reverseTransformer = new DateTimeToStringTransformer();
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$reverseTransformer->reverseTransform(1234, null);
}
public function testReverseTransformExpectsValidDateString()
{
$reverseTransformer = new DateTimeToStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$reverseTransformer->reverseTransform('2010-2010-2010', null);
}
}

View File

@ -63,7 +63,8 @@ class DateTimeToTimestampTransformerTest extends DateTimeTestCase
{
$transformer = new DateTimeToTimestampTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('1234');
}
@ -105,7 +106,8 @@ class DateTimeToTimestampTransformerTest extends DateTimeTestCase
{
$reverseTransformer = new DateTimeToTimestampTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$reverseTransformer->reverseTransform('2010-2010-2010', null);
}
}

View File

@ -20,13 +20,13 @@ class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase
$this->assertEquals('1,23', $transformer->transform(123));
}
public function testTransformThrowsExceptionIfNotNumeric()
public function testTransformExpectsNumeric()
{
$transformer = new MoneyToLocalizedStringTransformer(array(
'divisor' => 100,
));
$this->setExpectedException('InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('abcd');
}
@ -48,6 +48,17 @@ class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase
$this->assertEquals(123, $transformer->reverseTransform('1,23', null));
}
public function testReverseTransformExpectsString()
{
$transformer = new MoneyToLocalizedStringTransformer(array(
'divisor' => 100,
));
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform(12345, null);
}
public function testReverseTransform_empty()
{
$transformer = new MoneyToLocalizedStringTransformer();

View File

@ -102,7 +102,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
{
$transformer = new NumberToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('foo');
}
@ -111,7 +111,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
{
$transformer = new NumberToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform(1, null);
}

View File

@ -96,7 +96,7 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
{
$transformer = new PercentToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->transform('foo');
}
@ -105,7 +105,7 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
{
$transformer = new PercentToLocalizedStringTransformer();
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
$transformer->reverseTransform(1, null);
}