Buffer contents of HtmlReporter.

Collect HTML output into a buffer so we can provide compatibility across
PHPUnit 3.x and 4.x

Refs #11588
This commit is contained in:
mark_story 2017-12-30 23:37:10 -05:00
parent 51206d7358
commit d2c4bf2bb2

View file

@ -25,6 +25,13 @@ App::uses('CakeBaseReporter', 'TestSuite/Reporter');
*/ */
class CakeHtmlReporter extends CakeBaseReporter { class CakeHtmlReporter extends CakeBaseReporter {
/**
* The content buffer
*
* @var string
*/
protected $_buffer = '';
/** /**
* Paints the top of the web page setting the * Paints the top of the web page setting the
* title to the name of the starting test. * title to the name of the starting test.
@ -33,12 +40,13 @@ class CakeHtmlReporter extends CakeBaseReporter {
*/ */
public function paintHeader() { public function paintHeader() {
$this->_headerSent = true; $this->_headerSent = true;
ob_start();
$this->sendContentType(); $this->sendContentType();
$this->sendNoCacheHeaders(); $this->sendNoCacheHeaders();
$this->paintDocumentStart(); $this->paintDocumentStart();
$this->paintTestMenu(); $this->paintTestMenu();
echo "<ul class='tests'>\n"; echo "<ul class='tests'>\n";
ob_end_flush(); $this->_buffer = ob_get_clean();
} }
/** /**
@ -58,7 +66,6 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintDocumentStart() { public function paintDocumentStart() {
ob_start();
$baseDir = $this->params['baseDir']; $baseDir = $this->params['baseDir'];
include CAKE . 'TestSuite' . DS . 'templates' . DS . 'header.php'; include CAKE . 'TestSuite' . DS . 'templates' . DS . 'header.php';
} }
@ -135,7 +142,9 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintFooter($result) { public function paintFooter($result) {
echo $this->_buffer;
ob_end_flush(); ob_end_flush();
$colour = ($result->failureCount() + $result->errorCount() > 0 ? "red" : "green"); $colour = ($result->failureCount() + $result->errorCount() > 0 ? "red" : "green");
echo "</ul>\n"; echo "</ul>\n";
echo "<div style=\""; echo "<div style=\"";
@ -248,6 +257,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintFail($message, $test) { public function paintFail($message, $test) {
ob_start();
$trace = $this->_getStackTrace($message); $trace = $this->_getStackTrace($message);
$className = get_class($test); $className = get_class($test);
$testName = $className . '::' . $test->getName() . '()'; $testName = $className . '::' . $test->getName() . '()';
@ -286,6 +296,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
} }
echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n"; echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
echo "</li>\n"; echo "</li>\n";
$this->_buffer .= ob_get_clean();
} }
/** /**
@ -298,6 +309,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintPass(PHPUnit_Framework_Test $test, $time = null) { public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
ob_start();
if (isset($this->params['showPasses']) && $this->params['showPasses']) { if (isset($this->params['showPasses']) && $this->params['showPasses']) {
echo "<li class='pass'>\n"; echo "<li class='pass'>\n";
echo "<span>Passed</span> "; echo "<span>Passed</span> ";
@ -305,6 +317,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
echo "<br />" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n"; echo "<br />" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n";
echo "</li>\n"; echo "</li>\n";
} }
$this->_buffer .= ob_get_clean();
} }
/** /**
@ -315,6 +328,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintException($message, $test) { public function paintException($message, $test) {
ob_start();
$trace = $this->_getStackTrace($message); $trace = $this->_getStackTrace($message);
$testName = get_class($test) . '(' . $test->getName() . ')'; $testName = get_class($test) . '(' . $test->getName() . ')';
@ -325,6 +339,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n"; echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n"; echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
echo "</li>\n"; echo "</li>\n";
$this->_buffer .= ob_get_clean();
} }
/** /**
@ -335,10 +350,12 @@ class CakeHtmlReporter extends CakeBaseReporter {
* @return void * @return void
*/ */
public function paintSkip($message, $test) { public function paintSkip($message, $test) {
ob_start();
echo "<li class='skipped'>\n"; echo "<li class='skipped'>\n";
echo "<span>Skipped</span> "; echo "<span>Skipped</span> ";
echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage()); echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage());
echo "</li>\n"; echo "</li>\n";
$this->_buffer .= ob_get_clean();
} }
/** /**
@ -390,9 +407,9 @@ class CakeHtmlReporter extends CakeBaseReporter {
*/ */
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
if (!$this->_headerSent) { if (!$this->_headerSent) {
echo $this->paintHeader(); $this->paintHeader();
} }
echo '<h2>' . __d('cake_dev', 'Running %s', $suite->getName()) . '</h2>'; $this->_buffer .= '<h2>' . __d('cake_dev', 'Running %s', $suite->getName()) . '</h2>';
} }
/** /**