3 namespace Drupal\Tests\Listeners;
6 * Defines a class for providing html output results for functional tests.
8 class HtmlOutputPrinter extends \PHPUnit_TextUI_ResultPrinter {
11 * File to write html links to.
15 protected $browserOutputFile;
20 public function __construct($out, $verbose, $colors, $debug, $numberOfColumns) {
21 parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns);
22 if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) {
23 // Initialize html output debugging.
24 $html_output_directory = rtrim($html_output_directory, '/');
26 // Check if directory exists.
27 if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) {
28 $this->writeWithColor('bg-red, fg-black', "HTML output directory $html_output_directory is not a writable directory.");
31 // Convert to a canonicalized absolute pathname just in case the current
32 // working directory is changed.
33 $html_output_directory = realpath($html_output_directory);
34 $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_');
35 if ($this->browserOutputFile) {
36 touch($this->browserOutputFile);
39 $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory.");
44 if ($this->browserOutputFile) {
45 putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
48 // Remove any environment variable.
49 putenv('BROWSERTEST_OUTPUT_FILE');
56 public function printResult(\PHPUnit_Framework_TestResult $result) {
57 parent::printResult($result);
59 if ($this->browserOutputFile) {
60 $contents = file_get_contents($this->browserOutputFile);
62 $this->writeNewLine();
63 $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
64 $this->write($contents);
66 // No need to keep the file around any more.
67 unlink($this->browserOutputFile);