Skip to content

Commit

Permalink
resolvers added
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemoden committed Mar 26, 2012
1 parent 6f9be2a commit b430f6e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 71 deletions.
12 changes: 12 additions & 0 deletions DbDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @author Kirill "Nemoden" K
* $ Date: Mon 26 Mar 2012 02:12:57 PM VLAST $
*/

class DbDate extends DateTime {
private $_format = 'Y-m-d';
public function __toString() {
return (string)parent::format($this->_format);
}
}
36 changes: 23 additions & 13 deletions Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,34 @@ public function __construct() {
}
}

private function formatValues($v) {
if ($v instanceof DbExpression) {
return $v;
}
else {
return ':'.$v;
}
protected function resolvers() {
return array();
}

private function formatKeys($k) {
return '`'.$k.'`';
}

private function filterDbExpressions($v) {
return !($v instanceof DbExpression);
protected function applyResolvers() {
$resolvers = $this->resolvers();
foreach ($resolvers as $resolver) {
$this->$resolver();
}
}

public function save() {

$this->applyResolvers();


if ($this->isNewRecord()) {

$query = $this->_queryBuilder->insert($this->_a);
$statement = $this->_dbh->prepare($query);
$result = $statement->execute($this->_queryBuilder->getBindParams());
echo $query;
var_export($this->_queryBuilder->getBindparams());

if ($result) {
$this->_a[$this->_pk] = $this->_dbh->lastInsertId();
}

}
else { // update

Expand All @@ -107,6 +108,11 @@ public function isNewRecord() {

// get an attribute
public function __get($name) {
$resolvers = $this->resolvers();
if (isset($resolvers[$name])) {
$resolver = $resolvers[$name];
$this->$resolver();
}
if (method_exists($this, 'get'.ucfirst($name))) {
$method = 'get'.ucfirst($name);
return $this->$method();
Expand All @@ -124,6 +130,10 @@ public function __set($name, $value) {
$this->_a[$name] = $value;
}
}

public function __isset($name) {
return isset($this->_a[$name]);
}

public function __call($fn, $args) {
}
Expand Down
5 changes: 5 additions & 0 deletions exceptions/ResolverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class ResolverException extends Exception {

}
6 changes: 6 additions & 0 deletions tests/DbQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
require_once 'PHPUnit/Autoload.php';
require_once '../DbExpression.php';
require_once '../DbQueryBuilder.php';
require_once '../DbCriteria.php';

/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/
class DbQueryBuilderTest extends PHPUnit_Framework_TestCase {

private $queryBuilder;
Expand All @@ -30,6 +35,7 @@ public function testCreateInsertQuery() {
public function testUpdateQuery() {
$criteria = new DbCriteria();
$criteria->conditions = array('id = :id');
$update_fields = array();
$query = $this->queryBuilder->update($update_fields, $criteria);
$this->markTestIncomplete();
}
Expand Down
70 changes: 14 additions & 56 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require_once '../ModelClass.php';
require_once '../DbExpression.php';
require_once '../DbDateTime.php';
require_once '../DbDate.php';
require_once 'Person.php';


/**
Expand All @@ -17,44 +19,9 @@
class ModelTest extends PHPUnit_Framework_TestCase {

public static function setUpBeforeClass() {
global $dbh;
$create_database = <<<SQL
create database if not exists `test` default character set = utf8 default collate = utf8_general_ci;
SQL;

$create_table = <<<SQL
create table if not exists `test`.`model_test` (
`id` int(11) unsigned not null auto_increment,
`string` varchar(255) not null default '',
`date` datetime not null default '0000-00-00 00:00:00',
`timestamp` timestamp,
`decimal` decimal(10,2) not null default 0.0,
`float` float not null default 0,
`int` int(11) unsigned not null default '0',
PRIMARY KEY `id` (`id`)
) ENGINE=InnoDB COMMENT='testing active record';
SQL;
try {
$dbh->beginTransaction();
$res1 = $dbh->exec($create_database);
$res2 = $dbh->exec(str_replace(PHP_EOL, '', $create_table));
if ($dbh->errorCode()) {
list($sql_error, $driver_error, $error_message) = $dbh->errorInfo();
echo $error_message;
}
$dbh->commit();
}
catch (Exception $e) {
echo $e->getMessage();
}
catch (PDOException $e) {
echo $e->getMessage();
}
}

public static function tearDownAfterClass() {
global $dbh;
# $res = $dbh->exec('drop table `model_test`');
}

public function setUp() {
Expand All @@ -64,28 +31,19 @@ public function tearDown() {
}

public function testCreateModel() {
$model= new ModelClass();
$model->string = 'lorem ipsum dolor';
$model->int = 10;
$model->date = new DbExpression('NOW() + INTERVAL 10 HOUR');
$model->timestamp = new DbExpression('NOW()');
$model->decimal = 10.03;
$model->float = 10/3;
$save_response = $model->save();
$this->assertTrue($save_response);
$this->assertEquals(10, $model->int);
}

public function testCreateModel2() {
$model= new ModelClass();
$model->string = "The name of the song was: \"Qui pense a l'amour, a l'amour\"";
$model->int = 100000;
$model->date = new DbDateTime('NOW - 100 YEAR + 2 HOUR');
$model->timestamp = new DbExpression('NOW()');
$model->decimal = 10.03;
$model->float = 10/3;
$save_response = $model->save();
$person = new Person();
$person->name = 'Kirill';
$person->address = 'Nei\'buta str.';
$person->birthday = new DbExpression('NOW()');
$person->created_at = new DbDateTime('26 Mar 2012');
$person->save();

$this->assertEquals(53, $person->age);
$person->birthday = new DbDate('30 Dec 1986');
$this->assertEquals(25, $person->age);
$save_response = $person->save();
$this->assertTrue($save_response);
$this->assertEquals(100000, $model->int);
}

}
28 changes: 28 additions & 0 deletions tests/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @author Kirill "Nemoden" K
* $ Date: Mon 26 Mar 2012 01:39:00 PM VLAST $
*/
require_once '../Model.php';
require_once '../exceptions/ResolverException.php';

class Person extends Model {
protected $_table = 'people';
protected $_schema = 'test';
protected $_pk = 'id';

protected function resolvers() {
return array('age' => 'resolveAge');
}

protected function resolveAge() {
if (!isset($this->birthday) || (!($this->birthday instanceof DateTime))) {
throw new ResolverException('Birthday field is not set or not DateTime instance');
}
$now = new DateTime();
$interval = date_diff($now, $this->birthday);
$this->age = $interval->y;
}

}

4 changes: 4 additions & 0 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

require_once 'PHPUnit/Autoload.php';

/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/
class UserTest extends PHPUnit_Framework_TestCase {

public function setUp() {
Expand Down
4 changes: 2 additions & 2 deletions tests/data/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ create table if not exists `people` (
`id` int(11) unsigned not null auto_increment,
`name` varchar(255) not null default '',
`address` text,
`birthday` datetime not null default '0000-00-00 00:00:00', -- test dates < 1970
`birthday` date not null default '0000-00-00', -- test dates < 1970
`created_at` datetime not null default '0000-00-00 00:00:00', -- test dates
`updated_at` timstamp not null,
`updated_at` timestamp not null,
`age` tinyint(4) not null, -- test field setter
`gender` set('M', 'F') not null, -- test set
PRIMARY KEY `id` (`id`),
Expand Down

1 comment on commit b430f6e

@small-jeeper
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Клевый комит!

Please sign in to comment.