diff --git a/src/Database/Type/IntegerType.php b/src/Database/Type/IntegerType.php index e21e635089d..981e8883462 100644 --- a/src/Database/Type/IntegerType.php +++ b/src/Database/Type/IntegerType.php @@ -64,7 +64,7 @@ public function toDatabase($value, Driver $driver) return null; } - if (!is_scalar($value)) { + if (!is_numeric($value)) { throw new InvalidArgumentException(sprintf( 'Cannot convert value of type `%s` to integer', getTypeName($value) @@ -98,7 +98,7 @@ public function toPHP($value, Driver $driver) public function manyToPHP(array $values, array $fields, Driver $driver) { foreach ($fields as $field) { - if (!isset($values[$field])) { + if (!isset($values[$field]) || !is_numeric($values[$field])) { continue; } $values[$field] = (int)$values[$field]; diff --git a/tests/TestCase/Database/Type/IntegerTypeTest.php b/tests/TestCase/Database/Type/IntegerTypeTest.php index 5fcbbc3fe71..82dd7541fc1 100644 --- a/tests/TestCase/Database/Type/IntegerTypeTest.php +++ b/tests/TestCase/Database/Type/IntegerTypeTest.php @@ -79,14 +79,16 @@ public function testManyToPHP() 'b' => '2.3', 'c' => '15', 'd' => '0.0', - 'e' => 10 + 'e' => 10, + 'f' => '6a88accf-a34e-4dd9-ade0-8d255ccaecbe' ]; $expected = [ 'a' => null, 'b' => 2, 'c' => 15, 'd' => 0, - 'e' => 10 + 'e' => 10, + 'f' => '6a88accf-a34e-4dd9-ade0-8d255ccaecbe' ]; $this->assertEquals( $expected, @@ -103,9 +105,6 @@ public function testToDatabase() { $this->assertNull($this->type->toDatabase(null, $this->driver)); - $result = $this->type->toDatabase('some data', $this->driver); - $this->assertSame(0, $result); - $result = $this->type->toDatabase(2, $this->driver); $this->assertSame(2, $result); @@ -113,15 +112,31 @@ public function testToDatabase() $this->assertSame(2, $result); } + /** + * Invalid Integer Data Provider + * + * @return void + */ + public function invalidIntegerProvider() + { + return [ + 'array' => [['3', '4']], + 'non-numeric-string' => ['some-data'], + 'uuid' => ['6a88accf-a34e-4dd9-ade0-8d255ccaecbe'], + ]; + } + /** * Tests that passing an invalid value will throw an exception * + * @dataProvider invalidIntegerProvider + * @param mixed $value Invalid value to test against the database type. * @return void */ - public function testToDatabaseInvalid() + public function testToDatabaseInvalid($value) { $this->expectException(\InvalidArgumentException::class); - $this->type->toDatabase(['3', '4'], $this->driver); + $this->type->toDatabase($value, $this->driver); } /**