Open Sencillo  2015.107
Long live the simplicity of PHP
 All Data Structures Namespaces Functions Pages
test.tool.framework.php
1 <?php
11 class log
12 {
13  protected $name;
14  protected $file;
15  protected $path;
16  protected $current;
17  protected $store=array();
18  protected $maxLine=0;
19  protected $useFile;
20 
27  final public function __construct($path='',$file='log.txt',$maxLine=100,$useFile=true,$mysql=null)
28  {
29  $this->file = $path.$file;
30  $this->path = $path;
31  $this->name = $file;
32  $this->maxLine = $maxLine;
33  $this->useFile = $useFile;
34  $this->store['mysql'] = $mysql;
35  }
36 
41  final public function saveToLog($data)
42  {
43  if($this->useFile)
44  {
45  $this->getContent();
46  $this->current = fopen($this->file, "w");
47  fprintf($this->current, "%s\t%s\t%s\t%s\t%s\t%s\n",date('Y-m-d'),date('H:i:s'),$data[0],$data[1],$data[2],$data[3]);
48  foreach ($this->store as $val)
49  {
50  fprintf($this->current, "%s\t%s\t%s\t%s\t%s\t%s\n",$val[0],$val[1],$val[2],$val[3],$val[4],$val[5]);
51  }
52 
53  fclose($this->current);
54 
55  if($this->maxLine<sizeof($this->store))
56  {
57  rename($_SERVER['DOCUMENT_ROOT'] . $this->file,$_SERVER['DOCUMENT_ROOT'] . $this->path ."old_" . $this->name);
58  }
59  }
60  else
61  {
62  $name=explode('.',$this->name);
63  $this->store['mysql']->newColumn('date','VARCHAR(10)');
64  $this->store['mysql']->newColumn('time','VARCHAR(8)');
65  $this->store['mysql']->newColumn('data0','TEXT');
66  $this->store['mysql']->newColumn('data1','TEXT');
67  $this->store['mysql']->newColumn('data2','TEXT');
68  $this->store['mysql']->newColumn('data3','TEXT');
69  $this->store['mysql']->createTable($name[0].'_log');
70  $this->store['mysql']->openTable($name[0].'_log');
71  $this->store['mysql']->insert('\''.date('Y-m-d').'\',\''.date('H:i:s').'\',\''.$data[0].'\',\''.$data[1].'\',\''.$data[2].'\',\''.$data[3].'\'');
72  }
73  }
74 
78  private function getContent()
79  {
80  if($this->useFile)
81  {
82  $this->current = fopen($this->file, "r");
83  $i=0;
84  while(($buffer = fgets($this->current, 4096)) !== false)
85  {
86  $this->store[$i++] = sscanf($buffer, "%s\t%s\t%s\t%s\t%s\t%s\n");
87  }
88  fclose($this->current);
89  }
90  else
91  {
92  return $this->output("`id`>0","`id` DESC",$this->maxLine);
93  }
94  }
95 
100  public function getLog()
101  {
102  $this->getContent();
103  return $this->store;
104  }
105 
109  public function vd($var)
110  {
111  ob_start();
112  var_dump($var);
113  $result = ob_get_clean();
114  $result = highlight_string($result,true);
115  $result = str_replace('=&gt;<br />&nbsp;&nbsp;&nbsp;','<b>=&gt;</b>',$result);
116  $result = str_replace(')&nbsp;"',')&nbsp;"<font color="blue">',$result);
117  $result = str_replace('"<br />','</font>"<br />',$result);
118  echo $result;
119  return $result;
120  }
121 }
122 
132 class unitTest extends log
133 {
134  protected $env='public';
135 
143  public function setEnvironment($public=true)
144  {
145  $this->env = ($public===true?'public':'developer');
146  if($public===false)
147  {
148  print "<script type='text/javascript'>console.log('<<< Sencillo Testing Tool >>>');</script>";
149  error_reporting(E_ALL);
150  print "<script type='text/javascript'>console.log('<<< DEVEL MODE ON >>>');</script>";
151  }
152  return $public;
153  }
154 
164  public function print_test($function=null,$validator=null,$die=false)
165  {
166  return $this->print_ut($this->init($function,$validator),$die);
167  }
168 
198  public function init($function=null,$validator=null)
199  {
200  if((is_array($validator))&&($this->env==='developer'))
201  {
202  foreach($validator as $key=>$val)
203  {
204  switch($key)
205  {
206  case '%s':
207  case 'string':
208  if(!(is_string($function)))
209  {
210  return array("status"=>"no$key",
211  "value"=>var_export($function,true)
212  );
213  }
214  break;
215  case '%d':
216  case 'int':
217  case 'integer':
218  if(!(is_integer($function)))
219  {
220  return array("status"=>"no$key",
221  "value"=>var_export($function,true)
222  );
223  }
224  break;
225  case '%f':
226  case 'float':
227  if(!(is_float($function)))
228  {
229  return array("status"=>"no$key",
230  "value"=>var_export($function,true)
231  );
232  }
233  break;
234  case '%b':
235  case 'bool':
236  case 'boolean':
237  if(!(is_bool($function)))
238  {
239  return array("status"=>"no$key",
240  "value"=>var_export($function,true)
241  );
242  }
243  break;
244  case 'email':
245  if(!(filter_var($function, FILTER_VALIDATE_EMAIL)))
246  {
247  return array("status"=>"no$key",
248  "value"=>var_export($function,true)
249  );
250  }
251  break;
252  case 'ip':
253  if(!(filter_var($function, FILTER_VALIDATE_IP)))
254  {
255  return array("status"=>"no$key",
256  "value"=>var_export($function,true)
257  );
258  }
259  break;
260  case 'url':
261  if(!(filter_var($function, FILTER_VALIDATE_URL)))
262  {
263  return array("status"=>"no$key",
264  "value"=>var_export($function,true)
265  );
266  }
267  break;
268  case 'regexp':
269  if(!(filter_var($function, FILTER_VALIDATE_REGEXP)))
270  {
271  return array("status"=>"no$key",
272  "value"=>var_export($function,true)
273  );
274  }
275  break;
276  case 'arr':
277  case 'array':
278  if(!(is_array($function)))
279  {
280  return array("status"=>"no$key",
281  "value"=>var_export($function,true)
282  );
283  }
284  break;
285  case 'obj':
286  case 'object':
287  if(!(is_object($function)))
288  {
289  return array("status"=>"no$key",
290  "value"=>var_export($function,true)
291  );
292  }
293  break;
294  case '=':
295  case '==':
296  if($val!=$function)
297  {
298  return array("status"=>"no$key",
299  "value"=>var_export($function,true)
300  );
301  }
302  break;
303  case '<>':
304  case '!=':
305  if(!($val!=$function))
306  {
307  return array("status"=>"no$key",
308  "value"=>var_export($function,true)
309  );
310  }
311  break;
312  case '<':
313  if(!($val>$function))
314  {
315  return array("status"=>"no$key",
316  "value"=>var_export($function,true)
317  );
318  }
319  break;
320  case '>':
321  if(!($val<$function))
322  {
323  return array("status"=>"no$key",
324  "value"=>var_export($function,true)
325  );
326  }
327  break;
328  case '<=':
329  if(!($val>=$function))
330  {
331  return array("status"=>"no$key",
332  "value"=>var_export($function,true)
333  );
334  }
335  break;
336  case '>=':
337  if(!($val<=$function))
338  {
339  return array("status"=>"no$key",
340  "value"=>var_export($function,true)
341  );
342  }
343  break;
344  }
345  }
346  return array("status"=>"ok",
347  "output"=>$function,
348  "value"=>var_export($function,true)
349  );
350  }
351  return $function;
352  }
353 
358  public function print_ut($export,$die=true)
359  {
360  if($this->env==='developer')
361  {
362  if($export['status']=="ok")
363  {
364  $console='info';
365  }
366  else
367  {
368  $console='error';
369  }
370  print "<script type='text/javascript'>console.$console('status: ".$export['status']."');</script>";
371  print "<script type='text/javascript'>console.$console('value: ".$export['value']."');</script>";
372  if($die){die('STOP:500:by Unit Test');}
373  }
374  return $export;
375  }
376 }
377 ?>
saveToLog($data)
__construct($path='', $file='log.txt', $maxLine=100, $useFile=true, $mysql=null)
print_ut($export, $die=true)
setEnvironment($public=true)