Skip to content
Browse files

major changes to unit test output and improvements to the ORM and ORM…

… test suite

Signed-off-by: Chris Granger <chris@wrenchlabs.com>
  • Loading branch information...
1 parent 4c0cc09 commit 80f006e21573907538e8f14b2cac2cd79c09e80b Chris Granger committed Sep 10, 2009
View
4 bindings/bindings.php
@@ -39,4 +39,6 @@
$this->event->register('sys::preOutput', 'profiler::addProfileInfo');
$this->event->register('sys::preForm', 'validator::dispatch');
-$this->event->register('url::'.config::get('unit.url'), 'unit::runUnits');
+$this->event->register('url::'.config::get('unit.url'), 'unit::runUnits');
+
+//$this->event->mapUrlToFolder('/__trigger__', 'sys/__trigger__');
View
240 components/tests/ormObjectHandling_test.php
@@ -21,21 +21,253 @@ public function setup() {
config::set("schema.user_test", array( "address_test" => RelTypes::HasMany ) );
config::set("schema.address_test", array( "addresstype" => RelTypes::RefsMany, "state" => RelTypes::RefsOne ) );
- $this->result = array(
+ $this->resultArray = array(
array( "1", "chris", "1", "308 108th ave ne", "1", "Home", "2", "WA"),
array( "1", "chris", "1", "308 108th ave ne", "2", "Current", "2", "WA"),
array( "1", "chris", "3", "12650 woodside falls rd", "3", "Original", "1", "NC" ),
array( "2", "ryan", "4", "12650 woodside falls rd", "1", "Home", "1", "NC" ),
);
}
+
+ public function hasOne_test() {
+ $resultArray = array(
+ array( "1", "ibdknox@mail.com", "1", "home"),
+ );
+
+ $email = (object) null;
+ $email->id = "1";
+ $email->address = "ibdknox@mail.com";
+ $email->type = (object) null;
+ $email->type->id = "1";
+ $email->type->value = "home";
+
+ $expected = array( $email );
+
+ $orm = ORM::select("email:address", "email.type:value");
+ $this->assertEquals($orm->buildObject($resultArray), $expected);
+
+ }
+
+ public function refsOne_test() {
+ $resultArray = array(
+ array( "1", "308 108th ave", "1", "home"),
+ );
+
+ $address = (object) null;
+ $address->id = "1";
+ $address->line1 = "308 108th ave";
+ $address->addresstype = (object) null;
+ $address->addresstype->id = "1";
+ $address->addresstype->value = "home";
+
+ $expected = array( $address );
+
+ $orm = ORM::select("address:line1", "address.addresstype:value");
+ $this->assertEquals($orm->buildObject($resultArray), $expected);
+
+ }
+
+ public function hasMany_test() {
+
+ $resultArray = array(
+ array( "1", "chris", "1", "308 108th ave ne"),
+ array( "1", "chris", "3", "12650 woodside falls rd"),
+ );
+
+ $chris = (object) null;
+ $chris->id = "1";
+ $chris->first = "chris";
+ $chris->address_test = array();
+ $chris->address_test[0] = (object) null;
+ $chris->address_test[0]->id = "1";
+ $chris->address_test[0]->line1 = "308 108th ave ne";
+ $chris->address_test[1] = (object) null;
+ $chris->address_test[1]->id = "3";
+ $chris->address_test[1]->line1 = "12650 woodside falls rd";
+
+ $expected = array( $chris );
+
+ $orm = ORM::select("user_test:first", "user_test.address_test:line1");
+ $this->assertEquals($orm->buildObject($resultArray), $expected);
+ }
+
+ public function refsMany_test() {
+
+ $resultArray = array(
+ array( "1", "awesome post!", "1", "newtag"),
+ array( "1", "awesome post!", "2", "besttag"),
+ );
+
+ $post = (object) null;
+ $post->id = "1";
+ $post->title = "awesome post!";
+ $post->tag = array();
+ $post->tag[0] = (object) null;
+ $post->tag[0]->id = "1";
+ $post->tag[0]->value = "newtag";
+ $post->tag[1] = (object) null;
+ $post->tag[1]->id = "2";
+ $post->tag[1]->value = "besttag";
+
+ $expected = array( $post );
+
+ $orm = ORM::select("post:title", "post.tag:value");
+ $this->assertEquals($orm->buildObject($resultArray), $expected);
+ }
+
+ public function complexObject_test() {
+
+ array_pop($this->resultArray);
+
+ $chris = (object) null;
+ $chris->id = "1";
+ $chris->first = "chris";
+ $chris->address_test = array();
+ $chris->address_test[0] = (object) null;
+ $chris->address_test[0]->id = "1";
+ $chris->address_test[0]->line1 = "308 108th ave ne";
+ $chris->address_test[0]->state = (object) null;
+ $chris->address_test[0]->state->id = "2";
+ $chris->address_test[0]->state->value = "WA";
+ $chris->address_test[0]->addresstype = array();
+ $chris->address_test[0]->addresstype[0] = (object) null;
+ $chris->address_test[0]->addresstype[0]->id = "1";
+ $chris->address_test[0]->addresstype[0]->value = "Home";
+ $chris->address_test[0]->addresstype[1] = (object) null;
+ $chris->address_test[0]->addresstype[1]->id = "2";
+ $chris->address_test[0]->addresstype[1]->value = "Current";
+ $chris->address_test[1] = (object) null;
+ $chris->address_test[1]->id = "3";
+ $chris->address_test[1]->line1 = "12650 woodside falls rd";
+ $chris->address_test[1]->state = (object) null;
+ $chris->address_test[1]->state->id = "1";
+ $chris->address_test[1]->state->value = "NC";
+ $chris->address_test[1]->addresstype = array();
+ $chris->address_test[1]->addresstype[0] = (object) null;
+ $chris->address_test[1]->addresstype[0]->id = "3";
+ $chris->address_test[1]->addresstype[0]->value = "Original";
+
+ $expected = array( $chris );
+
+ $cur = ORM::select("user_test:first", "user_test.address_test:line1", "user_test.address_test.state:value", "user_test.address_test.addresstype:value");
+
+ $this->assertEquals($cur->buildObject($this->resultArray), $expected);
+
+ }
- public function faulty_test() {
+ public function multiComplexObject_test() {
+
+ $chris = (object) null;
+ $chris->id = "1";
+ $chris->first = "chris";
+ $chris->address_test = array();
+ $chris->address_test[0] = (object) null;
+ $chris->address_test[0]->id = "1";
+ $chris->address_test[0]->line1 = "308 108th ave ne";
+ $chris->address_test[0]->state = (object) null;
+ $chris->address_test[0]->state->id = "2";
+ $chris->address_test[0]->state->value = "WA";
+ $chris->address_test[0]->addresstype = array();
+ $chris->address_test[0]->addresstype[0] = (object) null;
+ $chris->address_test[0]->addresstype[0]->id = "1";
+ $chris->address_test[0]->addresstype[0]->value = "Home";
+ $chris->address_test[0]->addresstype[1] = (object) null;
+ $chris->address_test[0]->addresstype[1]->id = "2";
+ $chris->address_test[0]->addresstype[1]->value = "Current";
+ $chris->address_test[1] = (object) null;
+ $chris->address_test[1]->id = "3";
+ $chris->address_test[1]->line1 = "12650 woodside falls rd";
+ $chris->address_test[1]->state = (object) null;
+ $chris->address_test[1]->state->id = "1";
+ $chris->address_test[1]->state->value = "NC";
+ $chris->address_test[1]->addresstype = array();
+ $chris->address_test[1]->addresstype[0] = (object) null;
+ $chris->address_test[1]->addresstype[0]->id = "3";
+ $chris->address_test[1]->addresstype[0]->value = "Original";
+
+ $ryan = (object) null;
+ $ryan->id = "2";
+ $ryan->first = "ryan";
+ $ryan->address_test = array();
+ $ryan->address_test[0] = (object) null;
+ $ryan->address_test[0]->id = "4";
+ $ryan->address_test[0]->line1 = "12650 woodside falls rd";
+ $ryan->address_test[0]->state = (object) null;
+ $ryan->address_test[0]->state->id = "1";
+ $ryan->address_test[0]->state->value = "NC";
+ $ryan->address_test[0]->addresstype = array();
+ $ryan->address_test[0]->addresstype[0] = (object) null;
+ $ryan->address_test[0]->addresstype[0]->id = "1";
+ $ryan->address_test[0]->addresstype[0]->value = "Home";
+
+ $expected = array( $chris, $ryan );
$cur = ORM::select("user_test:first", "user_test.address_test:line1", "user_test.address_test.state:value", "user_test.address_test.addresstype:value");
- profiler::debug($cur->buildObject($this->result));
- $this->assertEquals("a! woot", "awesome! woot");
+
+ $this->assertEquals($cur->buildObject($this->resultArray), $expected);
}
+
+ public function add_test() {
+
+ $email = (object) null;
+ $email->id = "1";
+ $email->address = "ibdknox@mail.com";
+ $email->type = (object) null;
+ $email->type->id = "1";
+ $email->type->value = "home";
+
+ $expected = "INSERT INTO `email` (`id`, `address`, `type_id`) VALUES ( '1', 'ibdknox@mail.com', '1' )";
+
+ $this->assertEquals(ORM::add('email', $email)->getSQL(), $expected);
+
+ }
+ public function multiAdd_test() {
+
+ $email = (object) null;
+ $email->id = "1";
+ $email->address = "ibdknox@mail.com";
+ $email->type = (object) null;
+ $email->type->id = "1";
+ $email->type->value = "home";
+
+
+ $email2 = (object) null;
+ $email2->id = "2";
+ $email2->address = "chris@mail.com";
+ $email2->type = (object) null;
+ $email2->type->id = "2";
+ $email2->type->value = "home";
+
+ $objToAdd = array( $email, $email2 );
+
+ $expected = "INSERT INTO `email` (`id`, `address`, `type_id`) VALUES ( '1', 'ibdknox@mail.com', '1' ), ( '2', 'chris@mail.com', '2' )";
+
+ $this->assertEquals(ORM::add('email', $objToAdd)->getSQL(), $expected);
+
+ }
+
+ public function update_test() {
+
+ $email = (object) null;
+ $email->id = "1";
+ $email->address = "ibdknox@mail.com";
+ $email->type = (object) null;
+ $email->type->id = "1";
+ $email->type->value = "home";
+
+ $expected = "UPDATE email SET email.id = '1', email.address = 'ibdknox@mail.com', email.type_id = '1' WHERE email.id = '1'";
+
+ $this->assertEquals(ORM::update('email', $email)->getSQL(), $expected);
+
+ }
+
+ public function delete_test() {
+
+ $expected = "DELETE FROM email WHERE email.id = '1'";
+
+ $this->assertEquals( ORM::delete('email', 1)->getSQL(), $expected );
+ }
}
View
145 helpers/orm/orm.php
@@ -339,7 +339,7 @@ public function buildObject($rows) {
$rowCounter = 0;
$numRows = count($rows);
- profiler::debug($this->_selectOrder);
+
while( $rowCounter < $numRows ) {
@@ -364,14 +364,14 @@ public function buildObject($rows) {
$curObject = $rootObject;
} else {
if( $this->_objectRoots[$table] != $curObjectName ) {
- profiler::debug($this->_objectRoots[$table]);
- profiler::debug($table);
+
+
$curObject = $prevObjects[$this->_objectRoots[$table]];
$curObjectName = $this->_objectRoots[$table];
}
$rel = $this->getRel($curObjectName, $table);
- profiler::debug($table);
+
if( $rel == RelTypes::HasMany || $rel == RelTypes::RefsMany ) {
@@ -384,17 +384,17 @@ public function buildObject($rows) {
$newArray =& $curObject->$table;
$newArray[] = (object) null;
$curObject = end($newArray);
- profiler::debug(print_r($curObject,true));
+
} else {
$curObject = end($curObject->$table);
}
} else if( !isset( $curObject->$table ) ) {
- profiler::debug($curObject);
- profiler::debug($table);
+
+
$curObject->$table = (object) null;
$curObject = $curObject->$table;
- profiler::debug($curObject);
+
} else {
$curObject = $curObject->$table;
}
@@ -409,8 +409,8 @@ public function buildObject($rows) {
}
$prevObjects[$curObjectName] = $curObject;
- profiler::debug(print_r($curObject,true));
- profiler::debug(print_r($rootObject, true));
+
+
}
$rowCounter++;
@@ -437,40 +437,60 @@ public function fetch($ID = false) {
$this->limitByKey($ID);
- if(!$this->sqlMode) {
- $result = $this->query($sql);
- return new resultObject($this->table, $result);
- } else {
- return $sql;
- }
-
+ $this->query($this->_sql);
}
+
+ public function delete($table, $id = null) {
+
+ $returnObj = new ORM();
+
+ $returnObj->_queryType = QueryTypes::Delete;
+ $returnObj->_sql = "DELETE FROM $table";
+
+ if( $id != null ) {
+ $returnObj->where("$table.id = '$id'");
+ }
+
+ return $returnObj;
+
+ }
public function update($table, $object) {
- $this->_queryType = QueryTypes::Update;
+ $returnObj = new ORM();
+
+ $returnObj->_queryType = QueryTypes::Update;
- $sql = "UPDATE $this->table SET ";
+ $sql = "UPDATE $table SET ";
foreach($object as $field => $value) {
+
if(!is_object($value) && !is_array($value)) {
- $sql .= "$this->table.$field = '$value', ";
- } else {
-
- $parts = $this->recursiveSave($field, $value);
- if( !empty( $parts ) ) {
- $sql .= "$parts[fieldName] = '$parts[fieldValue]', ";
- }
-
- }
+ $sql .= "$table.$field = '$value', ";
+ } else if( is_object($value) ) {
+
+ $rel = $returnObj->getRel($table, $field);
+ if( isset($value->id) && $rel == RelTypes::HasOne) {
+ $sql .= "$table.{$field}_id = '$value->id', ";
+ }
+
+ }
+
}
- $sql = substr($sql, 0, -2);
- $sql .= " WHERE $this->table.ID = '$object->ID'";
+ $returnObj->_sql = substr($sql, 0, -2);
+
+ if( isset( $object->id ) ) {
+ $returnObj->where("$table.id = '$object->id'");
+ }
+
+ return $returnObj;
}
public function add($table, $objectsToAdd) {
+
+ $returnObj = new ORM();
if( !is_array($objectsToAdd) ) {
@@ -484,48 +504,43 @@ public function add($table, $objectsToAdd) {
$fieldsString = '';
foreach($fields as $field) {
- if( is_object( $firstObject->$field ) || is_array( $firstObject->$field ) ) {
-
- $type = $this->findRelationship($this->table, $field);
- if($type == 'has_one') {
- $fieldsString .= "`{$field}_ID`, ";
- }
-
+ if( is_object( $firstObject->$field ) ) {
+ $fieldsString .= "`{$field}_id`, ";
} else {
$fieldsString .= "`$field`, ";
}
}
$fieldsString = substr($fieldsString, 0, -2);
- $sql = "INSERT INTO $this->table ($fieldsString) VALUES ";
+ $sql = "INSERT INTO `$table` ($fieldsString) VALUES ";
foreach($objectsToAdd as $object) {
$sql .= "( ";
foreach($fields as $curField) {
$value = $object->$curField;
+ $rel = $returnObj->getRel($table, $field);
+
if( !is_object($value) && !is_array($value) ) {
$sql .= "'$value', ";
- } else {
-
- $parts = $this->recursiveSave($curField, $value);
+ } else if ( is_object($value) && $rel == RelTypes::HasOne ) {
- if( !empty( $parts ) ) {
- $sql .= "'$parts[fieldValue]', ";
- }
+ if( isset( $value->id ) ) {
+ $sql .= "'$value->id', ";
+ } else {
+ $sql .= "'0', ";
+ }
}
}
$sql = substr($sql, 0, -2) . ' ), ';
}
$sql = substr($sql, 0, -2);
-
- if(!$this->sqlMode) {
- //TODO: perform query
- } else {
- return $sql;
- }
+
+ $returnObj->_sql = $sql;
+ $returnObj->_queryType = QueryTypes::Insert;
+ return $returnObj;
}
@@ -539,7 +554,7 @@ public function getNumFound() {
}
- private function getSelectSQL() {
+ private function prepareSelectSQL() {
$sql = "SELECT ";
$sql .= $this->concatSection($this->_modifiers, ' ', ' ');
@@ -576,38 +591,52 @@ private function getSelectSQL() {
}
//TODO : Implement
- private function getDeleteSQL() {
+ private function prepareDeleteSQL() {
+
+ $this->_sql .= $this->concatSection($this->_where, ' WHERE ');
+
+ if( !empty( $this->_limit ) ) {
+ $this->_sql .= " LIMIT $this->_limit";
+ }
}
//TODO : Implement
- private function getInsertSQL() {
+ private function prepareInsertSQL() {
+
+ $this->_sql;
}
//TODO : Implement
- private function getUpdateSQL() {
+ private function prepareUpdateSQL() {
+ $this->_sql .= $this->concatSection($this->_where, ' WHERE ');
+
+ if( !empty( $this->_limit ) ) {
+ $this->_sql .= " LIMIT $this->_limit";
+ }
+
}
public function getSQL() {
switch($this->_queryType) {
case QueryTypes::Select:
- $this->getSelectSQL();
+ $this->prepareSelectSQL();
break;
case QueryTypes::Delete:
- $this->getDeleteSQL();
+ $this->prepareDeleteSQL();
break;
case QueryTypes::Insert:
- $this->getInsertSQL();
+ $this->prepareInsertSQL();
break;
case QueryTypes::Update:
- $this->getUpdateSQL();
+ $this->prepareUpdateSQL();
break;
}
View
48 helpers/unit/output.php
@@ -1,55 +1,13 @@
<html>
<head>
<title>Test Runner</title>
- <style type="text/css">
-
- /* Reset browser styles: get to a known state. */
- html, body, h1, h2, h3, h4, h5, h6, table, tr, th, td, form, fieldset, select, input, textarea, dl, dt, dd, ul, ol, li, address, blockquote, pre, code { margin: 0; padding: 0; }
- body { font: normal 62.5% Verdana, sans-serif; }
- h1, h2, h3, h4, h5, h6 { font-size: 100%; }
-
- a { outline: none; }
- img { border: none; }
-
- p { margin: 1em 0; }
- * html p { margin: .83333em 0; }
-
- /* Doc flow. */
- .block { display: block !important; }
- .inline { display: inline !important; }
- .left { float: left !important; display: inline !important; }
- .right { float: right !important; display: inline !important; }
- .clear { clear: both !important; }
-
- /* Margins. */
- .collapse { margin: 0 !important; }
- .first { margin-left: 0 !important; }
- .last { margin-right: 0 !important; }
- .top { margin-top: 0 !important; }
- .bottom { margin-bottom: 0 !important; }
-
- body { background: #0c0f12; color:#ccc; font-family: Verdana, Arial, serif; font-size:110%; }
- div.outerwrapper { margin: 0 auto; margin-top:40px; width: 800px; }
- div.test { margin-bottom: 10px; background:#161B21; border: 1px solid #384554; padding:15px; }
- .failed { color: #FF8934; }
- .passed { color: #3FFF6E; }
-
- span { font-size:80%; }
-
- ul { list-style:none; margin-left: 25px; margin-top:15px; }
- ul li { margin-bottom:5px; background:#2D3545; color:#ccc; }
- ul.failed li { border: 1px solid #FF8934; padding:10px; background: #451E17; }
- ul.failed li h3 { color: #FF8934; }
- ul.passed li { color: #3FFF6E; border: 1px solid #3FFF6E; padding:10px; background:#2B4531; }
-
- del { color: #FF8934; }
- ins { color: #3FFF6E; }
-
- </style>
+ <?php echo $this->partial("script", array(), "helpers/unit/"); ?>
+ <?php echo $this->partial("style", array(), "helpers/unit/"); ?>
</head>
<body>
<div class="outerwrapper">
+ <a href="#" class="failToggle onlyFailed">Failed tests only</a>
<?php foreach( unit::$units as $test => $result ) { ?>
<div class="test">
<h2 class="<?php echo unit::passFail($result); ?>">
View
18 helpers/unit/script.php
@@ -0,0 +1,18 @@
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"> </script>
+ <script type="text/javascript">
+ $(document).ready( function() {
+ $("a.failToggle").click( function(event) {
+
+ $(this).toggleClass("onlyFailed").toggleClass("allTests");
+ $("ul.passed").toggle('fast');
+
+ if( $(this).hasClass("allTests") ) {
+ $(this).text("Show all tests");
+ } else {
+ $(this).text("Failed tests only");
+ }
+
+ });
+
+ });
+ </script>
View
49 helpers/unit/style.php
@@ -0,0 +1,49 @@
+ <style type="text/css">
+
+ /* Reset browser styles: get to a known state. */
+ html, body, h1, h2, h3, h4, h5, h6, table, tr, th, td, form, fieldset, select, input, textarea, dl, dt, dd, ul, ol, li, address, blockquote, pre, code { margin: 0; padding: 0; }
+ body { font: normal 62.5% Verdana, sans-serif; }
+ h1, h2, h3, h4, h5, h6 { font-size: 100%; }
+
+ a { outline: none; }
+ img { border: none; }
+
+ p { margin: 1em 0; }
+ * html p { margin: .83333em 0; }
+
+ /* Doc flow. */
+ .block { display: block !important; }
+ .inline { display: inline !important; }
+ .left { float: left !important; display: inline !important; }
+ .right { float: right !important; display: inline !important; }
+ .clear { clear: both !important; }
+
+ /* Margins. */
+ .collapse { margin: 0 !important; }
+ .first { margin-left: 0 !important; }
+ .last { margin-right: 0 !important; }
+ .top { margin-top: 0 !important; }
+ .bottom { margin-bottom: 0 !important; }
+
+ body { background: #0c0f12; color:#ccc; font-family: Verdana, Arial, serif; font-size:110%; }
+ div.outerwrapper { margin: 0 auto; margin-top:40px; width: 800px; }
+ div.test { margin-bottom: 10px; background:#161B21; border: 1px solid #384554; padding:15px; }
+ .failed { color: #FF8934; }
+ .passed { color: #3FFF6E; }
+
+ .failToggle { display: block; width: 200px;}
+ .onlyFailed { border: 1px solid #FF8934; padding:5px; background: #451E17; color: #FF8934; text-decoration:none; }
+ .allTests { border: 1px solid #3FFF6E; padding:5px; background: #2B4531; color: #3FFF6E; text-decoration:none; }
+
+ span { font-size:80%; }
+
+ ul { list-style:none; margin-left: 25px; margin-top:15px; }
+ ul li { margin-bottom:5px; background:#2D3545; color:#ccc; }
+ ul.failed li { border: 1px solid #FF8934; padding:10px; background: #451E17; }
+ ul.failed li h3 { color: #FF8934; }
+ ul.passed li { color: #3FFF6E; border: 1px solid #3FFF6E; padding:10px; background:#2B4531; }
+
+ del { color: #FF8934; }
+ ins { color: #3FFF6E; }
+
+ </style>
View
8 sys/core/loader.php
@@ -21,6 +21,14 @@ public function loadCore() {
$this->config();
}
+
+ public function helper($path) {
+
+ if( file_exists( HELPERSDIR . "/$path.php" ) ) {
+ include( HELPERSDIR . "/$path.php" );
+ }
+
+ }
public function component($name, $path = false) {

0 comments on commit 80f006e

Please sign in to comment.
Something went wrong with that request. Please try again.