From 641af152807fdbcf69a701ce9284dd8cb66db9d9 Mon Sep 17 00:00:00 2001 From: FabioBatSilva Date: Wed, 15 Aug 2012 22:57:21 +0200 Subject: [PATCH] Fix DDC-1977 --- .../ORM/Query/ParameterTypeInferer.php | 33 +++++------- .../ORM/Query/ParameterTypeInfererTest.php | 53 +++++++++++++++++++ 2 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Query/ParameterTypeInfererTest.php diff --git a/lib/Doctrine/ORM/Query/ParameterTypeInferer.php b/lib/Doctrine/ORM/Query/ParameterTypeInferer.php index 7baa1d233..45a0aab08 100644 --- a/lib/Doctrine/ORM/Query/ParameterTypeInferer.php +++ b/lib/Doctrine/ORM/Query/ParameterTypeInferer.php @@ -19,8 +19,8 @@ namespace Doctrine\ORM\Query; -use Doctrine\DBAL\Connection, - Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Types\Type; /** * Provides an enclosed support for parameter infering. @@ -46,27 +46,20 @@ class ParameterTypeInferer */ public static function inferType($value) { - switch (true) { - case is_integer($value): - return Type::INTEGER; + if (is_integer($value)) { + return Type::INTEGER; + } - case ($value instanceof \DateTime): - return Type::DATETIME; + if ($value instanceof \DateTime) { + return Type::DATETIME; + } - case is_array($value): - $key = key($value); - - if (is_integer($value[$key])) { - return Connection::PARAM_INT_ARRAY; - } - - return Connection::PARAM_STR_ARRAY; - - default: - // Do nothing - break; + if (is_array($value)) { + return is_integer(current($value)) + ? Connection::PARAM_INT_ARRAY + : Connection::PARAM_STR_ARRAY; } return \PDO::PARAM_STR; } -} +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/ParameterTypeInfererTest.php b/tests/Doctrine/Tests/ORM/Query/ParameterTypeInfererTest.php new file mode 100644 index 000000000..60b118570 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Query/ParameterTypeInfererTest.php @@ -0,0 +1,53 @@ +. + */ +namespace Doctrine\Tests\ORM\Query; + +use Doctrine\ORM\Query\ParameterTypeInferer; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Types\Type; +use PDO; + +require_once __DIR__ . '/../../TestInit.php'; + +class ParameterTypeInfererTest extends \Doctrine\Tests\OrmTestCase +{ + + public function providerParameterTypeInferer() + { + return array( + array(1, Type::INTEGER), + array("bar", PDO::PARAM_STR), + array("1", PDO::PARAM_STR), + array(new \DateTime, Type::DATETIME), + array(array(2), Connection::PARAM_INT_ARRAY), + array(array("foo"), Connection::PARAM_STR_ARRAY), + array(array("1","2"), Connection::PARAM_STR_ARRAY), + array(array(), Connection::PARAM_STR_ARRAY), + ); + } + + /** + * @dataProvider providerParameterTypeInferer + */ + + public function testParameterTypeInferer($value, $expected) + { + $this->assertEquals($expected, ParameterTypeInferer::inferType($value)); + } +} \ No newline at end of file