PHP Classes
elePHPant
Icontem

File: undoTestCases.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of yo keller  >  metaForm  >  undoTestCases.php  >  Download  
File: undoTestCases.php
Role: Unit test script
Content type: text/plain
Description: PhpUnit test suite for dbUndo class
Class: metaForm
Generate and validate forms from XML definitions
Author: By
Last change:
Date: 10 years ago
Size: 8,287 bytes
 

Contents

Class file image Download
<?php
   
// undoTestCases.php
require_once('class.undo.php');
require_once(
'PEAR/PHPUnit.php');
$undoTestId = -1;
$undoDummyId = -1;

class
undoTestCases extends PHPUnit_TestCase
{

  var
$t_conf = 'jsrs-adr-php/config.inc.php'; // test conf : the db configuration file
 
var $t_undo; // test dbUndo object
 
var $test_id = 0; // a record created by the testcase
 
var $pc_applied = false;
 
// constructor of the test suite
   
function undoTestCases($name, $fconf='config.inc.php') {
      
$this->PHPUnit_TestCase($name);
       if (!empty(
$fconf) and file_exists($fconf))
              
$this->t_conf = $fconf; // a config.inc.php file
       //$this->peri_constructor();
   
}

    function
peri_constructor() {
        global
$undoTestId;
        if (
$this->pc_applied)
            return;
       
$this->pc_applied = true;
       
$this->t_undo = new dbUndo();
       
$this->t_undo->verb = 1;
       
$this->t_undo->init($this->t_conf);
       
// connect to the database
       
$this->t_undo->dbConnect();
       
// create data ... in the undo_tbl
       
$tbl = 'adresse';
   
$col = 'rue';
   
$data = 'blvd du mail poupoule';
       
$new_dum_id = $this->t_undo->mark_dummy_record($tbl);
       
$this->t_undo->update_col_dummy($tbl,$new_dum_id,$col,$data);
   
$col = 'commune';
   
$data = 'REVE sur Plage';
       
$this->t_undo->update_col_dummy($tbl,$new_dum_id,$col,$data);
       
// apply all these to a new 'real record' in $tbl
    
$this->test_id = $this->t_undo->apply_all_recent_updates($tbl,$new_dum_id);
    
$undoTestId = $this->test_id;
    }
 
// called before the test functions will be executed
    // this function is defined in PHPUnit_TestCase and overwritten
    // here
   
function setUp() {
       
// create a new instance of dbUndo with the
        // conf t_conf
       
       
$this->t_undo = new dbUndo();
       
$this->t_undo->init($this->t_conf);
       
// connect to the database
       
$this->t_undo->dbConnect();
       
$this->t_undo->verb = 1;
       
    }

 
// called after the test functions are executed
    // this function is defined in PHPUnit_TestCase and overwritten
    // here
   
function tearDown() {
       
// delete the test instance
       
unset($this->t_undo);
    }
 
// test suite
    
function testDbConnect()
     {
        
// assume that config.inc.php or the provided fconf is well-defined
        
$this->assertTrue($this->t_undo->dbConnect(),'invalid db connect info in ... '.$this->t_conf);
     }
     function
testSetup(){
        
$this->assertTrue($this->t_undo->setup(),'invalid db setup info ');
     }
     function
test_peri_constructor(){
         global
$undoTestId;
        
$this->peri_constructor(); // check $this->test_id
        
$this->assertTrue($undoTestId != 0 ,"test_peri_constructor could not create a real record ");
     }
     function
test_check_record(){
         global
$undoTestId;
        
//-1 adresse
        
$tbl = 'adresse';
        
$id = $undoTestId;
        
$check = $this->t_undo->check_record($tbl,$id, $tbl_keyname='id');
        
$this->assertTrue($check ,"check_record detected invalid record table ".$tbl." id: ".$id." ");
        
//-2 makes
        
        
$tbl = 'makes';
        
$id = $undoTestId;
        
$check = $this->t_undo->check_record($tbl,$id, $tbl_keyname='id');
        
$this->assertFalse($check ,"check_record detected invalid record table ".$tbl." id: ".$id." ");
        
     }
     function
test_mark_record(){
         global
$undoTestId;
        
$tbl = 'adresse';
        
$id = $undoTestId;
        
$check = $this->t_undo->mark_record($tbl,$id, $tbl_keyname='id');
        
$this->assertTrue($check ,"mark_record detected invalid record table ".$tbl." id: ".$id." or could not insert the mark in the undo table ");
     }
     function
test_mark_dummy_record(){
        
$tbl = 'adresse';
        
$check = $this->t_undo->mark_dummy_record($tbl);
        
$this->assertTrue($check ,"mark_dummy_record detected invalid record table ".$tbl." or could not insert the mark in the undo table ");
     }
     function
test_mark_record_upd(){
         global
$undoTestId;
        
$col = 'rue';
        
$this->t_undo->cur_tbl = 'adresse';
        
$this->t_undo->cur_id = $undoTestId;
        
$check = $this->t_undo->mark_record_upd($col);
        
$this->assertTrue($check ,"mark_record_upd detected invalid record column ".$col." or could not insert the mark in the undo table ");
     }
    
     function
test_update_col_0(){
         global
$undoTestId;
        
// test no change case: feed again the old data
        
$col = 'rue';
        
$this->t_undo->cur_tbl = 'adresse';
        
$this->t_undo->cur_id = $undoTestId;
        
$old_data = $this->t_undo->mark_record_upd($col);
        
$data = $this->t_undo->update_col($col,$old_data);
        
$this->assertTrue($data == $old_data ,"update_col(0) detected a change in record column ".$col." or could not insert the mark in the undo table ");
     }

     function
test_update_col_1(){
         global
$undoTestId;
        
// test no change case: feed again the old data
        
$col = 'rue';
        
$this->t_undo->cur_tbl = 'adresse';
        
$this->t_undo->cur_id = $undoTestId;
        
$old_data = $this->t_undo->mark_record_upd($col);
        
$new_data = str_shuffle($old_data);
        
$data = $this->t_undo->update_col($col,$new_data);
        
$this->assertTrue($data != $new_data && $old_data == $data,"update_col(1) detected no change in record column ".$col." or could not insert the mark in the undo table ");
     }
     function
test_undo_all_recent_updates(){
         global
$undoTestId;
        
$tbl = 'adresse';
        
$id = $undoTestId;
       
$check = $this->t_undo->undo_all_recent_updates($tbl,$id); // $check should be the nbr of rows involved in these recent updates
        
$this->assertTrue($check ,"undo_all_recent_updates detected no change in record column ".$col." or could not insert the mark in the undo table ");
    }
    function
test_update_col_dummy(){
        global
$undoDummyId;
       
$tbl = 'adresse';
       
$undoDummyId = $this->t_undo->mark_dummy_record($tbl); // create a dummy undo-record
       
echo "mark_dummy_record ".$undoDummyId." created!<br />\n";
       
$col = 'rue';
       
$data = 'blvd du cours pilpoule';
       
$check = $this->t_undo->update_col_dummy($tbl,$undoDummyId,$col,$data);
        
$this->assertTrue($check==$data ,"update_col_dummy detected a change in record column ".$col." or could not insert the update in the undo table ");
    }
     function
test_undo_all_recent_col_updates(){
         global
$undoTestId;
        
$tbl = 'adresse';
        
$id = $undoTestId;
       
$col = 'rue';
        
$check = $this->t_undo->undo_all_recent_col_updates($tbl,$id,$col);
        
$this->assertTrue($check>0 ,"undo_all_recent_col_updates did not or could not undo changes in record ".$id." (column ".$col.") or could not insert the update in the undo table ");
     }
     function
test_apply_updates(){
         global
$undoTestId;
        
$field_values = array( 'rue' => 'blvd du mail poupoule',
            
'commune' => 'REVE de cinglés'
        
);
        
$tbl = 'adresse';
        
$id = $undoTestId;
        
$check = $this->t_undo->apply_updates($tbl,$id, $field_values);
        
$this->assertTrue($check ,"apply_updates did not or could not apply changes to the table: ".$tbl." record: ".$id);
     }
     function
test_apply_all_recent_updates(){
         global
$undoDummyId;
        
$tbl = 'adresse';
        
$id = $undoDummyId; //created by test_update_col_dummy
        
$check = $this->t_undo->apply_all_recent_updates($tbl,$id);
        
$this->assertTrue($check ,"apply_all_recent_updates did not or could not find changes to the table: ".$tbl." record: ".$id);
     }
     function
test_delete_recent_undo_data(){
       
// delete all the undo data introduced by this test suite
       
$n_test = 4; // tests generate inserts, 17 generate tags '--#tag#--'
        
for($i=0;$i<$n_test;$i++){
           
$check = $this->t_undo->delete_recent_undo_data();
           
$this->assertTrue($check ,"delete_recent_undo_data: could not find and remove undo records from the undo table ");
         }
     }
    
/*
     function test_re_apply_updates(){
         $field_values = array( 'rue' => 'rue Riboutté',
             'commune' => 'Paris'
         );
         $tbl = 'adresse';
         $id = $this->test_id;
         $check = $this->t_undo->apply_updates($tbl,$id, $field_values);
         $this->assertTrue($check ,"apply_updates did not or could not apply changes to the table: ".$tbl." record: ".$id);
     }
     */
    

}

$suite = new PHPUnit_TestSuite("undoTestCases");
$result = PHPUnit::run($suite);

echo
$result -> toHTML();

?>