dedlfix: Fragen zu PHPUnit

Beitrag lesen

echo $begrüßung;

Gibt es vll. irgendwelche Plugins für Eclipse, wie bei JUnit oder sonstiges Software die das Testen etwas angenehmer gestalten.

Die in Example 7.1 und 7.2 dargestellten Scripte lassen sich mit vorangestelltem

if (PHP_SAPI != 'cli' and !headers_sent()) {  
  echo '<pre>';  
  ob_start('htmlspecialchars');  
}

browsertauglich machen und dort aufrufen. Die Ausgabe ist dabei von der selben schlichten Eleganz wie an der Kommandozeile.

Etwas bunter wird es mit folgendem Script. Die passenden Punkte für die Farben im Array $colors fand ich in SELFHTMLs Beispielen zu Grafiksorten.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!-- ggf. anpassen -->  
<title>Fx-Tests</title>  
<style type="text/css">  
  ul { list-style-type: none; padding: 0; }  
  ul ul { padding-left: 1em; }  
  li { font-family: monospace; }  
  li p { margin: 0; padding: 0; padding-left: 2em; white-space: pre; }  
</style>  
</head>  
<body>  
  

~~~~~~php
<?php  
if (!defined('PHPUnit2_MAIN_METHOD'))  
  define('PHPUnit2_MAIN_METHOD', 'runAllTests');  
  
require_once 'AllTests.php';  
  
class UlLiTestListener implements PHPUnit2_Framework_TestListener {  
  private $results = array();  
  private $result = '';  
  
  public function getResult() {  
    return $this->result;  
  }  
  
  public function addError(PHPUnit2_Framework_Test $test, Exception $e) {  
    $this->results[$test->getName()]['type'] = 'error';  
    $this->results[$test->getName()]['exception'] = $e;  
  }  
  
  public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {  
    $this->results[$test->getName()]['type'] = 'failure';  
    $this->results[$test->getName()]['exception'] = $e;  
  }  
  
  public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e) {  
    $this->results[$test->getName()]['type'] = 'incomplete';  
    $this->results[$test->getName()]['exception'] = $e;  
  }  
  
  public function addSkippedTest(PHPUnit2_Framework_Test $test, Exception $e) {  
    $this->results[$test->getName()]['type'] = 'skipped';  
    $this->results[$test->getName()]['exception'] = $e;  
  }  
  
  public function startTest(PHPUnit2_Framework_Test $test) {  
    $this->results[$test->getName()] = null;  
    $this->result .= '<li>';  
  }  
  
  public function endTest(PHPUnit2_Framework_Test $test) {  
    if (empty($this->results[$test->getName()])) {  
      $this->results[$test->getName()] = $test->getName() . ' passed.';  
      $this->result .= "<img src='green.gif'> {$test->getName()}";  
    } else {  
      $result = $this->results[$test->getName()];  
      $colors = array(  
        'error' => 'fuchsia',  
        'failure' => 'red',  
        'incomplete' => 'yellow',  
        'skipped' => 'blue');  
      $color = isset($colors[$result['type']]) ? $colors[$result['type']] : 'gray';  
      $this->result .= "<img src='$color.gif'> {$test->getName()}";  
      $this->result .= "<p>" . htmlspecialchars($result['exception']->getMessage());  
      if ($result['type'] == 'skipped')  
        $this->result .= " skipped";  
      elseif (is_callable(array($result['exception'], 'getLocation'))) {  
        $location = $result['exception']->getLocation();  
        $this->result .= "File: " . $location['file'];  
        $this->result .= "Line: " . $location['line'];  
      }  
      $this->result .= "</p>";  
    }  
     $this->result .= "</li>\n";  
  }  
  
  public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) {  
    $this->result .= "<li>TestSuite: {$suite->getName()}<ul>\n";  
  }  
  
  public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) {  
    $this->result .= "</ul></li>\n";  
  }  
} //UlLiTestListener  
  
$listener = new UlLiTestListener();  
$result = new PHPUnit2_Framework_TestResult;  
$result->addListener($listener);  
  
// Run the tests.  
AllTests::suite()->run($result);  
echo '<ul>', $listener->getResult(), '</ul>';  
  
if ($result->wasSuccessful()) {  
  echo 'O.K. ', $result->count(), ' Tests.';  
} else {  
  echo "<p>Fehler</p>\n";  
  echo '<ul>';  
  echo '<li>Tests: ', $result->count(), "</li>\n";  
  if ($failure = $result->failureCount())  
    echo "<li>Failures: $failure</li>\n";  
  if ($error = $result->errorCount())  
    echo "<li>Errors: $error</li>\n";  
  if ($notImplemented = $result->notImplementedCount())  
    echo "<li>Incomplete: $notImplemented</li>\n";  
  if ($skipped = $result->skippedCount())  
    echo "<li>Skipped: $skipped</li>\n";  
  echo '</ul>';  
}  
  
?>  
</body>  
</html>

echo "$verabschiedung $name";