Skip to content

Commit

Permalink
Add tests for DateType.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 6, 2013
1 parent 812a060 commit f18a11e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
24 changes: 12 additions & 12 deletions lib/Cake/Model/Datasource/Database/Type.php
Expand Up @@ -35,11 +35,11 @@ class Type {
* @var array
*/
protected static $_types = [
'boolean' => '\Cake\Model\Datasource\Database\Type\BooleanType',
'binary' => '\Cake\Model\Datasource\Database\Type\BinaryType',
'date' => '\Cake\Model\Datasource\Database\Type\DateType',
'datetime' => '\Cake\Model\Datasource\Database\Type\DateTimeType',
'time' => '\Cake\Model\Datasource\Database\Type\TimeType'
'boolean' => 'Cake\Model\Datasource\Database\Type\BooleanType',
'binary' => 'Cake\Model\Datasource\Database\Type\BinaryType',
'date' => 'Cake\Model\Datasource\Database\Type\DateType',
'datetime' => 'Cake\Model\Datasource\Database\Type\DateTimeType',
'time' => 'Cake\Model\Datasource\Database\Type\TimeType'
];

/**
Expand Down Expand Up @@ -86,16 +86,16 @@ public function __construct($name = null) {
* @return Type
*/
public static function build($name) {
if (isset(self::$_builtTypes[$name])) {
return self::$_builtTypes[$name];
if (isset(static::$_builtTypes[$name])) {
return static::$_builtTypes[$name];
}
if (isset(self::$_basicTypes[$name])) {
return self::$_builtTypes[$name] = new self($name);
if (isset(static::$_basicTypes[$name])) {
return static::$_builtTypes[$name] = new static($name);
}
if (!isset(self::$_types[$name])) {
throw new \InvalidArgumentException('No such type');
if (!isset(static::$_types[$name])) {
throw new \InvalidArgumentException(__d('cake_dev', 'Unknown type "%s"', $name));
}
return self::$_builtTypes[$name] = new self::$_types[$name]($name);
return static::$_builtTypes[$name] = new static::$_types[$name]($name);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions lib/Cake/Model/Datasource/Database/Type/DateType.php
Expand Up @@ -18,24 +18,36 @@
namespace Cake\Model\Datasource\Database\Type;

use Cake\Model\Datasource\Database\Driver;

use \DateTime;

class DateType 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');
}

/**
* 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;
}
$value = DateTime::createFromFormat('Y-m-d', $value);
return $value;
return DateTime::createFromFormat('Y-m-d', $value);
}

}
Expand Down
@@ -0,0 +1,70 @@
<?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
* @package Cake.Model
* @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\DateType;
use Cake\TestSuite\TestCase;

/**
* Test for the Date type.
*/
class DateTypeTest extends TestCase {

/**
* Setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->type = Type::build('date');
$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', $this->driver);
$this->assertInstanceOf('DateTime', $result);
$this->assertEqual('2001', $result->format('Y'));
$this->assertEqual('01', $result->format('m'));
$this->assertEqual('04', $result->format('d'));
}

/**
* Test converting to database format
*
* @return void
*/
public function testToDatabase() {
$value = '2001-01-04';
$result = $this->type->toDatabase($value, $this->driver);
$this->assertEquals($value, $result);

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

}

0 comments on commit f18a11e

Please sign in to comment.