More SQL command (drop table, drop database)
delete, select & insert upgraded More utility function (node to array, arrayToNode) XMLDB special move command PHP Unit Test
This commit is contained in:
@@ -1,104 +1,60 @@
|
||||
<?
|
||||
require_once('simpletest/autorun.php');
|
||||
require_once('log.php');
|
||||
require_once 'XMLDB.php';
|
||||
echo 'Creating new DB (should be "ok" creating a new file "test")<br/>';
|
||||
if($xmldbtest1 = new XMLDB('test.xml', 'id', true))
|
||||
echo "ok";
|
||||
else
|
||||
echo "ko";
|
||||
echo '<br/>Creating new table (should be "ok" adding table1 in test)<br/>';
|
||||
if($xmldbtest1->createTable('table1'))
|
||||
echo "ok";
|
||||
else
|
||||
echo "ko";
|
||||
|
||||
echo '<br/>loading an inexistant DB (error)<br/>';
|
||||
$xmldbtest = new XMLDB('text.xml');
|
||||
if(!$xmldbtest->isLoaded()){
|
||||
echo '<br/><br/>can\'t load test.xml<br/>';
|
||||
$xmldbtest = new XMLDB('database.xml');
|
||||
if($xmldbtest->isLoaded()){
|
||||
// Testing select($from, $attributeName = null, $attributeValue = null, $childName = null, $childValue = null)
|
||||
echo 'Testing empty select (exception)<br/>';
|
||||
try {
|
||||
$xmldbtest->select();
|
||||
}
|
||||
catch(Exception $e){
|
||||
echo $e->getMessage().'<br/><br/>';
|
||||
}
|
||||
class TestOfLogging extends UnitTestCase {
|
||||
function test(){
|
||||
//$this->expectException(new Exception('uhoh, no table selected'));
|
||||
@unlink('test.log');
|
||||
$log = new Log('test.log');
|
||||
$log->message('creating "test.xml" with a table');
|
||||
$this->assertTrue($xmldbtest1 = new XMLDB('test.xml', 'id', true));
|
||||
$this->assertTrue($xmldbtest1->createTable('table1'));
|
||||
//$xmldbtest = new XMLDB('text.xml');
|
||||
//$log->message('Trying to load an inexistant DB');
|
||||
//$this->assertFalse($xmldbtest->isLoaded());
|
||||
$log->message('Trying to load "database.xml"');
|
||||
$xmldbtest = new XMLDB('database.xml');
|
||||
$this->assertTrue($xmldbtest->isLoaded());
|
||||
|
||||
echo '<br/>Creating new table name "*" (should be "ko" table already exist)<br/>';
|
||||
if($xmldbtest->createTable('*'))
|
||||
echo "ok";
|
||||
else
|
||||
echo "ko";
|
||||
|
||||
echo '<br/>Creating new table (should be "ko" table already exist)<br/>';
|
||||
if($xmldbtest->createTable('table1'))
|
||||
echo "ok";
|
||||
else
|
||||
echo "ko";
|
||||
echo '<br/>Testing insertNode at XML root (should insert an item named "test" into table1)<br/>';
|
||||
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
$log->message('Trying to create 2 wrong tables');
|
||||
$this->assertFalse($xmldbtest->createTable('*'));
|
||||
$this->assertFalse($xmldbtest->createTable('table1'));
|
||||
$log->message('Trying to insert item "test" into "table1"');
|
||||
$this->assertTrue($xmldbtest->insertItem('test', array('pwet'=>'cacahuete'), array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||
$log->message('Trying to do several select');
|
||||
$this->assertEqual($xmldbtest->selectTable('table1', 'count'), 8);
|
||||
$this->assertEqual($xmldbtest->selectFromPK('table1', 'weather', 'count'), 1);
|
||||
$this->assertEqual($xmldbtest->selectFromAttribute('table1', array('id'=>'weather'), 'count'),1);
|
||||
$this->assertEqual($xmldbtest->selectFromChildren('table1',array('visibility'=>'true'),'count'), 6);
|
||||
$this->assertEqual($xmldbtest->selectFromChildren('*',array('visibility'=>'true'),'count'), 8);
|
||||
$this->assertEqual($xmldbtest->selectFromChildren('table1',array('visibility'=>'true', 'x'=>'32'),'count'), 1);
|
||||
$log->message('Trying to do several inserts');
|
||||
$this->assertFalse($xmldbtest->insertItem('test', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||
$this->assertTrue($xmldbtest->insertItem('test2', null, array('visibility'=>'true', 'x'=>'33'), 'table1'));
|
||||
$log->message('Trying to do several updates');
|
||||
$this->assertTrue($xmldbtest->updateNodeAttribute('table1', array('id', 'links'), array('id', 'zelda')));
|
||||
$this->assertTrue($xmldbtest->updateNodeValue('table1', array('id', 'notes'), null, 'booga!'));
|
||||
$log->message('Trying to delete the item "clock" from "table1"');
|
||||
//var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true);
|
||||
//$this->assertTrue($xmldbtest->deleteNode($xmldbtest->selectFromPK('table1', 'clock', 'node')));
|
||||
$this->assertTrue($xmldbtest->deleteItem('table1','clock'));
|
||||
//var_dump($xmldbtest->selectFromPK('table1', 'search', 'node')->item(0));
|
||||
$log->message('Trying to move an element to another table');
|
||||
$this->assertTrue($xmldbtest->move( $xmldbtest->selectFromPK('table1', 'search', 'node'), 'table2'));
|
||||
|
||||
echo 'Testing select (should give 8 results - 7 from the file + 1 we created earlier)<br/>';
|
||||
$result = $xmldbtest->select('table1');
|
||||
echo count($result).' results <br/><br/>';
|
||||
$log->message('Trying to erase a DB');
|
||||
$xmldbtest1->dropDatabase(true);
|
||||
|
||||
echo 'Testing select id=weather with pk (should find 1 result)<br/>';
|
||||
$result = $xmldbtest->selectFromPK('table1', 'weather');
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing select id=weather (should find 1 result)<br/>';
|
||||
$result = $xmldbtest->select('table1', null, array('id'=>'weather'));
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing select visibility = true (should throw 6 results - 5 from the base file + 1 we created)<br/>';
|
||||
$result = $xmldbtest->select('table1', null,null,array('visibility'=>'true'));
|
||||
echo count($result).' results<br/><br/>';
|
||||
/*$log->message('Trying to select w/o anything');
|
||||
$xmldbtest->select();*/
|
||||
}
|
||||
|
||||
echo 'Testing select visibility = true on all tables (should find 8 results - 7 from the base file + 1 we created)<br/>';
|
||||
$result = $xmldbtest->select('*', null,null,array('visibility'=>'true'));
|
||||
echo count($result).' results <br/><br/>';
|
||||
|
||||
echo 'Testing select visibility = true and x = 32 (experimental - should find 1)<br/>';
|
||||
$result = $xmldbtest->selectFromChilds('table1',array('visibility'=>'true', 'x'=>'32'));
|
||||
echo count($result).' results<br/><br/>';
|
||||
|
||||
echo '<br/>Testing insertNode that already exist (should do "ko")<br/>';
|
||||
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing insertNode that doesn\'t exist (should do "ok")<br/>';
|
||||
if($xmldbtest->insertNode(array('name'=>'item', 'attributes'=>array('id'=>'test2'), 'childs'=>array('visibility'=>'true', 'x'=>'33')), 'table1'))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing updatingNodeAttribute with no insert (should be ok and change the item "links" into "zelda" haha)<br/>';
|
||||
if($xmldbtest->updateNodeAttribute('table1', array('id', 'links'), array('id', 'zelda')))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing updateNodeValue via attribute (should be ok - inserting "booga!" into the item named "notes")<br/>';
|
||||
if($xmldbtest->updateNodeValue('table1', array('id', 'notes'), null, 'booga!'))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
echo '<br/>Testing deleteNode via pk (should be ok - deleting the item clock into table1)<br/>';
|
||||
if($xmldbtest->deleteNode('table1', 'clock', null))
|
||||
echo "ok<br/><br/>";
|
||||
else
|
||||
echo "ko<br/><br/>";
|
||||
|
||||
}else{
|
||||
exit("can't load config.xml either");
|
||||
}
|
||||
}
|
||||
|
||||
$test = new TestOfLogging;
|
||||
$test->test();
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user