Source for file Gtk.php
Documentation is available at Gtk.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Scott Mattocks |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Scott Mattocks <scottmattocks@php.net> |
// +----------------------------------------------------------------------+
// $Id: Gtk.php,v 1.2 2004/11/25 09:06:55 sebastian Exp $
* GTK GUI interface for PHPUnit.
* This class is a PHP port of junit.awtui.testrunner. Documentation
* for junit.awtui.testrunner can be found at
* http://junit.sourceforge.net
* Due to the limitations of PHP4 and PHP-Gtk, this class can not
* duplicate all of the functionality of the JUnit GUI. Some of the
* things this class cannot do include:
* - Reloading the class for each run
* - Stopping the test in progress
* To use simply intantiate the class and call main()
* $gtk =& new PHPUnit_GUI_Gtk;
* Once the window has finished loading, you can enter the name of
* a class that has been loaded (include/require some where in your
* code, or you can pass the name of the file containing the class.
* You can also load classes using the SetupDecorator class.
* require_once 'PHPUnit/GUI/SetupDecorator.php';
* require_once 'PHPUnit/GUI/Gtk.php';
* $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_Gtk());
* $gui->getSuitesFromDir('/path/to/test','.*\.php$',array('index.php','sql.php'));
* @todo Allow file drop. (Gtk_FileDrop)
* @copyright Copyright © 2004 Scott Mattocks
* The text entry that contains the name of the
* file that holds the test(s)/suite(s).
* The label that shows the number of tests that
* The label that shows the number of errors that
* The label that shows the number of failures
* The label for reporting user messages.
* The text area for reporting messages from successful
* test runs. (not necessarily successful tests)
* The text area for reporting errors encountered when
* The progress bar indicator. Shows the percentage of
* A checkbox for the user to indicate whether or not they
* would like to see results from all tests or just failures.
* The constructor checks for the gtk extension and loads it
* if needed. Then it creates the GUI. The GUI is not shown
* nor is the main gtk loop started until main() is called.
// Check for php-gtk extension.
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX );
// Create the interface but don't start the loop
* Start the main gtk loop.
* main() first sets the default state of the showPassed
* check box. Next all widgets that are part of the GUI
* are shown. Finally the main gtk loop is started.
* @param boolean $showPassed
function main($showPassed = true )
* Create the user interface.
* The user interface is pretty simple. It consists of a
* menu, text entry, run button, some labels, a progress
* indicator, and a couple of areas for notification of
$window = & new GtkWindow;
$window->set_title ('PHPUnit Gtk');
$window->set_usize (400 , -1 );
$mainBox->pack_start ($this->_createMenu ());
// Then add the suite field entry.
$mainBox->pack_start ($this->_createSuiteEntry ());
// Then add the report labels.
$mainBox->pack_start ($this->_createReportLabels ());
// Next add the progress bar.
$mainBox->pack_start ($this->_createProgressBar ());
// Then add the report area and the dump area.
$mainBox->pack_start ($this->_createReportAreas ());
// Finish off with the status line.
$mainBox->pack_start ($this->_createStatusLine ());
// Connect the destroy signal.
$window->connect_object ('destroy', array ('gtk', 'main_quit'));
* The menu is very simple. It an exit menu item, which exits
* the application, and an about menu item, which shows some
* basic information about the application itself.
* @return &object The GtkMenuBar
$menuBar = & new GtkMenuBar;
// Create the main (only) menu item.
$phpHeader = & new GtkMenuItem ('PHPUnit');
// Add the menu item to the menu bar.
$menuBar->append ($phpHeader);
// Create the PHPUnit menu.
$about = & new GtkMenuItem ('About...');
$about->connect ('activate', array (&$this, 'about'));
$phpMenu->append ($about);
$exit = & new GtkMenuItem ('Exit');
$exit->connect_object ('activate', array ('gtk', 'main_quit'));
$phpHeader->set_submenu ($phpMenu);
* Create the suite entry and related widgets.
* The suite entry has some supporting components such as a
* label, the show passed check box and the run button. All
* of these items are packed into two nested boxes.
* @return &object A box that contains all of the suite entry pieces.
function &_createSuiteEntry ()
// Create the outermost box.
$outerBox = & new GtkVBox;
// Create the suite label, box, and field.
$suiteLabel = & new GtkLabel ('Test class name:');
$suiteBox = & new GtkHBox;
$this->suiteField->set_text ($suiteName != NULL ? $suiteName : '');
// Create the button the user will use to start the test.
$runButton = & new GtkButton ('Run');
$runButton->connect_object ('clicked', array (&$this, 'run'));
// Create the check box that lets the user show only failures.
$this->showPassed = & new GtkCheckButton ('Show passed tests');
// Add the components to their respective boxes.
$suiteLabel->set_alignment (0 , 0 );
$outerBox->pack_start ($suiteLabel);
$outerBox->pack_start ($suiteBox);
$suiteBox->pack_start ($runButton);
* Create the labels that tell the user what has happened.
* There are three labels, one each for total runs, errors and
* failures. There is also one label for each of these that
* describes what the label is. It could be done with one label
* instead of two but that would make updates much harder.
* @return &object A box containing the labels.
function &_createReportLabels ()
// Create a box to hold everything.
$labelBox = & new GtkHBox;
// Create the non-updated labels.
$numberOfRuns = & new GtkLabel ('Runs:');
$numberOfErrors = & new GtkLabel ('Errors:');
$numberOfFailures = & new GtkLabel ('Failures:');
// Create the labels that will be updated.
// These are asssigned to members to make it easier to
// set their values later.
$labelBox->pack_start ($numberOfRuns);
$labelBox->pack_start ($numberOfErrors);
$labelBox->pack_start ($numberOfFailures);
* Create the success/failure indicator.
* A GtkProgressBar is used to visually indicate how many
* tests were successful compared to how many were not. The
* progress bar shows the percentage of success and will
* change from green to red if there are any failures.
* @return &object The progress bar
function &_createProgressBar ()
// Create the progress bar.
$this->progress = & new GtkProgressBar (new GtkAdjustment (0 , 0 , 1 , .1 , 1 , 0 ));
// Set the progress bar to print the percentage.
* Create the report text areas.
* The report area consists of one text area for failures, one
* text area for errors and one label for identification purposes.
* All three widgets are packed into a box.
* @return &object The box containing the report areas.
function &_createReportAreas ()
// Create the containing box.
$reportBox = & new GtkVBox;
// Create the identification label
$reportLabel = & new GtkLabel ('Errors and Failures:');
$reportLabel->set_alignment (0 , 0 );
// Create the scrolled windows for the text areas.
$reportScroll = & new GtkScrolledWindow;
$dumpScroll = & new GtkScrolledWindow;
// Make the scroll areas big enough.
$reportScroll->set_usize (-1 , 150 );
$dumpScroll->set_usize (-1 , 150 );
// Only show the vertical scroll bar when needed.
// Never show the horizontal scroll bar.
$reportScroll->set_policy (GTK_POLICY_NEVER , GTK_POLICY_AUTOMATIC );
$dumpScroll->set_policy (GTK_POLICY_NEVER , GTK_POLICY_AUTOMATIC );
// Create the text areas.
// Don't let words get broken.
$reportBox->pack_start ($reportLabel);
$reportBox->pack_start ($reportScroll);
$reportBox->pack_start ($dumpScroll);
* A status line at the bottom of the application is used
* to notify the user of non-test related messages such as
* failures loading a test suite.
* @return &object The status label.
function &_createStatusLine ()
// Create the status label.
* Show a popup with information about the application.
* The popup should show information about the version,
* the author, the license, where to get the latest
* version and a short description.
// Create the new window.
$about->set_title ('About PHPUnit GUI Gtk');
$about->set_usize (250 , -1 );
// Put two vboxes in the hbox.
$version = & new GtkLabel (" Version: 1.0");
$license = & new GtkLabel (" License: PHP License v3.0");
$where = & new GtkLabel (" Download from: http://pear.php.net/PHPUnit/");
$unitAuth = & new GtkLabel (" PHPUnit Author: Sebastian Bergman");
$gtkAuth = & new GtkLabel (" Gtk GUI Author: Scott Mattocks");
// Align everything to the left
$where->set_alignment (0 , .5 );
$version->set_alignment (0 , .5 );
$license->set_alignment (0 , .5 );
$gtkAuth->set_alignment (0 , .5 );
$unitAuth->set_alignment (0 , .5 );
// Pack everything into the vBox;
$vBox->pack_start ($version);
$vBox->pack_start ($license);
$vBox->pack_start ($where);
$vBox->pack_start ($unitAuth);
$vBox->pack_start ($gtkAuth);
// Connect the destroy signal.
$about->connect ('destroy', array ('gtk', 'true'));
* This method tries to load test suite based on the user
* info. If the user passes the name of a tests suite, it
* is instantiated and a new object is returned. If the
* user passes a file that contains a test suite, the class
* is instantiated and a new object is returned. If the user
* passes a file that contains a test case, the test case is
* passed to a new test suite and the new suite object is
* @param string The file that contains a test case/suite or the classname.
* @return &object The new test suite.
// Check to see if a class name was given.
if (is_a($file, 'PHPUnit_TestSuite')) {
require_once 'PHPUnit/TestSuite.php';
// Check that the file exists.
$this->_showStatus ('Cannot find file: ' . $file);
$this->_showStatus ('Loading test suite...');
// Instantiate the class.
// If the path is /path/to/test/TestClass.php
// the class name should be test_TestClass
$className = str_replace(DIRECTORY_SEPARATOR , '_', $file);
$className = substr($className, 0 , strpos($className, '.'));
require_once 'PHPUnit/TestSuite.php';
* This method runs the test suite and updates the messages
* for the user. When finished it changes the status line
// Notify the user that the test is running.
$this->_showStatus ('Running Test...');
$this->_setLabelValue ($this->numberOfRuns, $result->runCount ());
// Update the progress bar.
$this->_updateProgress ($result->runCount (),
$this->_showFailures ($result->errors (), $this->dumpArea);
// Show the messages from the tests.
// Show failures and success.
$this->_showFailures ($result->failures (), $this->reportArea);
// Update the status message.
$this->_showStatus ('Test complete');
* Set the text of a label.
* Change the text of a given label.
* @param widget &$label The label whose value is to be changed.
* @param string $value The new text of the label.
function _setLabelValue (&$label, $value)
$label->set_text ($value);
* The main work of the application.
* Load the test suite and then execute the tests.
// Check to make sure the suite was loaded properly.
$this->_showStatus ('Could not load test suite.');
* Update the status message.
* @param string $status The new message.
function _showStatus ($status)
function show($showPassed = true )
$this->main($showPassed);
* Add a suite to the tests.
* This method is require by SetupDecorator. It adds a
* suite to the the current set of suites.
* @param object $testSuite The suite to add.
foreach ($testSuite as $suite) {
if (is_a($this->suite, 'PHPUnit_TestSuite')) {
$this->suite->addTestSuite ($suite->getName ());
$this->suite = & $this->loadTest($suite);
$this->suiteField->set_text ($this->suite->getName ());
* Show all test messages.
* @param object The TestResult from the test suite.
function _showAll (&$result)
$this->reportArea->insert_text ($result->toString (), 0 );
* Show failure/error messages in the given text area.
* @param object &$results The results of the test.
* @param widget &$area The area to show the results in.
function _showFailures (&$results, &$area)
$area->delete_text (0 , -1 );
$area->insert_text ($result->toString (), 0 );
* Update the progress indicator.
* @param integer $failures
function _updateProgress ($runs, $errors, $failures)
$percentage = 1 - (($errors + $failures) / $runs);
$this->progress->set_percentage ($percentage);
Documentation generated on Mon, 11 Mar 2019 14:22:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|