Started unit tests for condition and argument classes.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0-DB@8577 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ucorina 2011-07-07 14:34:56 +00:00
parent 7112d518c8
commit 76019a4b2b
15 changed files with 537 additions and 108 deletions

View file

@ -31,6 +31,7 @@
} }
function toString($withValue = true){ function toString($withValue = true){
if(!$this->show()) return '';
if($withValue) if($withValue)
return $this->toStringWithValue(); return $this->toStringWithValue();
return $this->toStringWithoutValue(); return $this->toStringWithoutValue();
@ -51,6 +52,7 @@
} }
function show(){ function show(){
if($this->hasArgument() && !$this->argument->isValid()) return false;
switch($this->operation) { switch($this->operation) {
case 'equal' : case 'equal' :
case 'more' : case 'more' :

View file

@ -17,113 +17,114 @@
function createConditionValue(){ function createConditionValue(){
if(!isset($this->value)) return; if(!isset($this->value)) return;
$name = $this->column_name; $name = $this->column_name;
$operation = $this->operation; $operation = $this->operation;
$value = $this->value; $value = $this->value;
switch($operation) {
case 'like_prefix' :
$this->value = $value.'%';
break;
case 'like_tail' :
$this-> value = '%'.$value;
break;
case 'like' :
$this->value = '%'.$value.'%';
break;
case 'in' :
if(is_array($value))
{
//$value = $this->addQuotesArray($value);
//if($type=='number') return join(',',$value);
//else
//$this->value = "['". join("','",$value)."']";
}
else
{
$this->value = $value;
}
break;
}
/*
//if(!in_array($operation,array('in','notin','between')) && is_array($value)){
// $value = join(',', $value);
//}
// Daca operatia nu este in, notin, between si coloana e de tip numeric
// daca valoarea e array -> concatenare
// daca valoarea nu e array si nici nu contine paranteze (nu e functie) -> return (int)
// altfel return valoare
// if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
// if(is_array($value)){
// $value = join(',',$value);
// }
// if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
// return $value;
// }
//
// if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
// list($table_name, $column_name) = explode('.', $value);
// if($column_type[$column_name]) return $value;
// }
switch($operation) { switch($operation) {
case 'like_prefix' : case 'like_prefix' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value); $this->value = $value.'%';
$value = $value.'%'; break;
break; case 'like_tail' :
case 'like_tail' : $this->value = '%'.$value;
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value); break;
$value = '%'.$value; case 'like' :
break; $this->value = '%'.$value.'%';
case 'like' : break;
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value); case 'in' :
$value = '%'.$value.'%'; if(is_array($value))
break; {
// case 'notin' : //$value = $this->addQuotesArray($value);
// if(is_array($value)) if($this->getType() == 'number')
// { $this->value = "(" . join(',',$value) . ")";
// $value = $this->addQuotesArray($value); else
// if($type=='number') return join(',',$value); $this->value = "('". join("','",$value)."')";
// else return "'". join("','",$value)."'"; }
// } else
// else {
// { $this->value = $value;
// return $value; }
// } break;
// break; }
// case 'in' : /*
// if(is_array($value)) //if(!in_array($operation,array('in','notin','between')) && is_array($value)){
// { // $value = join(',', $value);
// $value = $this->addQuotesArray($value); //}
// if($type=='number') return join(',',$value); // Daca operatia nu este in, notin, between si coloana e de tip numeric
// else return "'". join("','",$value)."'"; // daca valoarea e array -> concatenare
// } // daca valoarea nu e array si nici nu contine paranteze (nu e functie) -> return (int)
// else // altfel return valoare
// {
// return $value; // if(!in_array($operation,array('in','notin','between')) && $type == 'number') {
// } // if(is_array($value)){
// break; // $value = join(',',$value);
// case 'between' : // }
// if(!is_array($value)) $value = array($value); // if(strpos($value, ',') === false && strpos($value, '(') === false) return (int)$value;
// $value = $this->addQuotesArray($value); // return $value;
// if($type!='number') // }
// { //
// foreach($value as $k=>$v) // if(!is_array($value) && strpos($name, '.') !== false && strpos($value, '.') !== false) {
// { // list($table_name, $column_name) = explode('.', $value);
// $value[$k] = "'".$v."'"; // if($column_type[$column_name]) return $value;
// } // }
// }
switch($operation) {
case 'like_prefix' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = $value.'%';
break;
case 'like_tail' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = '%'.$value;
break;
case 'like' :
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
$value = '%'.$value.'%';
break;
// case 'notin' :
// if(is_array($value))
// {
// $value = $this->addQuotesArray($value);
// if($type=='number') return join(',',$value);
// else return "'". join("','",$value)."'";
// }
// else
// {
// return $value;
// }
// break;
// case 'in' :
// if(is_array($value))
// {
// $value = $this->addQuotesArray($value);
// if($type=='number') return join(',',$value);
// else return "'". join("','",$value)."'";
// }
// else
// {
// return $value;
// }
// break;
// case 'between' :
// if(!is_array($value)) $value = array($value);
// $value = $this->addQuotesArray($value);
// if($type!='number')
// {
// foreach($value as $k=>$v)
// {
// $value[$k] = "'".$v."'";
// }
// }
//return $value;
break;
default:
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
}
$this->value = $value;
//return "'".$this->addQuotes($value)."'";
*/
//return $value;
break;
default:
if(!is_array($value)) $value = preg_replace('/(^\'|\'$){1}/', '', $value);
}
$this->value = $value;
//return "'".$this->addQuotes($value)."'";
*/
} }
function getType(){ function getType(){

View file

@ -23,8 +23,8 @@
if(!$name) $name = $tag->attrs->column; if(!$name) $name = $tag->attrs->column;
if(strpos($name, '.') === false) $this->column_name = $name; if(strpos($name, '.') === false) $this->column_name = $name;
else { else {
list($prefix, $name) = explode('.', $name); list($prefix, $name) = explode('.', $name);
$this->column_name = $name; $this->column_name = $name;
} }
if($tag->attrs->operation) $this->operation = $tag->attrs->operation; if($tag->attrs->operation) $this->operation = $tag->attrs->operation;

View file

@ -48,7 +48,8 @@
$arguments = array(); $arguments = array();
if($this->query) if($this->query)
$arguments = array_merge($arguments, $this->query->getArguments()); $arguments = array_merge($arguments, $this->query->getArguments());
$arguments[] = $this->argument; if($this->argument)
$arguments[] = $this->argument;
return $arguments; return $arguments;
} }

View file

@ -0,0 +1,43 @@
<?php
/**
* Test class for Condition.
*/
class ConditionTest extends CubridTest {
/**
* Checks equal operation
*/
public function testConditionString_Equal_WithoutPipe_NumericValue() {
$member_srl_argument = new ConditionArgument('"member_srl"', 20, 'equal');
$tag = new Condition('"member_srl"', $member_srl_argument, 'equal');
$this->assertEquals(' "member_srl" = 20', $tag->toString());
}
/**
* Checks equal operation
*/
public function testConditionString_Equal_WithPipe_NumericValue() {
$member_srl_argument = new ConditionArgument('"member_srl"', 20, 'equal');
$tag = new Condition('"member_srl"', $member_srl_argument, 'equal', 'and');
$this->assertEquals('and "member_srl" = 20', $tag->toString());
}
/**
* Checks condition returns nothing when argument is not valid
*/
public function testConditionString_InvalidArgument() {
$member_srl_argument = new ConditionArgument('"member_srl"', null, 'equal');
$member_srl_argument->checkNotNull();
$tag = new Condition('"member_srl"', $member_srl_argument, 'equal', 'and');
$this->assertEquals('', $tag->toString());
}
}
?>

View file

@ -0,0 +1,232 @@
<?php
/**
* Test class for Argument.
* Generated by PHPUnit on 2011-07-07 at 16:51:29.
*/
class ArgumentTest extends CubridTest {
/**
* @todo Implement testGetType().
*/
public function testGetType() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testSetColumnType().
*/
public function testSetColumnType() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetName().
*/
public function testGetName() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetValue().
*/
public function testGetValue() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetUnescapedValue().
*/
public function testGetUnescapedValue() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testToString().
*/
public function testToString() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testEscapeValue().
*/
public function testEscapeValue() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testIsValid().
*/
public function testIsValid() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetErrorMessage().
*/
public function testGetErrorMessage() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testEnsureDefaultValue().
*/
public function testEnsureDefaultValue() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testCheckFilter().
*/
public function testCheckFilter() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testCheckMaxLength().
*/
public function testCheckMaxLength() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testCheckMinLength().
*/
public function testCheckMinLength() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* Checks that argument is valid after a notnull check when value is not null
*/
public function testCheckNotNullWhenNotNull() {
$member_srl_argument = new ConditionArgument('member_srl', 20, 'equal');
$member_srl_argument->checkNotNull();
$this->assertEquals(true, $member_srl_argument->isValid());
}
/**
* Checks that argument becomes invalid after a notnull check when value is null
*/
public function testCheckNotNullWhenNull() {
$member_srl_argument = new ConditionArgument('member_srl', null, 'equal');
$member_srl_argument->checkNotNull();
$this->assertEquals(false, $member_srl_argument->isValid());
}
/**
* Checks that argument value stays the same when both user value and default value are given
*/
public function testCheckDefaultValueWhenNotNull() {
$member_srl_argument = new ConditionArgument('member_srl', 20, 'equal');
$member_srl_argument->ensureDefaultValue(25);
$this->assertEquals(20, $member_srl_argument->getValue());
}
/**
* Checks that argument value gets set when user value is null and default value is specified
*/
public function testCheckDefaultValueWhenNull() {
$member_srl_argument = new ConditionArgument('member_srl', null, 'equal');
$member_srl_argument->ensureDefaultValue(25);
$this->assertEquals(25, $member_srl_argument->getValue());
}
/**
* Checks like prefix operation
*/
public function testCreateConditionValue_LikePrefix() {
$member_srl_argument = new ConditionArgument('"mid"', 'forum', 'like_prefix');
$member_srl_argument->createConditionValue();
$this->assertEquals('forum%', $member_srl_argument->getValue());
}
/**
* Checks like tail operation
*/
public function testCreateConditionValue_LikeTail() {
$member_srl_argument = new ConditionArgument('"mid"', 'forum', 'like_tail');
$member_srl_argument->createConditionValue();
$this->assertEquals('%forum', $member_srl_argument->getValue());
}
/**
* Checks like operation
*/
public function testCreateConditionValue_Like() {
$member_srl_argument = new ConditionArgument('"mid"', 'forum', 'like');
$member_srl_argument->createConditionValue();
$this->assertEquals('%forum%', $member_srl_argument->getValue());
}
/**
* Checks in operation
*/
public function testCreateConditionValue_In_StringValues() {
$member_srl_argument = new ConditionArgument('"mid"', array('forum', 'board'), 'in');
$member_srl_argument->createConditionValue();
$this->assertEquals('(\'forum\',\'board\')', $member_srl_argument->getValue());
}
/**
* Checks in operation
*/
public function testCreateConditionValue_In_NumericValues() {
$member_srl_argument = new ConditionArgument('"module_srl"', array(3, 21), 'in');
$member_srl_argument->setColumnType('number');
$member_srl_argument->createConditionValue();
$this->assertEquals('(3,21)', $member_srl_argument->getValue());
}
}
?>

View file

@ -0,0 +1,40 @@
<?php
/**
* Test class for ConditionArgument.
*/
class ConditionArgumentTest extends CubridTest {
/**
* @todo Implement testCreateConditionValue().
*/
public function testCreateConditionValue() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testGetType().
*/
public function testGetType() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testSetColumnType().
*/
public function testSetColumnType() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
?>

View file

@ -0,0 +1,35 @@
<?php
/**
* Test class for QueryArgument.
*/
class QueryArgumentTest extends CubridTest {
var $xmlPath = "data/";
function QueryArgumentClass(){
$this->xmlPath = str_replace('QueryArgumentTest.php', '', str_replace('\\', '/', __FILE__)) . $this->xmlPath;
}
function testNotNullConditionArgument(){
$xml_file = $this->xmlPath . "condition1.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new QueryArgument($xml_obj->condition);
$this->assertEquals("member_srl", $tag->getArgumentName());
$this->assertEquals("member_srl", $tag->getColumnName());
$this->assertEquals(true, $tag->isConditionArgument());
$actual = $tag->toString();
$expected = ' $member_srl_argument = new ConditionArgument(\'member_srl\', $args->member_srl, \'equal\');
$member_srl_argument->checkNotNull();
$member_srl_argument->createConditionValue();
if(!$member_srl_argument->isValid()) return $member_srl_argument->getErrorMessage();';
$this->assertEquals($expected, $actual);
}
}
?>

View file

@ -0,0 +1 @@
<condition operation="equal" column="member_srl" var="member_srl" notnull="notnull" />

View file

@ -0,0 +1 @@
<index var="sort_index" default="list_order" order="order_type" />

View file

@ -0,0 +1,68 @@
<?php
/**
* Test class for ConditionTag.
*/
class ConditionTagTest extends CubridTest {
var $xmlPath = "data/";
function ConditionTagTest(){
$this->xmlPath = str_replace('ConditionTagTest.php', '', str_replace('\\', '/', __FILE__)) . $this->xmlPath;
}
/**
* Tests a simple <condition> tag:
* <condition operation="equal" column="user_id" var="user_id" />
*/
function testConditionStringWithArgument(){
$xml_file = $this->xmlPath . "condition1.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new ConditionTag($xml_obj->condition);
$expected = "new Condition('\"user_id\"',\$user_id_argument,\"equal\")";
$actual = $tag->getConditionString();
$this->assertEquals($expected, $actual);
$arguments = $tag->getArguments();
$this->assertEquals(1, count($arguments));
}
/**
* Tests a condition tag for joins - that uses no argument
* <condition operation="equal" column="comments.user_id" default="member.user_id" filter="userid" />
*/
function testConditionStringWithoutArgument(){
$xml_file = $this->xmlPath . "condition3.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new ConditionTag($xml_obj->condition);
$expected = "new Condition('\"comments\".\"user_id\"','\"member\".\"user_id\"',\"equal\")";
$actual = $tag->getConditionString();
$this->assertEquals($expected, $actual);
$arguments = $tag->getArguments();
$this->assertEquals(0, count($arguments));
}
/**
* Tests a <condition> tag with pipe:
* <condition operation="equal" column="type" var="type" notnull="notnull" pipe="and" />
*/
function testConditionStringWithPipe(){
$xml_file = $this->xmlPath . "condition2.xml";
$xml_obj = Helper::getXmlObject($xml_file);
$tag = new ConditionTag($xml_obj->condition);
$expected = "new Condition('\"type\"',\$type_argument,\"equal\", 'and')";
$actual = $tag->getConditionString();
$this->assertEquals($expected, $actual);
$arguments = $tag->getArguments();
$this->assertEquals(1, count($arguments));
}
}
?>

View file

@ -0,0 +1 @@
<condition operation="equal" column="user_id" var="user_id" />

View file

@ -0,0 +1 @@
<condition operation="equal" column="type" var="type" notnull="notnull" pipe="and" />

View file

@ -0,0 +1 @@
<condition operation="equal" column="comments.user_id" default="member.user_id" filter="userid" />

View file

@ -41,5 +41,7 @@
require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php'); require_once(_XE_PATH_.'classes/db/queryparts/Subquery.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php'); require_once(_XE_PATH_.'classes/xml/xmlquery/tags/table/TableTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/tags/condition/ConditionTag.class.php');
require_once(_XE_PATH_.'classes/xml/xmlquery/queryargument/QueryArgument.class.php');
?> ?>