/tests_runner/tests/common.inc.php

https://github.com/r-kitaev/limb · PHP · 199 lines · 161 code · 31 blank · 7 comment · 8 complexity · e47cd83edc696814d434b1f3aaf19f43 MD5 · raw file

  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. class GeneratedTestClass
  10. {
  11. protected $class_name;
  12. protected $parent_class_name = 'UnitTestCase';
  13. protected $body;
  14. function __construct($body = '%class%')
  15. {
  16. $this->class_name = 'GenClass_' . mt_rand(1, 10000) . uniqid();
  17. $this->body = $body;
  18. }
  19. function getClass()
  20. {
  21. return $this->class_name;
  22. }
  23. function setParentClass($parent_class_name)
  24. {
  25. $this->parent_class_name = $parent_class_name;
  26. }
  27. function getParentClass()
  28. {
  29. return $this->parent_class_name;
  30. }
  31. function getFileName()
  32. {
  33. return $this->class_name . ".class.php";
  34. }
  35. function getOutput()
  36. {
  37. return $this->class_name . "\n";
  38. }
  39. function generate($test_body = false)
  40. {
  41. $code = '';
  42. $code .= "<?php\n";
  43. $code .= $this->generateClass($test_body);
  44. $code .= "\n?>";
  45. return $code;
  46. }
  47. function generateFailing()
  48. {
  49. $code = '';
  50. $code .= "<?php\n";
  51. $code .= $this->generateClassFailing();
  52. $code .= "\n?>";
  53. return $code;
  54. }
  55. function generateClass($test_body = false)
  56. {
  57. if (!$test_body)
  58. $test_body = "echo \"" . $this->getOutput() . "\";";
  59. $parts = array();
  60. $parts['%class_header%'] = "\nclass {$this->class_name} extends {$this->parent_class_name} {\n";
  61. $parts['%class_footer%'] = "\n}\n";
  62. $parts['%class%'] = $parts['%class_header%'] .
  63. "function testMe()\n{\n".$test_body."\n}" .
  64. $parts['%class_footer%'];
  65. return str_replace(array_keys($parts), array_values($parts), $this->body);
  66. }
  67. function generateClassFailing()
  68. {
  69. return $this->generateClass("\$this->assertTrue(false);echo \"" . $this->getOutput() . "\";");
  70. }
  71. function generateClassThrowingException()
  72. {
  73. return $this->generateClass("throw new Exception('thrown from {$this->getOutput()}');");
  74. }
  75. }
  76. abstract class lmbTestRunnerBase extends UnitTestCase
  77. {
  78. function _rmdir($path)
  79. {
  80. if(!is_dir($path))
  81. return;
  82. $dir = opendir($path);
  83. while($entry = readdir($dir))
  84. {
  85. if(is_file("$path/$entry"))
  86. unlink("$path/$entry");
  87. elseif(is_dir("$path/$entry") && $entry != '.' && $entry != '..')
  88. $this->_rmdir("$path/$entry");
  89. }
  90. closedir($dir);
  91. $res = rmdir($path);
  92. clearstatcache();
  93. return $res;
  94. }
  95. function _createTestCase($file, $body = '%class%')
  96. {
  97. $this->_createDirForFile($file);
  98. $generated = new GeneratedTestClass($body);
  99. file_put_contents($file, "<?php\n" . $generated->generateClass() . "\n?>");
  100. return $generated;
  101. }
  102. function _createTestCaseFailing($file, $body = '%class%')
  103. {
  104. $this->_createDirForFile($file);
  105. $generated = new GeneratedTestClass($body);
  106. file_put_contents($file, "<?php\n" . $generated->generateClassFailing() . "\n?>");
  107. return $generated;
  108. }
  109. function _createTestCaseThrowingException($file, $body = '%class%')
  110. {
  111. $this->_createDirForFile($file);
  112. $generated = new GeneratedTestClass($body);
  113. file_put_contents($file, "<?php\n" . $generated->generateClassThrowingException() . "\n?>");
  114. return $generated;
  115. }
  116. protected function _createDirForFile($file)
  117. {
  118. $dir = dirname($file);
  119. if (!is_dir($dir)) {
  120. mkdir($dir, 0777, true);
  121. }
  122. }
  123. function _runNodeAndAssertOutput($node, $expected)
  124. {
  125. ob_start();
  126. $group = $node->createTestCase();
  127. $group->run(new SimpleReporter());
  128. $str = ob_get_contents();
  129. ob_end_clean();
  130. $this->assertEqual($str, $expected);
  131. }
  132. }
  133. class lmbTestReporter extends SimpleReporter
  134. {
  135. protected $output = '';
  136. function __construct()
  137. {
  138. $this->SimpleReporter();
  139. }
  140. function paintFail($message)
  141. {
  142. parent::paintFail($message);
  143. $this->_addToOutput('FAIL', $message);
  144. }
  145. function paintError($message)
  146. {
  147. parent::paintError($message);
  148. $this->_addToOutput('FORMATED', $message);
  149. }
  150. function paintException($exception)
  151. {
  152. parent::paintException($exception);
  153. $this->_addToOutput('FORMATED', $message);
  154. }
  155. function paintSkip($message)
  156. {
  157. parent::paintSkip($message);
  158. $this->_addToOutput('FORMATED', $message);
  159. }
  160. protected function _addToOutput($title, $message)
  161. {
  162. $this->output .= $title . ': ' . $message . PHP_EOL;
  163. }
  164. function getOutput()
  165. {
  166. return $this->output;
  167. }
  168. }