PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 14ms app.codeStats 0ms

/test/base.php

https://github.com/Yitsushi/Tiny-PHP-Tester
PHP | 103 lines | 88 code | 15 blank | 0 comment | 20 complexity | 95eb50cdc72c0eb8ecaaac7c5a054889 MD5 | raw file
  1. <?php
  2. namespace wsTestLib;
  3. if (!defined('TEST')) {
  4. define('TEST', 'test');
  5. }
  6. require_once(dirname(__FILE__).'/lib/wsResult.php');
  7. class wsBase {
  8. public function __construct() {
  9. $this->r = new wsResult();
  10. }
  11. public function find_and_call_tests() {
  12. $dir = opendir(\TEST."/cases");
  13. if ($dir == null) die(' >>> WTF! NEED cases dir...');
  14. while (($file = readdir($dir)) !== false) {
  15. if ( in_array($file, array('.', '..'))
  16. or (preg_match('/^\./',$file))
  17. or (!preg_match('/\.php$/',$file))) continue;
  18. require_once TEST."/cases/{$file}";
  19. $class_name = preg_replace('/\.php$/', '', $file);
  20. $class = __NAMESPACE__ . "\\" . $class_name;
  21. $c = new $class();
  22. if (property_exists($c, "name")) $case = $c->name;
  23. else $case = strtolower(preg_replace('/([A-Z])/', '_$1', $class_name));
  24. if (property_exists($c, "skip") and $c->skip === true) {
  25. $this->r->$case->skip();
  26. unset($c);
  27. continue;
  28. }
  29. ob_start();
  30. if(method_exists($c, 'sunrise')) $c->sunrise(); // __construct = sunrise (why not?)
  31. ob_end_clean();
  32. foreach(get_class_methods($c) as $m) {
  33. if (!preg_match('/^test_/', $m)) continue;
  34. $prefix = preg_replace('/^test_/', '', $m);
  35. $this->r->$case->set_test_namespace($prefix);
  36. $c->$m($this->r->$case);
  37. }
  38. ob_start();
  39. if(method_exists($c, 'sunset')) $c->sunset(); // anti__construct = sunset (why not?)
  40. ob_end_clean();
  41. unset($c);
  42. }
  43. }
  44. public function finish() {
  45. return $this->r->finish();
  46. }
  47. }
  48. $_colors = array(
  49. 'LIGHT_RED' => "[1;31m",
  50. 'LIGHT_GREEN' => "[1;32m",
  51. 'YELLOW' => "[1;33m",
  52. 'LIGHT_BLUE' => "[1;34m",
  53. 'MAGENTA' => "[1;35m",
  54. 'LIGHT_CYAN' => "[1;36m",
  55. 'WHITE' => "[1;37m",
  56. 'NORMAL' => "[0m",
  57. 'BLACK' => "[0;30m",
  58. 'RED' => "[0;31m",
  59. 'GREEN' => "[0;32m",
  60. 'BROWN' => "[0;33m",
  61. 'BLUE' => "[0;34m",
  62. 'CYAN' => "[0;36m",
  63. 'BOLD' => "[1m",
  64. 'UNDERSCORE' => "[4m",
  65. 'REVERSE' => "[7m"
  66. );
  67. function termcolored($text, $color="NORMAL", $back=1){
  68. if (NOCOLOR) {
  69. if ($back) return "{$text}";
  70. else echo "{$text}";
  71. return false;
  72. }
  73. global $_colors;
  74. $out = $_colors["{$color}"];
  75. if($out == ""){ $out = "[0m"; }
  76. if($back){
  77. return chr(27)."{$out}{$text}".chr(27)."[0m";
  78. }else{
  79. echo chr(27)."{$out}$text".chr(27).chr(27)."[0m";
  80. }
  81. return true;
  82. }
  83. if (basename(__FILE__) == basename($argv[0])) {
  84. $b = new wsBase();
  85. $b->find_and_call_tests();
  86. $exit_code = $b->finish();
  87. exit($exit_code == false);
  88. }