00001 <?php
00002
00003
00049 if (!function_exists('is_a')) {
00050 lt_include('PHP/Compat/Function/is_a.php');
00051 }
00052
00092 class PHPUnit_GUI_Gtk {
00093
00098 var $gui;
00104 var $suiteField;
00110 var $numberOfRuns;
00116 var $numberOfErrors;
00122 var $numberOfFailures;
00127 var $statusLine;
00133 var $reportArea;
00139 var $dumpArea;
00145 var $progress;
00151 var $showPassed;
00152
00164 function PHPUnit_GUI_Gtk()
00165 {
00166
00167 if (!extension_loaded('gtk')) {
00168 dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
00169 }
00170
00171
00172 $this->_createUI();
00173 }
00185 function main($showPassed = true)
00186 {
00187 $this->showPassed->set_active($showPassed);
00188 $this->gui->show_all();
00189
00190 gtk::main();
00191 }
00204 function _createUI()
00205 {
00206
00207 $window =& new GtkWindow;
00208 $window->set_title('PHPUnit Gtk');
00209 $window->set_usize(400, -1);
00210
00211
00212 $mainBox =& new GtkVBox;
00213 $window->add($mainBox);
00214
00215
00216 $mainBox->pack_start($this->_createMenu());
00217
00218
00219 $mainBox->pack_start($this->_createSuiteEntry());
00220
00221
00222 $mainBox->pack_start($this->_createReportLabels());
00223
00224
00225 $mainBox->pack_start($this->_createProgressBar());
00226
00227
00228 $mainBox->pack_start($this->_createReportAreas());
00229
00230
00231 $mainBox->pack_start($this->_createStatusLine());
00232
00233
00234 $window->connect_object('destroy', array('gtk', 'main_quit'));
00235
00236
00237 $this->gui =& $window;
00238 }
00250 function &_createMenu()
00251 {
00252
00253 $menuBar =& new GtkMenuBar;
00254
00255
00256 $phpHeader =& new GtkMenuItem('PHPUnit');
00257
00258
00259 $menuBar->append($phpHeader);
00260
00261
00262 $phpMenu =& new GtkMenu;
00263
00264
00265 $about =& new GtkMenuItem('About...');
00266 $about->connect('activate', array(&$this, 'about'));
00267 $phpMenu->append($about);
00268
00269 $exit =& new GtkMenuItem('Exit');
00270 $exit->connect_object('activate', array('gtk', 'main_quit'));
00271 $phpMenu->append($exit);
00272
00273
00274 $phpHeader->set_submenu($phpMenu);
00275
00276 return $menuBar;
00277 }
00289 function &_createSuiteEntry()
00290 {
00291
00292 $outerBox =& new GtkVBox;
00293
00294
00295 $suiteLabel =& new GtkLabel('Test class name:');
00296 $suiteBox =& new GtkHBox;
00297 $this->suiteField =& new GtkEntry;
00298 $this->suiteField->set_text($suiteName != NULL ? $suiteName : '');
00299
00300
00301 $runButton =& new GtkButton('Run');
00302 $runButton->connect_object('clicked', array(&$this, 'run'));
00303
00304
00305 $this->showPassed =& new GtkCheckButton('Show passed tests');
00306
00307
00308 $suiteLabel->set_alignment(0, 0);
00309 $outerBox->pack_start($suiteLabel);
00310 $outerBox->pack_start($suiteBox);
00311 $outerBox->pack_start($this->showPassed);
00312
00313 $suiteBox->pack_start($this->suiteField);
00314 $suiteBox->pack_start($runButton);
00315
00316 return $outerBox;
00317 }
00318
00331 function &_createReportLabels()
00332 {
00333
00334 $labelBox =& new GtkHBox;
00335
00336
00337 $numberOfRuns =& new GtkLabel('Runs:');
00338 $numberOfErrors =& new GtkLabel('Errors:');
00339 $numberOfFailures =& new GtkLabel('Failures:');
00340
00341
00342
00343
00344 $this->numberOfRuns =& new GtkLabel(0);
00345 $this->numberOfErrors =& new GtkLabel(0);
00346 $this->numberOfFailures =& new GtkLabel(0);
00347
00348
00349 $labelBox->pack_start($numberOfRuns);
00350 $labelBox->pack_start($this->numberOfRuns);
00351 $labelBox->pack_start($numberOfErrors);
00352 $labelBox->pack_start($this->numberOfErrors);
00353 $labelBox->pack_start($numberOfFailures);
00354 $labelBox->pack_start($this->numberOfFailures);
00355
00356 return $labelBox;
00357 }
00358
00371 function &_createProgressBar()
00372 {
00373
00374 $this->progress =& new GtkProgressBar(new GtkAdjustment(0, 0, 1, .1, 1, 0));
00375
00376
00377 $this->progress->set_show_text(true);
00378
00379 return $this->progress;
00380 }
00381
00393 function &_createReportAreas()
00394 {
00395
00396 $reportBox =& new GtkVBox;
00397
00398
00399 $reportLabel =& new GtkLabel('Errors and Failures:');
00400 $reportLabel->set_alignment(0, 0);
00401
00402
00403 $reportScroll =& new GtkScrolledWindow;
00404 $dumpScroll =& new GtkScrolledWindow;
00405
00406
00407 $reportScroll->set_usize(-1, 150);
00408 $dumpScroll->set_usize(-1, 150);
00409
00410
00411
00412 $reportScroll->set_policy(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
00413 $dumpScroll->set_policy(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
00414
00415
00416 $this->reportArea =& new GtkText;
00417 $this->dumpArea =& new GtkText;
00418
00419
00420 $this->reportArea->set_word_wrap(true);
00421 $this->dumpArea->set_word_wrap(true);
00422
00423
00424 $reportBox->pack_start($reportLabel);
00425 $reportScroll->add($this->reportArea);
00426 $reportBox->pack_start($reportScroll);
00427 $dumpScroll->add($this->dumpArea);
00428 $reportBox->pack_start($dumpScroll);
00429
00430 return $reportBox;
00431 }
00432
00444 function &_createStatusLine()
00445 {
00446
00447 $this->statusLine =& new GtkLabel('');
00448 $this->statusLine->set_alignment(0, 0);
00449
00450 return $this->statusLine;
00451 }
00452
00464 function about()
00465 {
00466
00467 $about =& new GtkWindow;
00468 $about->set_title('About PHPUnit GUI Gtk');
00469 $about->set_usize(250, -1);
00470
00471
00472 $vBox =& new GtkVBox;
00473 $about->add($vBox);
00474
00475
00476 $version =& new GtkLabel(" Version: 1.0");
00477 $license =& new GtkLabel(" License: PHP License v3.0");
00478 $where =& new GtkLabel(" Download from: http://pear.php.net/PHPUnit/");
00479 $unitAuth =& new GtkLabel(" PHPUnit Author: Sebastian Bergman");
00480 $gtkAuth =& new GtkLabel(" Gtk GUI Author: Scott Mattocks");
00481
00482
00483 $where->set_alignment(0, .5);
00484 $version->set_alignment(0, .5);
00485 $license->set_alignment(0, .5);
00486 $gtkAuth->set_alignment(0, .5);
00487 $unitAuth->set_alignment(0, .5);
00488
00489
00490 $vBox->pack_start($version);
00491 $vBox->pack_start($license);
00492 $vBox->pack_start($where);
00493 $vBox->pack_start($unitAuth);
00494 $vBox->pack_start($gtkAuth);
00495
00496
00497 $about->connect('destroy', array('gtk', 'true'));
00498
00499
00500 $about->show_all();
00501 }
00502
00519 function &loadTest(&$file)
00520 {
00521
00522 if (is_a($file, 'PHPUnit_TestSuite')) {
00523 return $file;
00524 } elseif (class_exists($file)) {
00525 lt_include('PHPUnit/TestSuite.php');
00526 return new PHPUnit_TestSuite($file);
00527 }
00528
00529
00530 if (!@is_readable($file)) {
00531 $this->_showStatus('Cannot find file: ' . $file);
00532 return false;
00533 }
00534
00535 $this->_showStatus('Loading test suite...');
00536
00537
00538
00539
00540 lt_include($file);
00541 $className = str_replace(DIRECTORY_SEPARATOR, '_', $file);
00542 $className = substr($className, 0, strpos($className, '.'));
00543
00544 lt_include('PHPUnit/TestSuite.php');
00545 return new PHPUnit_TestSuite($className);
00546 }
00547
00559 function runTest()
00560 {
00561
00562 $this->_showStatus('Running Test...');
00563
00564
00565 $result = PHPUnit::run($this->suite);
00566
00567
00568 $this->_setLabelValue($this->numberOfRuns, $result->runCount());
00569 $this->_setLabelValue($this->numberOfErrors, $result->errorCount());
00570 $this->_setLabelValue($this->numberOfFailures, $result->failureCount());
00571
00572
00573 $this->_updateProgress($result->runCount(),
00574 $result->errorCount(),
00575 $result->failureCount()
00576 );
00577
00578
00579 $this->_showFailures($result->errors(), $this->dumpArea);
00580
00581
00582 if ($this->showPassed->get_active()) {
00583
00584 $this->_showAll($result, $this->reportArea);
00585 } else {
00586
00587 $this->_showFailures($result->failures(), $this->reportArea);
00588 }
00589
00590
00591 $this->_showStatus('Test complete');
00592 }
00593
00604 function _setLabelValue(&$label, $value)
00605 {
00606 $label->set_text($value);
00607 }
00608
00618 function run()
00619 {
00620
00621 $this->suite =& $this->loadTest($this->suiteField->get_text());
00622
00623
00624 if (!is_object($this->suite)) {
00625
00626 $this->_showStatus('Could not load test suite.');
00627 return false;
00628 }
00629
00630
00631 $this->runTest();
00632 }
00633
00641 function _showStatus($status)
00642 {
00643 $this->statusLine->set_text($status);
00644 }
00645
00651 function show($showPassed = true)
00652 {
00653 $this->main($showPassed);
00654 }
00655
00666 function addSuites($testSuite)
00667 {
00668 if (!is_array($testSuite)) {
00669 settype($testSuite, 'array');
00670 }
00671
00672 foreach ($testSuite as $suite) {
00673
00674 if (is_a($this->suite, 'PHPUnit_TestSuite')) {
00675 $this->suite->addTestSuite($suite->getName());
00676 } else {
00677 $this->suite =& $this->loadTest($suite);
00678 }
00679
00680
00681 $text = $this->suiteField->get_text();
00682 if (empty($text)) {
00683 $this->suiteField->set_text($this->suite->getName());
00684 }
00685 }
00686 }
00687
00695 function _showAll(&$result)
00696 {
00697
00698 $this->reportArea->delete_text(0, -1);
00699 $this->reportArea->insert_text($result->toString(), 0);
00700 }
00701
00709 function _showFailures(&$results, &$area)
00710 {
00711 $area->delete_text(0, -1);
00712 foreach (array_reverse($results, true) as $result) {
00713 $area->insert_text($result->toString(), 0);
00714 }
00715 }
00716
00726 function _updateProgress($runs, $errors, $failures)
00727 {
00728 $percentage = 1 - (($errors + $failures) / $runs);
00729 $this->progress->set_percentage($percentage);
00730 }
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740 ?>