Skip to content

Commit

Permalink
tests now live in those own direcory, todo added, introduced new Db c…
Browse files Browse the repository at this point in the history
…lasses
  • Loading branch information
Nemoden committed Mar 23, 2012
1 parent 2e61f7b commit 6f9be2a
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 55 deletions.
10 changes: 10 additions & 0 deletions DbCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?
/**
* @author Kirill "Nemoden" K
* $ Date: Fri 23 Mar 2012 04:36:40 PM VLAT $
*/

class DbCriteria {
public function __construct() {
}
}
15 changes: 15 additions & 0 deletions DbExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* @author Kirill "Nemoden" K
* $ Date: Fri 23 Mar 2012 10:56:32 AM VLAT $
*/

class DbExpression {
private $_expression;
public function __construct($expression) {
$this->_expression = $expression;
}
public function __toString() {
return (string)$this->_expression;
}
}
44 changes: 44 additions & 0 deletions DbQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require_once 'DbExpression.php';
class DbQueryBuilder {

private $_schema;
private $_table;
private $_bind_params = array();

public function setSchema($schema) {
$this->_schema = sprintf('`%s`', (string)$schema);
return $this;
}

public function setTable($table) {
$this->_table = sprintf('`%s`', (string)$table);
return $this;
}

public function insert($params) {
$template = 'INSERT INTO %s (%s) VALUES (%s)';
$dest = implode('.', array($this->_schema, $this->_table));
$keys = $values = array();
foreach ($params as $key => $param) {
$keys[] = sprintf('`%s`', $key);
if ($param instanceof DbExpression) {
$values[] = $param;
}
else {
$placeholder = sprintf(':%s', $key);
$values[] = sprintf(':%s', $key);
$this->_bind_params[$placeholder] = $param;
}
}
return sprintf($template, $dest, implode(', ', $keys), implode(', ', $values));
}

public function update() {
}

public function getBindParams() {
return $this->_bind_params;
}
}
45 changes: 30 additions & 15 deletions Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

require_once 'db.php';
require_once 'ModelException.php';
require_once 'DbExpression.php';
require_once 'DbQueryBuilder.php';

abstract class Model {

private $_dbh;
private $_error_info;
private $_queryBuilder; // DbQueryBuilder instance

private $_is_new_instance = true;
private $_a = array(); // attributes
Expand All @@ -24,6 +28,7 @@ abstract class Model {
* @TODO Model::getByPK(array('id1' => 10, 'id2' => 20)) or just array(10,20)
*/
protected $_pk = 'id';
protected $_schema = 'id';
protected $_table = 'id';

/**
Expand All @@ -38,6 +43,8 @@ abstract class Model {
public function __construct() {
global $dbh;
$this->_dbh = $dbh;
$this->_queryBuilder = new DbQueryBuilder();
$this->_queryBuilder->setTable($this->_table)->setSchema($this->_schema);

// we need table
if (empty($this->_table)) {
Expand All @@ -49,43 +56,51 @@ public function __construct() {
}

private function formatValues($v) {
if (!isset($this->datefields[$v])) {
return ':'.$v; // bind
if ($v instanceof DbExpression) {
return $v;
}
else {
return $v; // as is e.g. NOW()
return ':'.$v;
}
}

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

private function filterDbExpressions($v) {
return !($v instanceof DbExpression);
}

public function save() {
if ($this->isNewRecord()) {
$keys = array_keys($this->_a);
$values = array_map(create_function('$k', 'return ":".$k;'), array_keys($this->_a));
$keys = implode(', ', array_map(array($this, 'formatKeys'), $keys));
$values = implode(', ', array_map(array($this, 'formatValues'), array_keys($this->_a)));
$sql = 'INSERT INTO '.$this->_table.' ('.$keys.') VALUES ('.$values.')';
$statement = $this->_dbh->prepare($sql);
$result = $statement->execute($this->_a);
echo 'params: ';
echo $statement->debugDumpParams();
echo 'error info: ';
echo var_export($statement->errorInfo(),1);
echo '<<<<<<<<';

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

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

$values = array_map(create_function('$k,$v', 'return $k."=:".$v;'), array_keys($this->_a), $this->_a);
$sql = 'UPDATE '.$this->_table.' SET '.implode(', ', $values).' WHERE '.$this->_pk.' = '.$this->_a[$this->_pk];
$statement = $this->db->prepare($sql);
$result = $statement->execute($this->_a);

}
if (!$result) {
$this->_error_info = $this->_dbh->errorInfo();
}
return $result;
}

public function getErrorInfo() {
return $this->_error_info;
}

public function isNewRecord() {
return !isset($this->_a[$this->_pk]);
}
Expand Down
4 changes: 2 additions & 2 deletions ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require_once 'Model.php';

class ModelClass extends Model {
protected $_table = 'test.model_test';
protected $_schema = 'test';
protected $_table = 'model_test';
protected $_pk = 'id';
}

1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pick out all the Db* classes from here - they should remain as an independant project
30 changes: 0 additions & 30 deletions schema.sql

This file was deleted.

45 changes: 45 additions & 0 deletions tests/DbQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?

require_once 'PHPUnit/Autoload.php';
require_once '../DbExpression.php';
require_once '../DbQueryBuilder.php';

class DbQueryBuilderTest extends PHPUnit_Framework_TestCase {

private $queryBuilder;

/**
* each time create new DbQueryBuilder
*/
public function setUp() {
$this->queryBuilder = new DbQueryBuilder();
}

public function testCreateInsertQuery() {
$params = array(
'name' => 'Kirill',
'date' => new DbExpression('NOW()'),
'value' => 10,
);
$this->queryBuilder = new DbQueryBuilder();
$query = $this->queryBuilder->setSchema('test')->setTable('test')->insert($params);
$this->assertEquals('INSERT INTO `test`.`test` (`name`, `date`, `value`) VALUES (:name, NOW(), :value)', $query);
$this->assertEquals(array(':name' => 'Kirill', ':value' => '10'), $this->queryBuilder->getBindParams());
}

public function testUpdateQuery() {
$criteria = new DbCriteria();
$criteria->conditions = array('id = :id');
$query = $this->queryBuilder->update($update_fields, $criteria);
$this->markTestIncomplete();
}

public function testSelectQuery() {
$this->markTestIncomplete();
}

public function testJoinQuery() {
$this->markTestIncomplete();
}

}
27 changes: 20 additions & 7 deletions TestModel.php → tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*/

require_once 'PHPUnit/Autoload.php';
require_once 'ModelClass.php';
require_once '../ModelClass.php';
require_once '../DbExpression.php';
require_once '../DbDateTime.php';


/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/
class TestModel extends PHPUnit_Framework_TestCase {
class ModelTest extends PHPUnit_Framework_TestCase {

public static function setUpBeforeClass() {
global $dbh;
Expand Down Expand Up @@ -65,14 +67,25 @@ public function testCreateModel() {
$model= new ModelClass();
$model->string = 'lorem ipsum dolor';
$model->int = 10;
$model->date = 'NOW()';
$model->timestamp = 'NOW()';
$model->date = new DbExpression('NOW() + INTERVAL 10 HOUR');
$model->timestamp = new DbExpression('NOW()');
$model->decimal = 10.03;
$model->float = 10/3;
$this->assertTrue($model->save(), 'save fuckup');
$save_response = $model->save();
$this->assertTrue($save_response);
$this->assertEquals(10, $model->int);
echo $model->date;
$this->assertEquals(10, $model->date);
}

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();
$this->assertTrue($save_response);
$this->assertEquals(100000, $model->int);
}
}
2 changes: 1 addition & 1 deletion TestUser.php → tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

require_once 'PHPUnit/Autoload.php';

class TestUserTest extends PHPUnit_Framework_TestCase {
class UserTest extends PHPUnit_Framework_TestCase {

public function setUp() {
}
Expand Down
1 change: 1 addition & 0 deletions tests/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8" ?>
37 changes: 37 additions & 0 deletions tests/data/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--
-- @author Kirill "Nemoden" K
-- schema for tests (MySQL)
--

create database if not exists `test` default character set = utf8 default collate = utf8_general_ci;

use `test`;

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
`created_at` datetime not null default '0000-00-00 00:00:00', -- test dates
`updated_at` timstamp not null,
`age` tinyint(4) not null, -- test field setter
`gender` set('M', 'F') not null, -- test set
PRIMARY KEY `id` (`id`),
KEY `birthday` (`birthday`),
KEY `created_at` (`created_at`),
KEY `gender` (`gender`),
KEY `gender_age` (`gender`, `age`)
) ENGINE=InnoDB CHARSET=utf8 COMMENT='People table for test cases';

create table if not exists `users` (
`id` int(11) unsigned not null auto_increment,
`name` varchar(64) not null default '',
`email` varchar(64) not null default '',
`password` varchar(64) not null default '', -- test field setter
`created_at` datetime not null default '0000-00-00 00:00:00',
`updated_at` timestamp not null,
`email_confirmation_code` varchar(64) not null default '', -- test field setter
PRIMARY KEY `id` (`id`),
KEY `email_confirmation_code` (`email_confirmation_code`),
KEY `created_at` (`created_at`),
KEY `updated_at` (`updated_at`)) ENGINE=InnoDB CHARSET=utf8 COMMENT='Users';

0 comments on commit 6f9be2a

Please sign in to comment.