cvs: pear /DB_QueryTool/tests Common.php Get.php GetAll.php GetCount.php Limit.php UnitTest.php Where.php index.php sql.php
| From: | Wolfram Kriesing | Date: | Fri, 06 Jun 2003 14:39:44 +0000 |
| Subject: | cvs: pear /DB_QueryTool/tests Common.php Get.php GetAll.php GetCount.php Limit.php UnitTest.php Where.php index.php sql.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-10728@lists.php.net to get a copy of this message | ||
cain Fri Jun 6 10:39:44 2003 EDT
Added files:
/pear/DB_QueryTool/tests Common.php Get.php GetAll.php GetCount.php
Limit.php UnitTest.php Where.php index.php
sql.php
Log:
- moved it here from the docs dir and use the SetupDecorator properly
Index: pear/DB_QueryTool/tests/Common.php +++ pear/DB_QueryTool/tests/Common.php <?php // // $Id: Common.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // require_once 'DB/QueryTool.php'; // so we have all errors saved in one place // its just due to the strange error handling i implemented here ... gotta change that some day $_Common_Errors = array(); class tests_Common extends DB_QueryTool { var $tableSpec = array( array('name' => TABLE_QUESTION, 'shortName' => TABLE_QUESTION) ,array('name' => TABLE_ANSWER, 'shortName' => TABLE_ANSWER) ); function tests_Common($table=null) { if ($table != null) { $this->table = $table; } parent::DB_QueryTool( DB_DSN ); $this->setErrorSetCallback( array(&$this,'errorSet') ); $this->setErrorLogCallback( array(&$this,'errorLog') ); } // // just for the error handling // function errorSet($msg) { $GLOBALS['_Common_Errors'][] = array('set',$msg); } function errorLog($msg) { $GLOBALS['_Common_Errors'][] = array('log',$msg); } function getErrors() { return $GLOBALS['_Common_Errors']; } } ?> Index: pear/DB_QueryTool/tests/Get.php +++ pear/DB_QueryTool/tests/Get.php <?php // // $Id: Get.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Get extends tests_UnitTest { function test_AddGet() { $user = new tests_Common(TABLE_USER); $newData = array( 'login' => 'cain', 'password' => '0', 'name' => 'Lutz Testern', 'address_id'=> 0, 'company_id'=> 0 ); $userId = $user->add( $newData ); $newData['id'] = $userId; $this->assertEquals($newData,$user->get($userId)); $newData = array( 'login' => '', 'password' => '', 'name' => '', 'address_id'=> 0, 'company_id'=> 0 ); $userId = $user->add( $newData ); $newData['id'] = $userId; $this->assertEquals($newData,$user->get($userId)); } // test if column==table works, using the table TABLE_QUESTION function test_tableEqualsColumn() { $question = new tests_Common(TABLE_QUESTION); $newData = array(TABLE_QUESTION=>'Why does this not work?'); $id=$question->add($newData); $newData['id']=$id; $this->assertEquals($newData,$question->get($id)); } // test if column==table works, using the table TABLE_QUESTION function test_tableEqualsColumnGetAll() { $question = new tests_Common(TABLE_QUESTION); $newData = array(TABLE_QUESTION=>'Why does this not work?'); $id=$question->add($newData); $newData['id']=$id; $data = $question->getAll(); // assertEquals doesnt sort arrays recursively, so we have to extract the data :-( // we cant do this: $this->assertEquals(array($newData),$question->getAll()); $this->assertEquals($newData,$data[0]); } // test if column==table works, using the table TABLE_QUESTION // this fails in v0.9.3 // a join makes it fail!!!, the tests above are just convinience tests // they are actually meant to work !always! :-) function test_tableEqualsColumnJoinedGetAll() { $theQuestion = 'Why does this not work?'; $theAnswer = 'I dont know!'; $question = new tests_Common(TABLE_QUESTION); $newQuest = array(TABLE_QUESTION=>$theQuestion); $qid=$question->add($newQuest); $answer = new tests_Common(TABLE_ANSWER); $newAnswer = array(TABLE_QUESTION.'_id'=>$qid,TABLE_ANSWER=>$theAnswer); $aid=$answer->add($newAnswer); $question->autoJoin(TABLE_ANSWER); //$newData['id']=$id; $data = $question->getAll(); $expected = array( '_answer_id'=>$aid, '_answer_answer'=>$theAnswer, '_answer_question_id'=>$qid, 'id'=>$qid, 'question'=>$theQuestion); // assertEquals doesnt sort arrays recursively, so we have to extract the data :-( // we cant do this: $this->assertEquals(array($newData),$question->getAll()); $this->assertEquals($expected,$data[0]); } /** * This method actually checks if the functionality that needs to be changed * for the above test to work will still work after the change ... * * check if stuff like MAX(id), LOWER(question), etc. will be converted to * MAX(TABLE_QUESTION.id), LOWER(TABLE_QUESTION.question) * this is done for preventing ambigious column names, that's why it only applies * in joined queries ... */ function test_testSqlFunction() { $theQuestion = 'Why does this not work?'; $theAnswer = 'I dont know!'; $question = new tests_Common(TABLE_QUESTION); $newQuest = array(TABLE_QUESTION=>$theQuestion); $qid=$question->add($newQuest); $answer = new tests_Common(TABLE_ANSWER); $newAnswer = array(TABLE_QUESTION.'_id'=>$qid,TABLE_ANSWER=>$theAnswer); $aid=$answer->add($newAnswer); $question->autoJoin(TABLE_ANSWER); // $question->setSelect('id,'.TABLE_QUESTION.' as question,'.TABLE_ANSWER.' as answer'); $question->setSelect('MAX(id),'.TABLE_ANSWER.'.id'); $this->assertTrue(strpos($question->_buildSelectQuery(),'MAX('.TABLE_QUESTION.'.id)')); // check '(question)' $question->setSelect('LOWER(question),'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),'LOWER('.TABLE_QUESTION.'.question)')); // check 'id,' $question->setSelect('id,'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),TABLE_QUESTION.'.id')); // check 'id as qid' $question->setSelect('id as qid,'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),TABLE_QUESTION.'.id as qid')); // check 'id as qid' $question->setSelect('LOWER( question ),'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),'LOWER( '.TABLE_QUESTION.'.question )')); } } ?> Index: pear/DB_QueryTool/tests/GetAll.php +++ pear/DB_QueryTool/tests/GetAll.php <?php // // $Id: GetAll.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_GetAll extends tests_UnitTest { function _setup() { $this->user =& new tests_Common(TABLE_USER); $this->user->add(array('name'=>'some name')); $this->user->add(array('name'=>'some name1')); $this->user->add(array('name'=>'some name2')); $this->user->add(array('name'=>'some name3')); } function test_getAll() { $this->_setup(); $this->assertEquals(4,sizeof($this->user->getAll())); } function test_getAllWhereSearch() { $this->_setup(); $this->user->addWhereSearch('name','some'); $this->assertEquals(4,sizeof($this->user->getAll())); } function test_getAllWhereSearch1() { $this->_setup(); $this->user->addWhereSearch('name','4'); $this->assertEquals(0,sizeof($this->user->getAll())); } function test_getAllWhereSearch2() { $this->_setup(); $this->user->addWhereSearch('name','some name'); $this->assertEquals(2,sizeof($this->user->getAll(0,2))); } function test_getAllWhereSearch8() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll())); } function test_getAllWhereSearch10() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll(0,10))); } function test_getAllWhereSearch0() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll(0,0))); } } ?> Index: pear/DB_QueryTool/tests/GetCount.php +++ pear/DB_QueryTool/tests/GetCount.php <?php // // $Id: GetCount.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_GetCount extends tests_UnitTest { function _setup($mode) { $this->user = new tests_Common(TABLE_USER); switch ($mode) { case '6': $this->user->add(array('name'=>'x')); $this->user->add(array('name'=>'y')); $this->user->add(array('name'=>'z')); case '3': $this->user->add(array('name'=>'x')); $this->user->add(array('name'=>'y')); $this->user->add(array('name'=>'z')); break; } } function test_getCount3() { $this->_setup(3); $this->assertEquals(3,$this->user->getCount(),'Wrong count after inserting 3 rows'); } function test_getCount6() { $this->_setup(6); $this->assertEquals(6,$this->user->getCount(),'Wrong count after inserting 6 rows'); } function test_getCountGrouped3() { $this->_setup(6); $this->user->setGroup('name'); $this->assertEquals(3,$this->user->getCount(),'Wrong count after 6 inserted and grouping them by name'); } function test_getCountGrouped2() { $this->_setup(6); $this->user->setWhere('name="z"'); $this->assertEquals(2,$this->user->getCount(),'setWhere and setGroup should have resulted in two'); } function test_getCountGrouped1() { $this->_setup(6); $this->user->setGroup('name'); $this->user->setWhere('name="z"'); $this->assertEquals(1,$this->user->getCount(),'setWhere and setGroup should have resulted in one'); } function test_getCountGrouped0() { $this->_setup(6); $this->user->setGroup('name'); $this->user->setWhere('name="xxx"'); $this->assertEquals(0,$this->user->getCount(),'setWhere and setGroup should have resulted in one'); } } ?> Index: pear/DB_QueryTool/tests/Limit.php +++ pear/DB_QueryTool/tests/Limit.php <?php // // $Id: Limit.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Limit extends tests_UnitTest { // test if setLimit works function test_setLimit() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $this->assertEquals(array(0,10),$user->getLimit()); } // test if setLimit works function test_setLimit1() { $user = new tests_Common(TABLE_USER); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $user->setLimit(0,2); $this->assertEquals(2,sizeof($user->getAll())); $user->setLimit(0,3); $this->assertEquals(3,sizeof($user->getAll())); } // test if getAll works // setLimit should have no effect when parameters are given to getAll() function test_getAll() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $this->assertEquals(1,sizeof($user->getAll(0,1))); $user->setLimit(0,3); $this->assertEquals(2,sizeof($user->getAll(0,2))); $this->assertEquals(3,sizeof($user->getAll())); } // test if getAll works // setLimit should have no effect when parameters are given to getAll() function test_getCol() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $this->assertEquals(1,sizeof($user->getCol('id',0,1))); $user->setLimit(0,3); $this->assertEquals(2,sizeof($user->getCol('id',0,2))); $this->assertEquals(3,sizeof($user->getCol('id'))); } } ?> Index: pear/DB_QueryTool/tests/UnitTest.php +++ pear/DB_QueryTool/tests/UnitTest.php <?php // // $Id: UnitTest.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_UnitTest extends PhpUnit_TestCase { function setUp() { foreach ($GLOBALS['allTables'] as $aTable) { $tableObj = new tests_Common($aTable); $tableObj->removeAll(); } $this->setLooselyTyped(true); } function tearDown() { /* global $dbStructure; $querytool = new Common(); foreach ($dbStructure[$querytool->db->phptype]['tearDown'] as $aQuery) { //print "$aQuery<br><br>"; if (DB::isError($ret=$querytool->db->query($aQuery))) { die($ret->getUserInfo()); } } */ } } ?> Index: pear/DB_QueryTool/tests/Where.php +++ pear/DB_QueryTool/tests/Where.php <?php // // $Id: Where.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Where extends tests_UnitTest { function test_setWhere() { $user = new tests_Common(TABLE_USER); $whereClause = 'name='.$user->db->quote('Wolfram'); $user->setWhere($whereClause); $this->assertEquals($whereClause,$user->getWhere()); $whereClause = 'name='.$user->db->quote('"test"oli'); $user->setWhere($whereClause); $this->assertEquals($whereClause,$user->getWhere()); $user = new tests_Common(TABLE_USER); $whereClause = 'name='.$user->db->quote('Wolfram'); $user->setWhere($whereClause); $whereClause1 = 'name='.$user->db->quote('Kriesing'); $user->addWhere($whereClause1); $this->assertEquals("$whereClause AND $whereClause1",$user->getWhere()); $whereClause = 'name='.$user->db->quote('"test"oli'); $user->setWhere($whereClause); $whereClause1 = 'name='.$user->db->quote('"testirt"oli'); $user->addWhere($whereClause1,'OR'); $this->assertEquals("$whereClause OR $whereClause1",$user->getWhere()); } function test_addWhereSearch() { $user = new tests_Common(TABLE_USER); $user->removeAll(); $user->add(array('name'=>'Wolfram Kriesing')); $user->add(array('name'=>'WOLFRAM Daniel KrIESIng')); $user->add(array('name'=>' kriesing wolfram ')); $user->setWhere(); $user->addWhereSearch('name','Wolfram Kriesing'); $this->assertEquals(2,$user->getCount(),'getCount(): Did not find the inserted number of user names.'); $user->add(array('name'=>'Wolfram and here goes some string Kriesing but it should be found')); $user->add(array('name'=>'%Wolfram man in the middle :-) Kriesing and smthg behind%')); $this->assertEquals(4,$user->getCount(),'getCount(): Did not find the inserted number of user names.'); $user->removeAll(); } } ?> Index: pear/DB_QueryTool/tests/index.php +++ pear/DB_QueryTool/tests/index.php <?php // // $Id: index.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // ini_set('include_path',realpath(dirname(__FILE__).'/../../../').':'.realpath(dirname(__FILE__).'/../../../../includes').':'.ini_get('include_path')); require_once 'DB/QueryTool.php'; require_once 'PHPUnit.php'; require_once 'PHPUnit/GUI/HTML.php'; define(DB_DSN,'mysql://root@localhost/test'); define(TABLE_USER, 'QueryTool_user'); define(TABLE_ADDRESS, 'QueryTool_address'); define(TABLE_QUESTION, 'question'); define(TABLE_ANSWER, 'answer'); $allTables = array(TABLE_USER,TABLE_ADDRESS,TABLE_QUESTION,TABLE_ANSWER); require_once 'sql.php'; require_once 'UnitTest.php'; require_once 'Common.php'; // // common setup (this actually also does the tearDown, since we have the DROP TABLE queries in the setup too // $querytool = new tests_Common(); foreach ($dbStructure[$querytool->db->phptype]['setup'] as $aQuery) { if (DB::isError($ret=$querytool->db->query($aQuery))) { die($ret->getUserInfo()); } } // // run the test suite // require_once 'PHPUnit/GUI/SetupDecorator.php'; $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_HTML()); $gui->getSuitesFromDir(dirname(__FILE__),'.*\.php',array('UnitTest.php','Common.php','sql.php','index.php')); $gui->show(); //print_r($errors); ?> Index: pear/DB_QueryTool/tests/sql.php +++ pear/DB_QueryTool/tests/sql.php <?php // $Id: sql.php,v 1.1 2003/06/06 14:39:44 cain Exp $ $dbStructure = array( 'mysql' => array( 'setup' => array( "DROP TABLE IF EXISTS ".TABLE_USER.";" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS.";" ,"DROP TABLE IF EXISTS ".TABLE_USER."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION.";" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER.";" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER."_seq;" ,"CREATE TABLE ".TABLE_ADDRESS." ( id int(11) NOT NULL default '0', city varchar(100) NOT NULL default '', zip varchar(5) NOT NULL default '', street varchar(100) NOT NULL default '', phone varchar(100) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_USER." ( id int(11) NOT NULL default '0', login varchar(255) NOT NULL default '', password varchar(255) NOT NULL default '', name varchar(255) NOT NULL default '', address_id int(11) NOT NULL default '0', company_id int(11) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_QUESTION." ( id int(11) NOT NULL default '0', ".TABLE_QUESTION." varchar(255) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_ANSWER." ( id int(11) NOT NULL default '0', ".TABLE_ANSWER." varchar(255) NOT NULL default '', ".TABLE_QUESTION."_id int(11) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;" ), 'tearDown' => array( "DROP TABLE IF EXISTS ".TABLE_USER.";" ,"DROP TABLE IF EXISTS ".TABLE_USER."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS.";" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION.";" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER.";" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER."_seq;" ) ), 'pgsql' => array( 'setup' => array(), 'tearDown'=>array() ) ); ?>
Index: pear/DB_QueryTool/tests/Common.php +++ pear/DB_QueryTool/tests/Common.php <?php // // $Id: Common.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // require_once 'DB/QueryTool.php'; // so we have all errors saved in one place // its just due to the strange error handling i implemented here ... gotta change that some day $_Common_Errors = array(); class tests_Common extends DB_QueryTool { var $tableSpec = array( array('name' => TABLE_QUESTION, 'shortName' => TABLE_QUESTION) ,array('name' => TABLE_ANSWER, 'shortName' => TABLE_ANSWER) ); function tests_Common($table=null) { if ($table != null) { $this->table = $table; } parent::DB_QueryTool( DB_DSN ); $this->setErrorSetCallback( array(&$this,'errorSet') ); $this->setErrorLogCallback( array(&$this,'errorLog') ); } // // just for the error handling // function errorSet($msg) { $GLOBALS['_Common_Errors'][] = array('set',$msg); } function errorLog($msg) { $GLOBALS['_Common_Errors'][] = array('log',$msg); } function getErrors() { return $GLOBALS['_Common_Errors']; } } ?> Index: pear/DB_QueryTool/tests/Get.php +++ pear/DB_QueryTool/tests/Get.php <?php // // $Id: Get.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Get extends tests_UnitTest { function test_AddGet() { $user = new tests_Common(TABLE_USER); $newData = array( 'login' => 'cain', 'password' => '0', 'name' => 'Lutz Testern', 'address_id'=> 0, 'company_id'=> 0 ); $userId = $user->add( $newData ); $newData['id'] = $userId; $this->assertEquals($newData,$user->get($userId)); $newData = array( 'login' => '', 'password' => '', 'name' => '', 'address_id'=> 0, 'company_id'=> 0 ); $userId = $user->add( $newData ); $newData['id'] = $userId; $this->assertEquals($newData,$user->get($userId)); } // test if column==table works, using the table TABLE_QUESTION function test_tableEqualsColumn() { $question = new tests_Common(TABLE_QUESTION); $newData = array(TABLE_QUESTION=>'Why does this not work?'); $id=$question->add($newData); $newData['id']=$id; $this->assertEquals($newData,$question->get($id)); } // test if column==table works, using the table TABLE_QUESTION function test_tableEqualsColumnGetAll() { $question = new tests_Common(TABLE_QUESTION); $newData = array(TABLE_QUESTION=>'Why does this not work?'); $id=$question->add($newData); $newData['id']=$id; $data = $question->getAll(); // assertEquals doesnt sort arrays recursively, so we have to extract the data :-( // we cant do this: $this->assertEquals(array($newData),$question->getAll()); $this->assertEquals($newData,$data[0]); } // test if column==table works, using the table TABLE_QUESTION // this fails in v0.9.3 // a join makes it fail!!!, the tests above are just convinience tests // they are actually meant to work !always! :-) function test_tableEqualsColumnJoinedGetAll() { $theQuestion = 'Why does this not work?'; $theAnswer = 'I dont know!'; $question = new tests_Common(TABLE_QUESTION); $newQuest = array(TABLE_QUESTION=>$theQuestion); $qid=$question->add($newQuest); $answer = new tests_Common(TABLE_ANSWER); $newAnswer = array(TABLE_QUESTION.'_id'=>$qid,TABLE_ANSWER=>$theAnswer); $aid=$answer->add($newAnswer); $question->autoJoin(TABLE_ANSWER); //$newData['id']=$id; $data = $question->getAll(); $expected = array( '_answer_id'=>$aid, '_answer_answer'=>$theAnswer, '_answer_question_id'=>$qid, 'id'=>$qid, 'question'=>$theQuestion); // assertEquals doesnt sort arrays recursively, so we have to extract the data :-( // we cant do this: $this->assertEquals(array($newData),$question->getAll()); $this->assertEquals($expected,$data[0]); } /** * This method actually checks if the functionality that needs to be changed * for the above test to work will still work after the change ... * * check if stuff like MAX(id), LOWER(question), etc. will be converted to * MAX(TABLE_QUESTION.id), LOWER(TABLE_QUESTION.question) * this is done for preventing ambigious column names, that's why it only applies * in joined queries ... */ function test_testSqlFunction() { $theQuestion = 'Why does this not work?'; $theAnswer = 'I dont know!'; $question = new tests_Common(TABLE_QUESTION); $newQuest = array(TABLE_QUESTION=>$theQuestion); $qid=$question->add($newQuest); $answer = new tests_Common(TABLE_ANSWER); $newAnswer = array(TABLE_QUESTION.'_id'=>$qid,TABLE_ANSWER=>$theAnswer); $aid=$answer->add($newAnswer); $question->autoJoin(TABLE_ANSWER); // $question->setSelect('id,'.TABLE_QUESTION.' as question,'.TABLE_ANSWER.' as answer'); $question->setSelect('MAX(id),'.TABLE_ANSWER.'.id'); $this->assertTrue(strpos($question->_buildSelectQuery(),'MAX('.TABLE_QUESTION.'.id)')); // check '(question)' $question->setSelect('LOWER(question),'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),'LOWER('.TABLE_QUESTION.'.question)')); // check 'id,' $question->setSelect('id,'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),TABLE_QUESTION.'.id')); // check 'id as qid' $question->setSelect('id as qid,'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),TABLE_QUESTION.'.id as qid')); // check 'id as qid' $question->setSelect('LOWER( question ),'.TABLE_ANSWER.'.*'); $this->assertTrue(strpos($question->_buildSelectQuery(),'LOWER( '.TABLE_QUESTION.'.question )')); } } ?> Index: pear/DB_QueryTool/tests/GetAll.php +++ pear/DB_QueryTool/tests/GetAll.php <?php // // $Id: GetAll.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_GetAll extends tests_UnitTest { function _setup() { $this->user =& new tests_Common(TABLE_USER); $this->user->add(array('name'=>'some name')); $this->user->add(array('name'=>'some name1')); $this->user->add(array('name'=>'some name2')); $this->user->add(array('name'=>'some name3')); } function test_getAll() { $this->_setup(); $this->assertEquals(4,sizeof($this->user->getAll())); } function test_getAllWhereSearch() { $this->_setup(); $this->user->addWhereSearch('name','some'); $this->assertEquals(4,sizeof($this->user->getAll())); } function test_getAllWhereSearch1() { $this->_setup(); $this->user->addWhereSearch('name','4'); $this->assertEquals(0,sizeof($this->user->getAll())); } function test_getAllWhereSearch2() { $this->_setup(); $this->user->addWhereSearch('name','some name'); $this->assertEquals(2,sizeof($this->user->getAll(0,2))); } function test_getAllWhereSearch8() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll())); } function test_getAllWhereSearch10() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll(0,10))); } function test_getAllWhereSearch0() { $this->_setup(); $this->_setup(); $this->assertEquals(8,sizeof($this->user->getAll(0,0))); } } ?> Index: pear/DB_QueryTool/tests/GetCount.php +++ pear/DB_QueryTool/tests/GetCount.php <?php // // $Id: GetCount.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_GetCount extends tests_UnitTest { function _setup($mode) { $this->user = new tests_Common(TABLE_USER); switch ($mode) { case '6': $this->user->add(array('name'=>'x')); $this->user->add(array('name'=>'y')); $this->user->add(array('name'=>'z')); case '3': $this->user->add(array('name'=>'x')); $this->user->add(array('name'=>'y')); $this->user->add(array('name'=>'z')); break; } } function test_getCount3() { $this->_setup(3); $this->assertEquals(3,$this->user->getCount(),'Wrong count after inserting 3 rows'); } function test_getCount6() { $this->_setup(6); $this->assertEquals(6,$this->user->getCount(),'Wrong count after inserting 6 rows'); } function test_getCountGrouped3() { $this->_setup(6); $this->user->setGroup('name'); $this->assertEquals(3,$this->user->getCount(),'Wrong count after 6 inserted and grouping them by name'); } function test_getCountGrouped2() { $this->_setup(6); $this->user->setWhere('name="z"'); $this->assertEquals(2,$this->user->getCount(),'setWhere and setGroup should have resulted in two'); } function test_getCountGrouped1() { $this->_setup(6); $this->user->setGroup('name'); $this->user->setWhere('name="z"'); $this->assertEquals(1,$this->user->getCount(),'setWhere and setGroup should have resulted in one'); } function test_getCountGrouped0() { $this->_setup(6); $this->user->setGroup('name'); $this->user->setWhere('name="xxx"'); $this->assertEquals(0,$this->user->getCount(),'setWhere and setGroup should have resulted in one'); } } ?> Index: pear/DB_QueryTool/tests/Limit.php +++ pear/DB_QueryTool/tests/Limit.php <?php // // $Id: Limit.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Limit extends tests_UnitTest { // test if setLimit works function test_setLimit() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $this->assertEquals(array(0,10),$user->getLimit()); } // test if setLimit works function test_setLimit1() { $user = new tests_Common(TABLE_USER); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $user->setLimit(0,2); $this->assertEquals(2,sizeof($user->getAll())); $user->setLimit(0,3); $this->assertEquals(3,sizeof($user->getAll())); } // test if getAll works // setLimit should have no effect when parameters are given to getAll() function test_getAll() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $this->assertEquals(1,sizeof($user->getAll(0,1))); $user->setLimit(0,3); $this->assertEquals(2,sizeof($user->getAll(0,2))); $this->assertEquals(3,sizeof($user->getAll())); } // test if getAll works // setLimit should have no effect when parameters are given to getAll() function test_getCol() { $user = new tests_Common(TABLE_USER); $user->setLimit(0,10); $user->add(array('login'=>1)); $user->add(array('login'=>2)); $user->add(array('login'=>3)); $user->add(array('login'=>4)); $this->assertEquals(1,sizeof($user->getCol('id',0,1))); $user->setLimit(0,3); $this->assertEquals(2,sizeof($user->getCol('id',0,2))); $this->assertEquals(3,sizeof($user->getCol('id'))); } } ?> Index: pear/DB_QueryTool/tests/UnitTest.php +++ pear/DB_QueryTool/tests/UnitTest.php <?php // // $Id: UnitTest.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_UnitTest extends PhpUnit_TestCase { function setUp() { foreach ($GLOBALS['allTables'] as $aTable) { $tableObj = new tests_Common($aTable); $tableObj->removeAll(); } $this->setLooselyTyped(true); } function tearDown() { /* global $dbStructure; $querytool = new Common(); foreach ($dbStructure[$querytool->db->phptype]['tearDown'] as $aQuery) { //print "$aQuery<br><br>"; if (DB::isError($ret=$querytool->db->query($aQuery))) { die($ret->getUserInfo()); } } */ } } ?> Index: pear/DB_QueryTool/tests/Where.php +++ pear/DB_QueryTool/tests/Where.php <?php // // $Id: Where.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // class tests_Where extends tests_UnitTest { function test_setWhere() { $user = new tests_Common(TABLE_USER); $whereClause = 'name='.$user->db->quote('Wolfram'); $user->setWhere($whereClause); $this->assertEquals($whereClause,$user->getWhere()); $whereClause = 'name='.$user->db->quote('"test"oli'); $user->setWhere($whereClause); $this->assertEquals($whereClause,$user->getWhere()); $user = new tests_Common(TABLE_USER); $whereClause = 'name='.$user->db->quote('Wolfram'); $user->setWhere($whereClause); $whereClause1 = 'name='.$user->db->quote('Kriesing'); $user->addWhere($whereClause1); $this->assertEquals("$whereClause AND $whereClause1",$user->getWhere()); $whereClause = 'name='.$user->db->quote('"test"oli'); $user->setWhere($whereClause); $whereClause1 = 'name='.$user->db->quote('"testirt"oli'); $user->addWhere($whereClause1,'OR'); $this->assertEquals("$whereClause OR $whereClause1",$user->getWhere()); } function test_addWhereSearch() { $user = new tests_Common(TABLE_USER); $user->removeAll(); $user->add(array('name'=>'Wolfram Kriesing')); $user->add(array('name'=>'WOLFRAM Daniel KrIESIng')); $user->add(array('name'=>' kriesing wolfram ')); $user->setWhere(); $user->addWhereSearch('name','Wolfram Kriesing'); $this->assertEquals(2,$user->getCount(),'getCount(): Did not find the inserted number of user names.'); $user->add(array('name'=>'Wolfram and here goes some string Kriesing but it should be found')); $user->add(array('name'=>'%Wolfram man in the middle :-) Kriesing and smthg behind%')); $this->assertEquals(4,$user->getCount(),'getCount(): Did not find the inserted number of user names.'); $user->removeAll(); } } ?> Index: pear/DB_QueryTool/tests/index.php +++ pear/DB_QueryTool/tests/index.php <?php // // $Id: index.php,v 1.1 2003/06/06 14:39:44 cain Exp $ // ini_set('include_path',realpath(dirname(__FILE__).'/../../../').':'.realpath(dirname(__FILE__).'/../../../../includes').':'.ini_get('include_path')); require_once 'DB/QueryTool.php'; require_once 'PHPUnit.php'; require_once 'PHPUnit/GUI/HTML.php'; define(DB_DSN,'mysql://root@localhost/test'); define(TABLE_USER, 'QueryTool_user'); define(TABLE_ADDRESS, 'QueryTool_address'); define(TABLE_QUESTION, 'question'); define(TABLE_ANSWER, 'answer'); $allTables = array(TABLE_USER,TABLE_ADDRESS,TABLE_QUESTION,TABLE_ANSWER); require_once 'sql.php'; require_once 'UnitTest.php'; require_once 'Common.php'; // // common setup (this actually also does the tearDown, since we have the DROP TABLE queries in the setup too // $querytool = new tests_Common(); foreach ($dbStructure[$querytool->db->phptype]['setup'] as $aQuery) { if (DB::isError($ret=$querytool->db->query($aQuery))) { die($ret->getUserInfo()); } } // // run the test suite // require_once 'PHPUnit/GUI/SetupDecorator.php'; $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_HTML()); $gui->getSuitesFromDir(dirname(__FILE__),'.*\.php',array('UnitTest.php','Common.php','sql.php','index.php')); $gui->show(); //print_r($errors); ?> Index: pear/DB_QueryTool/tests/sql.php +++ pear/DB_QueryTool/tests/sql.php <?php // $Id: sql.php,v 1.1 2003/06/06 14:39:44 cain Exp $ $dbStructure = array( 'mysql' => array( 'setup' => array( "DROP TABLE IF EXISTS ".TABLE_USER.";" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS.";" ,"DROP TABLE IF EXISTS ".TABLE_USER."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION.";" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER.";" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER."_seq;" ,"CREATE TABLE ".TABLE_ADDRESS." ( id int(11) NOT NULL default '0', city varchar(100) NOT NULL default '', zip varchar(5) NOT NULL default '', street varchar(100) NOT NULL default '', phone varchar(100) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_USER." ( id int(11) NOT NULL default '0', login varchar(255) NOT NULL default '', password varchar(255) NOT NULL default '', name varchar(255) NOT NULL default '', address_id int(11) NOT NULL default '0', company_id int(11) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_QUESTION." ( id int(11) NOT NULL default '0', ".TABLE_QUESTION." varchar(255) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;" ,"CREATE TABLE ".TABLE_ANSWER." ( id int(11) NOT NULL default '0', ".TABLE_ANSWER." varchar(255) NOT NULL default '', ".TABLE_QUESTION."_id int(11) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;" ), 'tearDown' => array( "DROP TABLE IF EXISTS ".TABLE_USER.";" ,"DROP TABLE IF EXISTS ".TABLE_USER."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS.";" ,"DROP TABLE IF EXISTS ".TABLE_ADDRESS."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION.";" ,"DROP TABLE IF EXISTS ".TABLE_QUESTION."_seq;" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER.";" ,"DROP TABLE IF EXISTS ".TABLE_ANSWER."_seq;" ) ), 'pgsql' => array( 'setup' => array(), 'tearDown'=>array() ) ); ?>