From b430f6ec2e374e648448aa3f8d825bc3c05c00ec Mon Sep 17 00:00:00 2001 From: "Kirill \"Nemoden\" K" Date: Mon, 26 Mar 2012 14:57:24 +1100 Subject: [PATCH] resolvers added --- DbDate.php | 12 ++++++ Model.php | 36 ++++++++++------ exceptions/ResolverException.php | 5 +++ tests/DbQueryBuilderTest.php | 6 +++ tests/ModelTest.php | 70 +++++++------------------------- tests/Person.php | 28 +++++++++++++ tests/UserTest.php | 4 ++ tests/data/schema.sql | 4 +- 8 files changed, 94 insertions(+), 71 deletions(-) create mode 100644 DbDate.php create mode 100644 exceptions/ResolverException.php create mode 100644 tests/Person.php diff --git a/DbDate.php b/DbDate.php new file mode 100644 index 0000000..bf6fbdb --- /dev/null +++ b/DbDate.php @@ -0,0 +1,12 @@ +_format); + } +} diff --git a/Model.php b/Model.php index 71e7d57..adfa624 100644 --- a/Model.php +++ b/Model.php @@ -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 @@ -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(); @@ -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) { } diff --git a/exceptions/ResolverException.php b/exceptions/ResolverException.php new file mode 100644 index 0000000..ffbbf04 --- /dev/null +++ b/exceptions/ResolverException.php @@ -0,0 +1,5 @@ +conditions = array('id = :id'); + $update_fields = array(); $query = $this->queryBuilder->update($update_fields, $criteria); $this->markTestIncomplete(); } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index ac8b892..0aac4ea 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -8,6 +8,8 @@ require_once '../ModelClass.php'; require_once '../DbExpression.php'; require_once '../DbDateTime.php'; +require_once '../DbDate.php'; +require_once 'Person.php'; /** @@ -17,44 +19,9 @@ class ModelTest extends PHPUnit_Framework_TestCase { public static function setUpBeforeClass() { - global $dbh; - $create_database = <<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() { @@ -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); } + } diff --git a/tests/Person.php b/tests/Person.php new file mode 100644 index 0000000..6243a7e --- /dev/null +++ b/tests/Person.php @@ -0,0 +1,28 @@ + '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; + } + +} + diff --git a/tests/UserTest.php b/tests/UserTest.php index e9ebb94..c762498 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -6,6 +6,10 @@ require_once 'PHPUnit/Autoload.php'; +/** + * @backupGlobals disabled + * @backupStaticAttributes disabled + */ class UserTest extends PHPUnit_Framework_TestCase { public function setUp() { diff --git a/tests/data/schema.sql b/tests/data/schema.sql index 236a735..9f74287 100644 --- a/tests/data/schema.sql +++ b/tests/data/schema.sql @@ -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`),