- <?php
- /**
- * Test runner
- *
- * @author Joshua Eichorn <josh@bluga.net>
- * @copyright Joshua Eichorn 2004
- * @package PHPCodeAnalyzer
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * @license http://www.gnu.org/copyleft/lesser.html LGPL
- */
-
- /**
- * Include the code analyzer so we can test it
- */
- require_once 'PHPCodeAnalyzer.php';
- require_once 'Text/Diff.php';
- require_once 'Text/Diff/Renderer.php';
- require_once 'Text/Diff/Renderer/unified.php';
-
-
- /**
- * Test runner class
- *
- * @version 0.3
- * @license http://www.gnu.org/copyleft/lesser.html LGPL
- * @copyright Joshua Eichorn 2004
- * @package PHPCodeAnalyzer
- * @author Joshua Eichorn <josh@bluga.net>
- */
- class PHPcaTester
- {
- /**
- * Output class
- */
- var $output = null;
-
- /**
- * List of tests to run
- * @access private
- */
- var $tests = array();
-
- /**
- * Test test directory
- */
- var $testDirectory = "source/latest/tests";
-
- /**
- * Setup test idr
- */
- function PHPcaTester()
- {
- $this->testDirectory = dirname(__FILE__)."/tests";
- }
-
- /**
- * Build a list of tests
- */
- function buildTestList()
- {
- $d = dir($this->testDirectory);
- while($entry = $d->read())
- {
- if (preg_match('/(.+)\.test/',$entry,$match))
- {
- $this->tests[] = $match[1];
- }
- }
- }
-
- /**
- * Run a single test
- *
- * @param string $namn
- */
- function runTest($name,$returnOutput = false)
- {
- // setup
- $ca = new PHPCodeAnalyzer();
-
- $ca->source = file_get_contents("$this->testDirectory/$name.test");
- $ca->analyze();
-
- // print_r any vars that aren't empty
- ob_start();
- foreach($ca as $var => $data)
- {
- if (is_array($data) && count($data) > 0)
- {
- echo "$var\n";
- print_r($data);
- echo "\n\n";
- }
- }
- $output = ob_get_contents();
- ob_end_clean();
- if ($returnOutput)
- {
- return $output;
- }
-
- $match = file_get_contents("$this->testDirectory/$name.result");
-
- if (strcmp($match,$output) == 0)
- {
- $this->output->outputResult($name,true,"");
- }
- else
- {
- $diff = new Text_Diff(explode("\n",$output),explode("\n",$match));
- $render = new Text_Diff_Renderer_unified();
- $this->output->outputResult($name,false,$render->render($diff));
- }
- }
-
- /**
- * Run all the tests giving output
- */
- function runTests()
- {
- $this->buildTestList();
- $this->output->setup();
- foreach($this->tests as $test)
- {
- $this->runTest($test);
- }
- $this->output->tearDown();
- }
-
- }
-
- /**
- * Console table output class
- */
- class PHPcaTesterOutputHtml
- {
- /**
- * Setup output
- */
- function setup()
- {
- echo '<html><head><style type="text/css" media="screen">@import url("test.css"); </style></head><body>';
- echo "<h1>PHPCodeAnalyzer Test run: ".date("r")."</h1>";
-
- echo "<table cellspacing=0 cellpadding=3 border=1>\n<tr><th>Test</th><th>Status</th><th>Diff</th></tr>";
- }
-
- /**
- * Output the results of a test
- */
- function outputResult($test,$passed,$extra)
- {
- if ($passed)
- {
- echo "<tr class='passed'><td>$test</td><td>Passed</td><td><pre>$extra</pre></td></tr>";
- } else
- {
- echo "<tr class='failed'><td>$test</td><td>Failed</td><td><pre>$extra</pre></td></tr>";
- }
- }
-
- /**
- * Test down the output
- */
- function tearDown()
- {
- echo "</table></body></html>";
- }
-
- }
-
- $tester = new PHPcaTester();
- if (isset($argv[1]))
- {
- echo "#############################\n";
- echo $tester->runTest($argv[1],true);
- echo "#############################\n";
- exit;
- }
-
- $tester->output = new PHPcaTesterOutputHtml();
- $tester->runTests();
- ?>