Skip to content

Commit

Permalink
Fixing failing tests in the Database namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 15, 2014
1 parent 43dae0d commit 28b15a9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
29 changes: 15 additions & 14 deletions tests/TestCase/Database/Type/DateTimeTypeTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Database\Type;

use Carbon\Carbon;
use Cake\Database\Type;
use Cake\Database\Type\DateTimeType;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testToPHP() {
$this->assertNull($this->type->toPHP(null, $this->driver));

$result = $this->type->toPHP('2001-01-04 12:13:14', $this->driver);
$this->assertInstanceOf('DateTime', $result);
$this->assertInstanceOf('Carbon\Carbon', $result);
$this->assertEquals('2001', $result->format('Y'));
$this->assertEquals('01', $result->format('m'));
$this->assertEquals('04', $result->format('d'));
Expand All @@ -63,7 +64,7 @@ public function testToPHP() {
public function testToPHPIncludingMilliseconds() {
$in = '2014-03-24 20:44:36.315113';
$result = $this->type->toPHP($in, $this->driver);
$this->assertInstanceOf('\DateTime', $result);
$this->assertInstanceOf('Carbon\Carbon', $result);
}

/**
Expand All @@ -76,7 +77,7 @@ public function testToDatabase() {
$result = $this->type->toDatabase($value, $this->driver);
$this->assertEquals($value, $result);

$date = new \DateTime('2013-08-12 15:16:17');
$date = new Carbon('2013-08-12 15:16:17');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('2013-08-12 15:16:17', $result);
}
Expand All @@ -97,54 +98,54 @@ public function marshalProvider() {
['2013-nope!', '2013-nope!'],

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

// valid array types
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new \DateTime('2014-02-14 13:14:15')
new Carbon('2014-02-14 13:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'am'
],
new \DateTime('2014-02-14 01:14:15')
new Carbon('2014-02-14 01:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'pm'
],
new \DateTime('2014-02-14 13:14:15')
new Carbon('2014-02-14 13:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],

// Invalid array types
[
['year' => 'farts', 'month' => 'derp'],
new \DateTime(date('Y-m-d 00:00:00'))
new Carbon(date('Y-m-d 00:00:00'))
],
[
['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
new \DateTime(date('Y-m-d 00:00:00'))
new Carbon(date('Y-m-d 00:00:00'))
],
[
[
'year' => '2014', 'month' => '02', 'day' => '14',
'hour' => 'farts', 'minute' => 'farts'
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
];
}
Expand Down
36 changes: 22 additions & 14 deletions tests/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Database\Type;

use Carbon\Carbon;
use Cake\Database\Type;
use Cake\Database\Type\DateType;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -47,9 +48,16 @@ public function testToPHP() {
$this->assertEquals('2001', $result->format('Y'));
$this->assertEquals('01', $result->format('m'));
$this->assertEquals('04', $result->format('d'));
}

$result = $this->type->toPHP('2001-01-04 10:11:12', $this->driver);
$this->assertFalse($result);
/**
* Tests that passing invalid data will throw an exception
*
* @expectedException InvalidArgumentException
* @return void
*/
public function testToPHPError() {
$this->type->toPHP('2001-01-04 10:11:12', $this->driver);
}

/**
Expand All @@ -62,11 +70,11 @@ public function testToDatabase() {
$result = $this->type->toDatabase($value, $this->driver);
$this->assertEquals($value, $result);

$date = new \DateTime('2013-08-12');
$date = new Carbon('2013-08-12');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('2013-08-12', $result);

$date = new \DateTime('2013-08-12 15:16:18');
$date = new Carbon('2013-08-12 15:16:18');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('2013-08-12', $result);
}
Expand All @@ -77,7 +85,7 @@ public function testToDatabase() {
* @return array
*/
public function marshalProvider() {
$date = new \DateTime('@1392387900');
$date = new Carbon('@1392387900');
$date->setTime(0, 0, 0);

return [
Expand All @@ -92,52 +100,52 @@ public function marshalProvider() {
// valid string types
['1392387900', $date],
[1392387900, $date],
['2014-02-14', new \DateTime('2014-02-14')],
['2014-02-14 13:14:15', new \DateTime('2014-02-14 00:00:00')],
['2014-02-14', new Carbon('2014-02-14')],
['2014-02-14 13:14:15', new Carbon('2014-02-14 00:00:00')],

// valid array types
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'am'
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'pm'
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],

// Invalid array types
[
['year' => 'farts', 'month' => 'derp'],
new \DateTime(date('Y-m-d 00:00:00'))
new Carbon(date('Y-m-d 00:00:00'))
],
[
['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
new \DateTime(date('Y-m-d 00:00:00'))
new Carbon(date('Y-m-d 00:00:00'))
],
[
[
'year' => '2014', 'month' => '02', 'day' => '14',
'hour' => 'farts', 'minute' => 'farts'
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
];
}
Expand Down
34 changes: 21 additions & 13 deletions tests/TestCase/Database/Type/TimeTypeTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Database\Type;

use Carbon\Carbon;
use Cake\Database\Type;
use Cake\Database\Type\TimeType;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -47,9 +48,16 @@ public function testToPHP() {
$this->assertEquals('16', $result->format('H'));
$this->assertEquals('30', $result->format('i'));
$this->assertEquals('15', $result->format('s'));
}

$result = $this->type->toPHP('2001-01-04 10:11:12', $this->driver);
$this->assertFalse($result);
/**
* Tests that passing invalid data will throw an exception
*
* @expectedException InvalidArgumentException
* @return void
*/
public function testToPHPError() {
$this->type->toPHP('2001-01-04 10:11:12', $this->driver);
}

/**
Expand All @@ -62,11 +70,11 @@ public function testToDatabase() {
$result = $this->type->toDatabase($value, $this->driver);
$this->assertEquals($value, $result);

$date = new \DateTime('16:30:15');
$date = new Carbon('16:30:15');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('16:30:15', $result);

$date = new \DateTime('2013-08-12 15:16:18');
$date = new Carbon('2013-08-12 15:16:18');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('15:16:18', $result);
}
Expand All @@ -77,7 +85,7 @@ public function testToDatabase() {
* @return array
*/
public function marshalProvider() {
$date = new \DateTime('@1392387900');
$date = new Carbon('@1392387900');

return [
// invalid types.
Expand All @@ -91,48 +99,48 @@ public function marshalProvider() {
// valid string types
['1392387900', $date],
[1392387900, $date],
['13:10:10', new \DateTime('13:10:10')],
['2014-02-14 13:14:15', new \DateTime('2014-02-14 13:14:15')],
['13:10:10', new Carbon('13:10:10')],
['2014-02-14 13:14:15', new Carbon('2014-02-14 13:14:15')],

// valid array types
[
['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
new \DateTime('2014-02-14 13:14:15')
new Carbon('2014-02-14 13:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'am'
],
new \DateTime('2014-02-14 01:14:15')
new Carbon('2014-02-14 01:14:15')
],
[
[
'year' => 2014, 'month' => 2, 'day' => 14,
'hour' => 1, 'minute' => 14, 'second' => 15,
'meridian' => 'pm'
],
new \DateTime('2014-02-14 13:14:15')
new Carbon('2014-02-14 13:14:15')
],
[
[
'hour' => 1, 'minute' => 14, 'second' => 15,
],
new \DateTime('01:14:15')
new Carbon('01:14:15')
],

// Invalid array types
[
['hour' => 'nope', 'minute' => 14, 'second' => 15],
new \DateTime(date('Y-m-d 00:14:15'))
new Carbon(date('Y-m-d 00:14:15'))
],
[
[
'year' => '2014', 'month' => '02', 'day' => '14',
'hour' => 'nope', 'minute' => 'nope'
],
new \DateTime('2014-02-14 00:00:00')
new Carbon('2014-02-14 00:00:00')
],
];
}
Expand Down

0 comments on commit 28b15a9

Please sign in to comment.