Skip to content
Permalink
Browse files

Add tests for DateTimeType

Add some doc blocks too.
  • Loading branch information...
markstory committed Mar 7, 2013
1 parent 6bf528b commit 9c4a527045f161affaca907fb91292da31a37315
@@ -20,15 +20,34 @@
use Cake\Model\Datasource\Database\Driver;
use \DateTime;
/**
* Datetime type converter.
*
* Use to convert datetime instances to strings & back.
*/
class DateTimeType extends \Cake\Model\Datasource\Database\Type {
/**
* Convert DateTime instance into strings.
*
* @param string|Datetime $value The value to convert.
* @param Driver $driver The driver instance to convert with.
* @return string
*/
public function toDatabase($value, Driver $driver) {
if (is_string($value)) {
return $value;
}
return $value->format('Y-m-d H:i:s');
}
/**
* Convert strings into DateTime instances.
*
* @param string $value The value to convert.
* @param Driver $driver The driver instance to convert with.
* @return Datetime
*/
public function toPHP($value, Driver $driver) {
if ($value === null) {
return null;
@@ -0,0 +1,72 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Type;
use Cake\Model\Datasource\Database\Type;
use Cake\Model\Datasource\Database\Type\DateTimeType;
use Cake\TestSuite\TestCase;
/**
* Test for the DateTime type.
*/
class DateTimeTypeTest extends TestCase {
/**
* Setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->type = Type::build('datetime');
$this->driver = $this->getMock('Cake\Model\Datasource\Database\Driver');
}
/**
* Test toPHP
*
* @return void
*/
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->assertEqual('2001', $result->format('Y'));
$this->assertEqual('01', $result->format('m'));
$this->assertEqual('04', $result->format('d'));
$this->assertEqual('12', $result->format('H'));
$this->assertEqual('13', $result->format('i'));
$this->assertEqual('14', $result->format('s'));
}
/**
* Test converting to database format
*
* @return void
*/
public function testToDatabase() {
$value = '2001-01-04 12:13:14';
$result = $this->type->toDatabase($value, $this->driver);
$this->assertEquals($value, $result);
$date = new \DateTime('2013-08-12 15:16:17');
$result = $this->type->toDatabase($date, $this->driver);
$this->assertEquals('2013-08-12 15:16:17', $result);
}
}

0 comments on commit 9c4a527

Please sign in to comment.
You can’t perform that action at this time.