Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Listeners / HtmlOutputPrinter.php
1 <?php
2
3 namespace Drupal\Tests\Listeners;
4
5 /**
6  * Defines a class for providing html output results for functional tests.
7  */
8 class HtmlOutputPrinter extends \PHPUnit_TextUI_ResultPrinter {
9
10   /**
11    * File to write html links to.
12    *
13    * @var string
14    */
15   protected $browserOutputFile;
16
17   /**
18    * {@inheritdoc}
19    */
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, '/');
25
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.");
29       }
30       else {
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);
37         }
38         else {
39           $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory.");
40         }
41       }
42     }
43
44     if ($this->browserOutputFile) {
45       putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
46     }
47     else {
48       // Remove any environment variable.
49       putenv('BROWSERTEST_OUTPUT_FILE');
50     }
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function printResult(\PHPUnit_Framework_TestResult $result) {
57     parent::printResult($result);
58
59     if ($this->browserOutputFile) {
60       $contents = file_get_contents($this->browserOutputFile);
61       if ($contents) {
62         $this->writeNewLine();
63         $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
64         $this->write($contents);
65       }
66       // No need to keep the file around any more.
67       unlink($this->browserOutputFile);
68     }
69   }
70
71 }