PHPCodeAnalyzer
[ class tree: PHPCodeAnalyzer ] [ index: PHPCodeAnalyzer ] [ all elements ]

Source for file tester.php

Documentation is available at tester.php

  1. <?php
  2. /**
  3. * Test runner
  4. *
  5. * @author Joshua Eichorn <josh@bluga.net>
  6. * @copyright Joshua Eichorn 2004
  7. * @package PHPCodeAnalyzer
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation; either version 2.1 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  20. */
  21.  
  22. /**
  23. * Include the code analyzer so we can test it
  24. */
  25. require_once 'PHPCodeAnalyzer.php';
  26. require_once 'Text/Diff.php';
  27. require_once 'Text/Diff/Renderer.php';
  28. require_once 'Text/Diff/Renderer/unified.php';
  29.  
  30.  
  31. /**
  32. * Test runner class
  33. *
  34. * @version 0.3
  35. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  36. * @copyright Joshua Eichorn 2004
  37. * @package PHPCodeAnalyzer
  38. * @author Joshua Eichorn <josh@bluga.net>
  39. */
  40. class PHPcaTester
  41. {
  42. /**
  43. * Output class
  44. */
  45. var $output = null;
  46.  
  47. /**
  48. * List of tests to run
  49. * @access private
  50. */
  51. var $tests = array();
  52.  
  53. /**
  54. * Test test directory
  55. */
  56. var $testDirectory = "source/latest/tests";
  57.  
  58. /**
  59. * Setup test idr
  60. */
  61. function PHPcaTester()
  62. {
  63. $this->testDirectory = dirname(__FILE__)."/tests";
  64. }
  65.  
  66. /**
  67. * Build a list of tests
  68. */
  69. function buildTestList()
  70. {
  71. $d = dir($this->testDirectory);
  72. while($entry = $d->read())
  73. {
  74. if (preg_match('/(.+)\.test/',$entry,$match))
  75. {
  76. $this->tests[] = $match[1];
  77. }
  78. }
  79. }
  80.  
  81. /**
  82. * Run a single test
  83. *
  84. * @param string $namn
  85. */
  86. function runTest($name,$returnOutput = false)
  87. {
  88. // setup
  89. $ca = new PHPCodeAnalyzer();
  90. $ca->source = file_get_contents("$this->testDirectory/$name.test");
  91. $ca->analyze();
  92.  
  93. // print_r any vars that aren't empty
  94. ob_start();
  95. foreach($ca as $var => $data)
  96. {
  97. if (is_array($data) && count($data) > 0)
  98. {
  99. echo "$var\n";
  100. print_r($data);
  101. echo "\n\n";
  102. }
  103. }
  104. $output = ob_get_contents();
  105. ob_end_clean();
  106. if ($returnOutput)
  107. {
  108. return $output;
  109. }
  110.  
  111. $match = file_get_contents("$this->testDirectory/$name.result");
  112.  
  113. if (strcmp($match,$output) == 0)
  114. {
  115. $this->output->outputResult($name,true,"");
  116. }
  117. else
  118. {
  119. $diff = new Text_Diff(explode("\n",$output),explode("\n",$match));
  120. $render = new Text_Diff_Renderer_unified();
  121. $this->output->outputResult($name,false,$render->render($diff));
  122. }
  123. }
  124.  
  125. /**
  126. * Run all the tests giving output
  127. */
  128. function runTests()
  129. {
  130. $this->buildTestList();
  131. $this->output->setup();
  132. foreach($this->tests as $test)
  133. {
  134. $this->runTest($test);
  135. }
  136. $this->output->tearDown();
  137. }
  138. }
  139.  
  140. /**
  141. * Console table output class
  142. */
  143. class PHPcaTesterOutputHtml
  144. {
  145. /**
  146. * Setup output
  147. */
  148. function setup()
  149. {
  150. echo '<html><head><style type="text/css" media="screen">@import url("test.css"); </style></head><body>';
  151. echo "<h1>PHPCodeAnalyzer Test run: ".date("r")."</h1>";
  152.  
  153. echo "<table cellspacing=0 cellpadding=3 border=1>\n<tr><th>Test</th><th>Status</th><th>Diff</th></tr>";
  154. }
  155.  
  156. /**
  157. * Output the results of a test
  158. */
  159. function outputResult($test,$passed,$extra)
  160. {
  161. if ($passed)
  162. {
  163. echo "<tr class='passed'><td>$test</td><td>Passed</td><td><pre>$extra</pre></td></tr>";
  164. } else
  165. {
  166. echo "<tr class='failed'><td>$test</td><td>Failed</td><td><pre>$extra</pre></td></tr>";
  167. }
  168. }
  169.  
  170. /**
  171. * Test down the output
  172. */
  173. function tearDown()
  174. {
  175. echo "</table></body></html>";
  176. }
  177. }
  178.  
  179. $tester = new PHPcaTester();
  180. if (isset($argv[1]))
  181. {
  182. echo "#############################\n";
  183. echo $tester->runTest($argv[1],true);
  184. echo "#############################\n";
  185. exit;
  186. }
  187.  
  188. $tester->output = new PHPcaTesterOutputHtml();
  189. $tester->runTests();
  190. ?>

Documentation generated on Sun, 12 Dec 2004 17:15:31 -0600 by phpDocumentor 1.3.0RC3