1: <?php
  2: 
  3: 
  4: 
  5: require('./qcubed.inc.php');
  6: 
  7: 
  8: restore_error_handler();
  9: 
 10: require_once(__QCUBED_CORE__ . '/tests/qcubed-unit/QUnitTestCaseBase.php');
 11: require_once(__QCUBED_CORE__ . '/tests/qcubed-unit/QTestControl.class.php');
 12: 
 13: 
 14: class QHtmlReporter extends PHPUnit_TextUI_ResultPrinter {
 15:     protected $results;
 16:     protected $currentSuite;
 17:     protected $currentTest;
 18: 
 19:     public function write($buffer)
 20:     {
 21:     }
 22:     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
 23:     {
 24:         $this->currentSuite = $suite->getName();
 25:     }
 26: 
 27:     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
 28:     {
 29:         $this->currentSuite = null;
 30:     }
 31: 
 32: 
 33:     public function startTest(PHPUnit_Framework_Test $test)
 34:     {
 35:         $this->currentTest = $test->getName();
 36:         $this->results[$this->currentSuite][$test->getName()]['test'] = $test;
 37:     }
 38: 
 39:     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
 40:         $this->results[$this->currentSuite][$test->getName()]['status'] = 'error';
 41:         $this->results[$this->currentSuite][$test->getName()]['errors'][] = compact('e', 'time');
 42:     }
 43:     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
 44:         $this->results[$this->currentSuite][$test->getName()]['status'] = 'failed';
 45:         $this->results[$this->currentSuite][$test->getName()]['results'][] = compact('e', 'time');
 46:     }
 47:     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
 48:         $this->results[$this->currentSuite][$test->getName()]['status'] = 'incomplete';
 49:         $this->results[$this->currentSuite][$test->getName()]['errors'][] = compact('e', 'time');
 50:     }
 51:     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
 52:         $this->results[$this->currentSuite][$test->getName()]['status'] = 'skipped';
 53:         $this->results[$this->currentSuite][$test->getName()]['errors'][] = compact('e', 'time');
 54:     }
 55: 
 56:     public function endTest(PHPUnit_Framework_Test $test, $time) {
 57:         $t = &$this->results[$this->currentSuite][$test->getName()];
 58:         if (!isset($t['status'])) {
 59:             $t['status'] = 'passed';
 60:         }
 61:         $t['time'] = $time;
 62:         $this->currentTest = null;
 63:     }
 64: 
 65:     public function printResult(PHPUnit_Framework_TestResult $result)
 66:     {
 67:         echo('<h1>QCubed ' . QCUBED_VERSION_NUMBER_ONLY . ' Unit Tests - PHPUnit ' . PHPUnit_Runner_Version::id() . '</h1>');
 68: 
 69:         foreach ($this->results as $suiteName=>$suite) {
 70:             $strHtml = "<b>$suiteName</b><br />";
 71:             foreach ($suite as $testName=>$test) {
 72:                 $status = $test['status'];
 73:                 $status = ucfirst($status);
 74:                 if ($test['status'] !== 'passed') {
 75:                     $status = '<span style="color:red">' . $status . '</span>';
 76:                 } else {
 77:                     $status = '<span style="color:green">' . $status . '</span>';
 78:                 }
 79: 
 80:                 $strHtml .= "$status: $testName";
 81:                 $strHtml = "$strHtml<br />";
 82:                 if (isset($test['errors'])) foreach ($test['errors'] as $error){
 83:                     $strHtml .= nl2br(htmlentities($error['e']->__toString())) . '<br />';
 84:                 }
 85:                 if (isset($test['results'])) foreach ($test['results'] as $error) {
 86:                     $strMessage = $error['e']->toString() . "\n";
 87:                     
 88:                     $lines = explode ("\n", PHPUnit_Util_Filter::getFilteredStacktrace($error['e']));
 89:                     $strMessage .= $lines[0] . "\n";
 90:                     $strHtml .= nl2br(htmlentities($strMessage)) . '<br />';
 91:                 }
 92:             }
 93:             echo $strHtml;
 94: 
 95:         }
 96: 
 97:         $str = "\nRan " . $result->count() . " tests in " . $result->time() . " seconds.\n";
 98:         $str .= $result->failureCount() . " assertions failed.\n";
 99:         $str .= $result->errorCount() . " exceptions were thrown.\n";
100:         echo nl2br($str);
101: 
102:     }
103: 
104: }
105: 
106: 
107: 
108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 
136: 
137: class QTestForm extends QForm {
138:     public $ctlTest;
139:     public $btnRunTests;
140:     public $lblRunning;
141:     public $pnlOutput;
142: 
143:     protected function Form_Create() {
144:         $this->ctlTest = new QTestControl($this);
145:         $this->pnlOutput = new QPanel($this, 'outputPanel');
146:         $this->btnRunTests = new QButton($this);
147:         $this->btnRunTests->Text = "Run Tests";
148:         $this->btnRunTests->AddAction(new QClickEvent(), new QAjaxAction('startTesting'));
149: 
150:         $this->lblRunning = new QLabel($this);
151:         $this->lblRunning->Text = "Running, please wait...";
152:         $this->lblRunning->Visible = false;
153:     }
154: 
155:     protected function startTesting() {
156:         $this->lblRunning->Visible = true;
157:         
158:         $t1 = new QJsTimer($this, 200, false, true, 'timer1');
159:         $t1->AddAction(new QTimerExpiredEvent(), new QAjaxAction ('preTest'));
160:         $t2 = new QJsTimer($this, 201, false, true, 'timer2');
161:         $t2->AddAction(new QTimerExpiredEvent(), new QAjaxAction ('preTest2'));
162:         $t3 = new QJsTimer($this, 400, false, true, 'timer3');
163:         $t3->AddAction(new QTimerExpiredEvent(), new QServerAction ('runTests'));
164:     }
165:     
166:     public function preTest() {
167:         $this->ctlTest->savedValue1 = 2;    
168:     }
169:     
170:     public function preTest2() {
171:         $this->ctlTest->savedValue2 = $this->ctlTest->savedValue1;  
172:     }
173:     
174:     
175:     public function runTests() {
176:         $cliOptions = [ 'phpunit']; 
177:         array_push($cliOptions, '-c', __QCUBED_CORE__ . '/tests/phpunithtml.xml');  
178: 
179:         
180: 
181:         $tester = new PHPUnit_TextUI_Command();
182: 
183:         $tester->run($cliOptions);
184:     }
185: }
186: 
187: QTestForm::Run('QTestForm', __QCUBED_CORE__ . "/tests/qcubed-unit/QTestForm.tpl.php");