1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4/** 5 * PHP Version 4 6 * 7 * Copyright (c) 2002-2005, Sebastian Bergmann <sb@sebastian-bergmann.de>. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * * Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 17 * * Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in 19 * the documentation and/or other materials provided with the 20 * distribution. 21 * 22 * * Neither the name of Sebastian Bergmann nor the names of his 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 * 39 * @category Testing 40 * @package PHPUnit 41 * @author Scott Mattocks <scott@crisscott.com> 42 * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> 43 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 44 * @version CVS: $Id: Gtk.php,v 1.6 2005/11/10 09:47:15 sebastian Exp $ 45 * @link http://pear.php.net/package/PHPUnit 46 * @since File available since Release 1.2.0 47 */ 48 49if (!function_exists('is_a')) { 50 require_once 'PHP/Compat/Function/is_a.php'; 51} 52 53/** 54 * GTK GUI interface for PHPUnit. 55 * 56 * This class is a PHP port of junit.awtui.testrunner. Documentation 57 * for junit.awtui.testrunner can be found at 58 * http://junit.sourceforge.net 59 * 60 * Due to the limitations of PHP4 and PHP-Gtk, this class can not 61 * duplicate all of the functionality of the JUnit GUI. Some of the 62 * things this class cannot do include: 63 * - Reloading the class for each run 64 * - Stopping the test in progress 65 * 66 * To use simply intantiate the class and call main() 67 * $gtk =& new PHPUnit_GUI_Gtk; 68 * $gtk->main(); 69 * 70 * Once the window has finished loading, you can enter the name of 71 * a class that has been loaded (include/require some where in your 72 * code, or you can pass the name of the file containing the class. 73 * 74 * You can also load classes using the SetupDecorator class. 75 * require_once 'PHPUnit/GUI/SetupDecorator.php'; 76 * require_once 'PHPUnit/GUI/Gtk.php'; 77 * $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_Gtk()); 78 * $gui->getSuitesFromDir('/path/to/test','.*\.php$',array('index.php','sql.php')); 79 * $gui->show(); 80 * 81 * 82 * @category Testing 83 * @package PHPUnit 84 * @author Scott Mattocks <scott@crisscott.com> 85 * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> 86 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 87 * @version Release: 1.3.2 88 * @link http://pear.php.net/package/PHPUnit 89 * @since Class available since Release 1.2.0 90 * @todo Allow file drop. (Gtk_FileDrop) 91 */ 92class PHPUnit_GUI_Gtk { 93 94 /** 95 * The main gtk window 96 * @var object 97 */ 98 var $gui; 99 /** 100 * The text entry that contains the name of the 101 * file that holds the test(s)/suite(s). 102 * @var object 103 */ 104 var $suiteField; 105 /** 106 * The label that shows the number of tests that 107 * were run. 108 * @var object 109 */ 110 var $numberOfRuns; 111 /** 112 * The label that shows the number of errors that 113 * were encountered. 114 * @var object 115 */ 116 var $numberOfErrors; 117 /** 118 * The label that shows the number of failures 119 * that were encountered. 120 * @var object 121 */ 122 var $numberOfFailures; 123 /** 124 * The label for reporting user messages. 125 * @var object 126 */ 127 var $statusLine; 128 /** 129 * The text area for reporting messages from successful 130 * test runs. (not necessarily successful tests) 131 * @var object 132 */ 133 var $reportArea; 134 /** 135 * The text area for reporting errors encountered when 136 * running tests. 137 * @var object 138 */ 139 var $dumpArea; 140 /** 141 * The progress bar indicator. Shows the percentage of 142 * passed tests. 143 * @var object 144 */ 145 var $progress; 146 /** 147 * A checkbox for the user to indicate whether or not they 148 * would like to see results from all tests or just failures. 149 * @object 150 */ 151 var $showPassed; 152 153 /** 154 * Constructor. 155 * 156 * The constructor checks for the gtk extension and loads it 157 * if needed. Then it creates the GUI. The GUI is not shown 158 * nor is the main gtk loop started until main() is called. 159 * 160 * @access public 161 * @param none 162 * @return void 163 */ 164 function PHPUnit_GUI_Gtk() 165 { 166 // Check for php-gtk extension. 167 if (!extension_loaded('gtk')) { 168 dl( 'php_gtk.' . PHP_SHLIB_SUFFIX); 169 } 170 171 // Create the interface but don't start the loop 172 $this->_createUI(); 173 } 174 /** 175 * Start the main gtk loop. 176 * 177 * main() first sets the default state of the showPassed 178 * check box. Next all widgets that are part of the GUI 179 * are shown. Finally the main gtk loop is started. 180 * 181 * @access public 182 * @param boolean $showPassed 183 * @return void 184 */ 185 function main($showPassed = true) 186 { 187 $this->showPassed->set_active($showPassed); 188 $this->gui->show_all(); 189 190 gtk::main(); 191 } 192 /** 193 * Create the user interface. 194 * 195 * The user interface is pretty simple. It consists of a 196 * menu, text entry, run button, some labels, a progress 197 * indicator, and a couple of areas for notification of 198 * any messages. 199 * 200 * @access private 201 * @param none 202 * @return void 203 */ 204 function _createUI() 205 { 206 // Create a window. 207 $window =& new GtkWindow; 208 $window->set_title('PHPUnit Gtk'); 209 $window->set_usize(400, -1); 210 211 // Create the main box. 212 $mainBox =& new GtkVBox; 213 $window->add($mainBox); 214 215 // Start with the menu. 216 $mainBox->pack_start($this->_createMenu()); 217 218 // Then add the suite field entry. 219 $mainBox->pack_start($this->_createSuiteEntry()); 220 221 // Then add the report labels. 222 $mainBox->pack_start($this->_createReportLabels()); 223 224 // Next add the progress bar. 225 $mainBox->pack_start($this->_createProgressBar()); 226 227 // Then add the report area and the dump area. 228 $mainBox->pack_start($this->_createReportAreas()); 229 230 // Finish off with the status line. 231 $mainBox->pack_start($this->_createStatusLine()); 232 233 // Connect the destroy signal. 234 $window->connect_object('destroy', array('gtk', 'main_quit')); 235 236 // Assign the member. 237 $this->gui =& $window; 238 } 239 /** 240 * Create the menu. 241 * 242 * The menu is very simple. It an exit menu item, which exits 243 * the application, and an about menu item, which shows some 244 * basic information about the application itself. 245 * 246 * @access private 247 * @param none 248 * @return &object The GtkMenuBar 249 */ 250 function &_createMenu() 251 { 252 // Create the menu bar. 253 $menuBar =& new GtkMenuBar; 254 255 // Create the main (only) menu item. 256 $phpHeader =& new GtkMenuItem('PHPUnit'); 257 258 // Add the menu item to the menu bar. 259 $menuBar->append($phpHeader); 260 261 // Create the PHPUnit menu. 262 $phpMenu =& new GtkMenu; 263 264 // Add the menu items 265 $about =& new GtkMenuItem('About...'); 266 $about->connect('activate', array(&$this, 'about')); 267 $phpMenu->append($about); 268 269 $exit =& new GtkMenuItem('Exit'); 270 $exit->connect_object('activate', array('gtk', 'main_quit')); 271 $phpMenu->append($exit); 272 273 // Complete the menu. 274 $phpHeader->set_submenu($phpMenu); 275 276 return $menuBar; 277 } 278 /** 279 * Create the suite entry and related widgets. 280 * 281 * The suite entry has some supporting components such as a 282 * label, the show passed check box and the run button. All 283 * of these items are packed into two nested boxes. 284 * 285 * @access private 286 * @param none 287 * @return &object A box that contains all of the suite entry pieces. 288 */ 289 function &_createSuiteEntry() 290 { 291 // Create the outermost box. 292 $outerBox =& new GtkVBox; 293 294 // Create the suite label, box, and field. 295 $suiteLabel =& new GtkLabel('Test class name:'); 296 $suiteBox =& new GtkHBox; 297 $this->suiteField =& new GtkEntry; 298 $this->suiteField->set_text($suiteName != NULL ? $suiteName : ''); 299 300 // Create the button the user will use to start the test. 301 $runButton =& new GtkButton('Run'); 302 $runButton->connect_object('clicked', array(&$this, 'run')); 303 304 // Create the check box that lets the user show only failures. 305 $this->showPassed =& new GtkCheckButton('Show passed tests'); 306 307 // Add the components to their respective boxes. 308 $suiteLabel->set_alignment(0, 0); 309 $outerBox->pack_start($suiteLabel); 310 $outerBox->pack_start($suiteBox); 311 $outerBox->pack_start($this->showPassed); 312 313 $suiteBox->pack_start($this->suiteField); 314 $suiteBox->pack_start($runButton); 315 316 return $outerBox; 317 } 318 319 /** 320 * Create the labels that tell the user what has happened. 321 * 322 * There are three labels, one each for total runs, errors and 323 * failures. There is also one label for each of these that 324 * describes what the label is. It could be done with one label 325 * instead of two but that would make updates much harder. 326 * 327 * @access private 328 * @param none 329 * @return &object A box containing the labels. 330 */ 331 function &_createReportLabels() 332 { 333 // Create a box to hold everything. 334 $labelBox =& new GtkHBox; 335 336 // Create the non-updated labels. 337 $numberOfRuns =& new GtkLabel('Runs:'); 338 $numberOfErrors =& new GtkLabel('Errors:'); 339 $numberOfFailures =& new GtkLabel('Failures:'); 340 341 // Create the labels that will be updated. 342 // These are asssigned to members to make it easier to 343 // set their values later. 344 $this->numberOfRuns =& new GtkLabel(0); 345 $this->numberOfErrors =& new GtkLabel(0); 346 $this->numberOfFailures =& new GtkLabel(0); 347 348 // Pack everything in. 349 $labelBox->pack_start($numberOfRuns); 350 $labelBox->pack_start($this->numberOfRuns); 351 $labelBox->pack_start($numberOfErrors); 352 $labelBox->pack_start($this->numberOfErrors); 353 $labelBox->pack_start($numberOfFailures); 354 $labelBox->pack_start($this->numberOfFailures); 355 356 return $labelBox; 357 } 358 359 /** 360 * Create the success/failure indicator. 361 * 362 * A GtkProgressBar is used to visually indicate how many 363 * tests were successful compared to how many were not. The 364 * progress bar shows the percentage of success and will 365 * change from green to red if there are any failures. 366 * 367 * @access private 368 * @param none 369 * @return &object The progress bar 370 */ 371 function &_createProgressBar() 372 { 373 // Create the progress bar. 374 $this->progress =& new GtkProgressBar(new GtkAdjustment(0, 0, 1, .1, 1, 0)); 375 376 // Set the progress bar to print the percentage. 377 $this->progress->set_show_text(true); 378 379 return $this->progress; 380 } 381 382 /** 383 * Create the report text areas. 384 * 385 * The report area consists of one text area for failures, one 386 * text area for errors and one label for identification purposes. 387 * All three widgets are packed into a box. 388 * 389 * @access private 390 * @param none 391 * @return &object The box containing the report areas. 392 */ 393 function &_createReportAreas() 394 { 395 // Create the containing box. 396 $reportBox =& new GtkVBox; 397 398 // Create the identification label 399 $reportLabel =& new GtkLabel('Errors and Failures:'); 400 $reportLabel->set_alignment(0, 0); 401 402 // Create the scrolled windows for the text areas. 403 $reportScroll =& new GtkScrolledWindow; 404 $dumpScroll =& new GtkScrolledWindow; 405 406 // Make the scroll areas big enough. 407 $reportScroll->set_usize(-1, 150); 408 $dumpScroll->set_usize(-1, 150); 409 410 // Only show the vertical scroll bar when needed. 411 // Never show the horizontal scroll bar. 412 $reportScroll->set_policy(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); 413 $dumpScroll->set_policy(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); 414 415 // Create the text areas. 416 $this->reportArea =& new GtkText; 417 $this->dumpArea =& new GtkText; 418 419 // Don't let words get broken. 420 $this->reportArea->set_word_wrap(true); 421 $this->dumpArea->set_word_wrap(true); 422 423 // Pack everything in. 424 $reportBox->pack_start($reportLabel); 425 $reportScroll->add($this->reportArea); 426 $reportBox->pack_start($reportScroll); 427 $dumpScroll->add($this->dumpArea); 428 $reportBox->pack_start($dumpScroll); 429 430 return $reportBox; 431 } 432 433 /** 434 * Create a status label. 435 * 436 * A status line at the bottom of the application is used 437 * to notify the user of non-test related messages such as 438 * failures loading a test suite. 439 * 440 * @access private 441 * @param none 442 * @return &object The status label. 443 */ 444 function &_createStatusLine() 445 { 446 // Create the status label. 447 $this->statusLine =& new GtkLabel(''); 448 $this->statusLine->set_alignment(0, 0); 449 450 return $this->statusLine; 451 } 452 453 /** 454 * Show a popup with information about the application. 455 * 456 * The popup should show information about the version, 457 * the author, the license, where to get the latest 458 * version and a short description. 459 * 460 * @access public 461 * @param none 462 * @return void 463 */ 464 function about() 465 { 466 // Create the new window. 467 $about =& new GtkWindow; 468 $about->set_title('About PHPUnit GUI Gtk'); 469 $about->set_usize(250, -1); 470 471 // Put two vboxes in the hbox. 472 $vBox =& new GtkVBox; 473 $about->add($vBox); 474 475 // Create the labels. 476 $version =& new GtkLabel(" Version: 1.0"); 477 $license =& new GtkLabel(" License: PHP License v3.0"); 478 $where =& new GtkLabel(" Download from: http://pear.php.net/PHPUnit/"); 479 $unitAuth =& new GtkLabel(" PHPUnit Author: Sebastian Bergman"); 480 $gtkAuth =& new GtkLabel(" Gtk GUI Author: Scott Mattocks"); 481 482 // Align everything to the left 483 $where->set_alignment(0, .5); 484 $version->set_alignment(0, .5); 485 $license->set_alignment(0, .5); 486 $gtkAuth->set_alignment(0, .5); 487 $unitAuth->set_alignment(0, .5); 488 489 // Pack everything into the vBox; 490 $vBox->pack_start($version); 491 $vBox->pack_start($license); 492 $vBox->pack_start($where); 493 $vBox->pack_start($unitAuth); 494 $vBox->pack_start($gtkAuth); 495 496 // Connect the destroy signal. 497 $about->connect('destroy', array('gtk', 'true')); 498 499 // Show the goods. 500 $about->show_all(); 501 } 502 503 /** 504 * Load the test suite. 505 * 506 * This method tries to load test suite based on the user 507 * info. If the user passes the name of a tests suite, it 508 * is instantiated and a new object is returned. If the 509 * user passes a file that contains a test suite, the class 510 * is instantiated and a new object is returned. If the user 511 * passes a file that contains a test case, the test case is 512 * passed to a new test suite and the new suite object is 513 * returned. 514 * 515 * @access public 516 * @param string The file that contains a test case/suite or the classname. 517 * @return &object The new test suite. 518 */ 519 function &loadTest(&$file) 520 { 521 // Check to see if a class name was given. 522 if (is_a($file, 'PHPUnit_TestSuite')) { 523 return $file; 524 } elseif (class_exists($file)) { 525 require_once 'PHPUnit/TestSuite.php'; 526 return new PHPUnit_TestSuite($file); 527 } 528 529 // Check that the file exists. 530 if (!@is_readable($file)) { 531 $this->_showStatus('Cannot find file: ' . $file); 532 return false; 533 } 534 535 $this->_showStatus('Loading test suite...'); 536 537 // Instantiate the class. 538 // If the path is /path/to/test/TestClass.php 539 // the class name should be test_TestClass 540 require_once $file; 541 $className = str_replace(DIRECTORY_SEPARATOR, '_', $file); 542 $className = substr($className, 0, strpos($className, '.')); 543 544 require_once 'PHPUnit/TestSuite.php'; 545 return new PHPUnit_TestSuite($className); 546 } 547 548 /** 549 * Run the test suite. 550 * 551 * This method runs the test suite and updates the messages 552 * for the user. When finished it changes the status line 553 * to 'Test Complete' 554 * 555 * @access public 556 * @param none 557 * @return void 558 */ 559 function runTest() 560 { 561 // Notify the user that the test is running. 562 $this->_showStatus('Running Test...'); 563 564 // Run the test. 565 $result = PHPUnit::run($this->suite); 566 567 // Update the labels. 568 $this->_setLabelValue($this->numberOfRuns, $result->runCount()); 569 $this->_setLabelValue($this->numberOfErrors, $result->errorCount()); 570 $this->_setLabelValue($this->numberOfFailures, $result->failureCount()); 571 572 // Update the progress bar. 573 $this->_updateProgress($result->runCount(), 574 $result->errorCount(), 575 $result->failureCount() 576 ); 577 578 // Show the errors. 579 $this->_showFailures($result->errors(), $this->dumpArea); 580 581 // Show the messages from the tests. 582 if ($this->showPassed->get_active()) { 583 // Show failures and success. 584 $this->_showAll($result, $this->reportArea); 585 } else { 586 // Show only failures. 587 $this->_showFailures($result->failures(), $this->reportArea); 588 } 589 590 // Update the status message. 591 $this->_showStatus('Test complete'); 592 } 593 594 /** 595 * Set the text of a label. 596 * 597 * Change the text of a given label. 598 * 599 * @access private 600 * @param widget &$label The label whose value is to be changed. 601 * @param string $value The new text of the label. 602 * @return void 603 */ 604 function _setLabelValue(&$label, $value) 605 { 606 $label->set_text($value); 607 } 608 609 /** 610 * The main work of the application. 611 * 612 * Load the test suite and then execute the tests. 613 * 614 * @access public 615 * @param none 616 * @return void 617 */ 618 function run() 619 { 620 // Load the test suite. 621 $this->suite =& $this->loadTest($this->suiteField->get_text()); 622 623 // Check to make sure the suite was loaded properly. 624 if (!is_object($this->suite)) { 625 // Raise an error. 626 $this->_showStatus('Could not load test suite.'); 627 return false; 628 } 629 630 // Run the tests. 631 $this->runTest(); 632 } 633 634 /** 635 * Update the status message. 636 * 637 * @access private 638 * @param string $status The new message. 639 * @return void 640 */ 641 function _showStatus($status) 642 { 643 $this->statusLine->set_text($status); 644 } 645 646 /** 647 * Alias for main() 648 * 649 * @see main 650 */ 651 function show($showPassed = true) 652 { 653 $this->main($showPassed); 654 } 655 656 /** 657 * Add a suite to the tests. 658 * 659 * This method is require by SetupDecorator. It adds a 660 * suite to the the current set of suites. 661 * 662 * @access public 663 * @param object $testSuite The suite to add. 664 * @return void 665 */ 666 function addSuites($testSuite) 667 { 668 if (!is_array($testSuite)) { 669 settype($testSuite, 'array'); 670 } 671 672 foreach ($testSuite as $suite) { 673 674 if (is_a($this->suite, 'PHPUnit_TestSuite')) { 675 $this->suite->addTestSuite($suite->getName()); 676 } else { 677 $this->suite =& $this->loadTest($suite); 678 } 679 680 // Set the suite field. 681 $text = $this->suiteField->get_text(); 682 if (empty($text)) { 683 $this->suiteField->set_text($this->suite->getName()); 684 } 685 } 686 } 687 688 /** 689 * Show all test messages. 690 * 691 * @access private 692 * @param object The TestResult from the test suite. 693 * @return void 694 */ 695 function _showAll(&$result) 696 { 697 // Clear the area first. 698 $this->reportArea->delete_text(0, -1); 699 $this->reportArea->insert_text($result->toString(), 0); 700 } 701 702 /** 703 * Show failure/error messages in the given text area. 704 * 705 * @access private 706 * @param object &$results The results of the test. 707 * @param widget &$area The area to show the results in. 708 */ 709 function _showFailures(&$results, &$area) 710 { 711 $area->delete_text(0, -1); 712 foreach (array_reverse($results, true) as $result) { 713 $area->insert_text($result->toString(), 0); 714 } 715 } 716 717 /** 718 * Update the progress indicator. 719 * 720 * @access private 721 * @param integer $runs 722 * @param integer $errors 723 * @param integer $failures 724 * @return void 725 */ 726 function _updateProgress($runs, $errors, $failures) 727 { 728 $percentage = 1 - (($errors + $failures) / $runs); 729 $this->progress->set_percentage($percentage); 730 } 731} 732 733/* 734 * Local variables: 735 * tab-width: 4 736 * c-basic-offset: 4 737 * c-hanging-comment-ender-p: nil 738 * End: 739 */ 740?> 741