Skip to content

Commit

Permalink
Check for is_numeric() instead of is_scalar().
Browse files Browse the repository at this point in the history
Fixes issue #12509
  • Loading branch information
sdustinh committed Sep 6, 2018
1 parent 00cdb32 commit 8aac6cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Database/Type/IntegerType.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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];
Expand Down
29 changes: 22 additions & 7 deletions tests/TestCase/Database/Type/IntegerTypeTest.php
Expand Up @@ -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,
Expand All @@ -103,25 +105,38 @@ 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);

$result = $this->type->toDatabase('2', $this->driver);
$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);
}

/**
Expand Down

0 comments on commit 8aac6cf

Please sign in to comment.