Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active March 22, 2017 22:14
Show Gist options
  • Save tps2015gh/ed39a11bcec92e22f4ce0181b1354da5 to your computer and use it in GitHub Desktop.
Save tps2015gh/ed39a11bcec92e22f4ce0181b1354da5 to your computer and use it in GitHub Desktop.
<?php
/* Author: Thitipong Samranvanich
Require : PHP Codeigniter Framework
Since : 2017-03-23
For use in UnitTest Only
Call Function by
1. add "unittest1" in config/autoload.php
2. in controller
use tu\cihelper\utest;
and then
3. in function in controller
$is_exist = tu\cihelper\utest\field_exist("column_1","table1");
echo "<br>is column exist in database table = [$is_exist]<br>";
*/
namespace tu\cihelper\utest;
/* both paramter is string */
function field_exist($field,$table){
$ci = &\get_instance();
assert(is_string($field));
assert(is_string($table));
$require1 = $field;
$uf= $ci->db->list_fields($table);
//$msg = "table admin must have field $require1 " ;
if( in_array($require1, $uf)){
return true ;
}else{
return false;
}
}
function _failon3($msg, $require1 = "", $found =""){
if($require1){
return "<li><font color=red><b> Fail on $msg </b> </font>, require <b>'$require1'</b> but found '$found' </li>";
}else{
return "<li><font color=red><b> Fail on $msg </b> </font>";
}
}
function _fail1($msg){
return "<li><font color=red><b> Fail on $msg </b> </font>";
}
function _okon($msg){
return "<li><font color=blue><b> OK on $msg</b></font></li>";
}
// all parameter is string
function require_field($field,$table){
$msg = "table $table must have field '$field' " ;
$is_exist = field_exist($field,$table);
if(!$is_exist){
echo _fail1( $msg );
}else{
echo _okon( $msg );
}
}
function field_must_not_null($field_name,$table_name){
$ci = &\get_instance();
$is_exist = field_exist($field_name,$table_name);
if(!$is_exist){
echo _fail1("No field 'reset_pass' in table user / CAN NOT Test NEXT ");
}else{
$q = $ci->db->query("select count(*) as c1 from $table_name where $field_name is null ");
$r = $q->row();
$c1 = $r->c1;
if($c1 == 0 ){
echo _okon( " count (user.reset_pass where null ) must == 0 " );
}else{
echo _fail3( " count (user.reset_pass where null ) must == 0 " , " 0 ", $c1 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment