Skip to content

Commit

Permalink
Merge pull request #4986 from cakephp/issue-4790
Browse files Browse the repository at this point in the history
Implement less aggressive casting in ORM.
  • Loading branch information
lorenzo committed Oct 27, 2014
2 parents 1cccf3d + ba1c347 commit 5243b0d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/Database/Type/DateTimeType.php
Expand Up @@ -94,14 +94,19 @@ public function marshal($value) {

$class = static::$dateTimeClass;
try {
$compare = $date = false;
if ($value === '' || $value === null || $value === false || $value === true) {
return null;
} elseif (is_numeric($value)) {
$date = new $class('@' . $value);
} elseif (is_string($value)) {
$date = new $class($value);
$compare = true;
}
if (isset($date)) {
if ($compare && $date && $date->format($this->_format) !== $value) {
return $value;
}
if ($date) {
return $date;
}
} catch (\Exception $e) {
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Type/FloatType.php
Expand Up @@ -74,7 +74,10 @@ public function marshal($value) {
if ($value === null || $value === '') {
return null;
}
return floatval($value);
if (is_numeric($value)) {
return (float)$value;
}
return $value;
}

}
8 changes: 7 additions & 1 deletion src/Database/Type/IntegerType.php
Expand Up @@ -74,7 +74,13 @@ public function marshal($value) {
if ($value === null || $value === '') {
return null;
}
return (int)$value;
if (is_int($value)) {
return $value;
}
if (ctype_digit($value)) {
return (int)$value;
}
return $value;
}

}
3 changes: 2 additions & 1 deletion tests/TestCase/Database/Type/DateTimeTypeTest.php
Expand Up @@ -100,11 +100,12 @@ public function marshalProvider() {
['', null],
['derpy', 'derpy'],
['2013-nope!', '2013-nope!'],
['13-06-26', '13-06-26'],

// valid string types
['1392387900', new Time('@1392387900')],
[1392387900, new Time('@1392387900')],
['2014-02-14', new Time('2014-02-14')],
['2014-02-14 00:00:00', new Time('2014-02-14 00:00:00')],
['2014-02-14 13:14:15', new Time('2014-02-14 13:14:15')],

// valid array types
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -96,12 +96,13 @@ public function marshalProvider() {
['', null],
['derpy', 'derpy'],
['2013-nope!', '2013-nope!'],
['14-02-14', '14-02-14'],
['2014-02-14 13:14:15', '2014-02-14 13:14:15'],

// valid string types
['1392387900', $date],
[1392387900, $date],
['2014-02-14', new Time('2014-02-14')],
['2014-02-14 13:14:15', new Time('2014-02-14 00:00:00')],

// valid array types
[
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Database/Type/FloatTypeTest.php
Expand Up @@ -76,13 +76,16 @@ public function testToDatabase() {
*/
public function testMarshal() {
$result = $this->type->marshal('some data', $this->driver);
$this->assertSame(0.0, $result);
$this->assertSame('some data', $result);

$result = $this->type->marshal('', $this->driver);
$this->assertNull($result);

$result = $this->type->marshal('2.51', $this->driver);
$this->assertSame(2.51, $result);

$result = $this->type->marshal('3.5 bears', $this->driver);
$this->assertSame('3.5 bears', $result);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Database/Type/IntegerTypeTest.php
Expand Up @@ -76,13 +76,25 @@ public function testToDatabase() {
*/
public function testMarshal() {
$result = $this->type->marshal('some data', $this->driver);
$this->assertSame(0, $result);
$this->assertSame('some data', $result);

$result = $this->type->marshal('', $this->driver);
$this->assertNull($result);

$result = $this->type->marshal('0', $this->driver);
$this->assertSame(0, $result);

$result = $this->type->marshal('105', $this->driver);
$this->assertSame(105, $result);

$result = $this->type->marshal(105, $this->driver);
$this->assertSame(105, $result);

$result = $this->type->marshal('1.25', $this->driver);
$this->assertSame('1.25', $result);

$result = $this->type->marshal('2 monkeys', $this->driver);
$this->assertSame('2 monkeys', $result);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Database/Type/TimeTypeTest.php
Expand Up @@ -95,12 +95,13 @@ public function marshalProvider() {
['', null],
['derpy', 'derpy'],
['16-nope!', '16-nope!'],
['14:15', '14:15'],
['2014-02-14 13:14:15', '2014-02-14 13:14:15'],

// valid string types
['1392387900', $date],
[1392387900, $date],
['13:10:10', new Time('13:10:10')],
['2014-02-14 13:14:15', new Time('2014-02-14 13:14:15')],

// valid array types
[
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -184,6 +184,27 @@ public function testOneWithDatetimeField() {
$this->assertEquals($data['created'], $result->created->getTimestamp());
}

/**
* Ensure that marshalling casts reasonably.
*
* @return void
*/
public function testOneOnlyCastMatchingData() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 'derp',
'created' => 'fale'
];
$this->articles->entityClass(__NAMESPACE__ . '\OpenEntity');
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, []);

$this->assertSame($data['title'], $result->title);
$this->assertSame($data['author_id'], $result->author_id, 'No cast on bad data.');
$this->assertSame($data['created'], $result->created, 'No cast on bad data.');
}

/**
* Test one() follows mass-assignment rules.
*
Expand Down

0 comments on commit 5243b0d

Please sign in to comment.