r109763 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109762‎ | r109763 | r109764 >
Date:21:47, 22 January 2012
Author:jpostlethwaite
Status:ok
Tags:unittest 
Comment:
Adding UnitTest Extension. See r109762.
Modified paths:
  • /trunk/extensions/UnitTest/Debug.constants.php (added) (history)
  • /trunk/extensions/UnitTest/Debug.php (added) (history)
  • /trunk/extensions/UnitTest/ExtensionsSeleniumSlideshow.php (added) (history)
  • /trunk/extensions/UnitTest/ExtensionsSeleniumSlideshowTemplate.php (added) (history)
  • /trunk/extensions/UnitTest/ExtensionsSeleniumTestCase.php (added) (history)
  • /trunk/extensions/UnitTest/ExtensionsTestCase.php (added) (history)
  • /trunk/extensions/UnitTest/resources (added) (history)
  • /trunk/extensions/UnitTest/special (added) (history)
  • /trunk/extensions/UnitTest/tools (added) (history)

Diff [purge]

Index: trunk/extensions/UnitTest/ExtensionsTestCase.php
@@ -0,0 +1,67 @@
 2+<?php
 3+/**
 4+ * Wikimedia Foundation
 5+ *
 6+ * LICENSE
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 19+ */
 20+
 21+/**
 22+ * ExtensionsTestCase
 23+ *
 24+ * Abstract PHPUnit testing class
 25+ *
 26+ * Extend this class for any extension.
 27+ */
 28+abstract class ExtensionsTestCase extends PHPUnit_Framework_TestCase
 29+{
 30+
 31+ /**
 32+ * getData
 33+ *
 34+ * This is used to generate posted form data
 35+ *
 36+ * Everything should be returned as strings, in the array, since that is how
 37+ * they will be sent by the form.
 38+ *
 39+ * Anything set in $data will be converted to a string.
 40+ *
 41+ * If a value is null, in $data, it will be removed from $return.
 42+ *
 43+ * @param array $return This is a reference to return.
 44+ * @param array $data Anything set in this array will be returned.
 45+ *
 46+ * @return array
 47+ */
 48+ public function getData( &$return, $data = array() ) {
 49+
 50+ if ( is_array( $data ) ) {
 51+
 52+ foreach( $data as $key => $value ) {
 53+
 54+ // Remove values from return if $value is null.
 55+ if ( is_null( $value ) ) {
 56+
 57+ if ( isset( $return[ $key ] ) ) {
 58+
 59+ unset( $return[ $key ] );
 60+ }
 61+ }
 62+ else {
 63+ $return[ $key ] = (string) $value;
 64+ }
 65+ }
 66+ }
 67+ }
 68+}
Property changes on: trunk/extensions/UnitTest/ExtensionsTestCase.php
___________________________________________________________________
Added: svn:eol-style
169 + native
Added: svn:mime-type
270 + text/plain
Added: svn:keywords
371 + Author Date HeadURL Header Id Revision
Index: trunk/extensions/UnitTest/ExtensionsSeleniumTestCase.php
@@ -0,0 +1,748 @@
 2+<?php
 3+/**
 4+ * Wikimedia Foundation
 5+ *
 6+ * LICENSE
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 19+ */
 20+
 21+/**
 22+ * ExtensionsSeleniumTestCase
 23+ *
 24+ * Abstract Selenium testing class
 25+ *
 26+ * Extend this class for any extension.
 27+ */
 28+abstract class ExtensionsSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase
 29+{
 30+
 31+ /**
 32+ * Options loaded from the configuration file
 33+ *
 34+ * @param array $_options
 35+ */
 36+ protected static $_options = array();
 37+
 38+ /**
 39+ * Capture a screenshot on test failure. Screenshots may take a picture of
 40+ * your desktop and not just your browser.
 41+ *
 42+ * @param boolean $captureScreenshotOnFailure
 43+ */
 44+ protected $captureScreenshotOnFailure = true;
 45+
 46+ /**
 47+ * Capture screenshots for a slideshow
 48+ *
 49+ * @param boolean $captureScreenshotsForSlideshow
 50+ */
 51+ protected $captureScreenshotsForSlideshow = true;
 52+
 53+ /**
 54+ * Capture screenshots for a slideshow
 55+ *
 56+ * @param boolean $captureScreenshotsAfterAssertion
 57+ */
 58+ protected $captureScreenshotsAfterAssertion = true;
 59+
 60+ /**
 61+ * The extension name
 62+ *
 63+ * @param string $extensionName
 64+ */
 65+ protected $extensionName = '';
 66+
 67+ /**
 68+ * The default url to the mediawiki virtual host
 69+ *
 70+ * @param string $extensionsBrowsersUrl
 71+ */
 72+ protected $extensionsBrowsersUrl = 'http://localhost/';
 73+
 74+ /**
 75+ * The hostname for the mediawiki virtual host
 76+ *
 77+ * @param string $extensionsHostname
 78+ */
 79+ protected $extensionsHostname = 'localhost';
 80+
 81+ /**
 82+ * The extension test case stamp is based on the date and time. This is used
 83+ *
 84+ * @param string $extensionsTestCaseStamp
 85+ */
 86+ protected $extensionsTestCaseStamp = '';
 87+
 88+ /**
 89+ * The directory to save Selenese generated by Selenium IDE which you are
 90+ * able to run from Firefox. This is an absolute path like:
 91+ *
 92+ * /www/sites/localhost/wikimedia-commit.localhost.wikimedia.org/extensions/CentralNotice/tests/selenium/selenese
 93+ *
 94+ * This will be populated in class instantiation.
 95+ *
 96+ * @param string $extensionsSeleneseDirectory
 97+ */
 98+ protected $extensionsSeleneseDirectory = '';
 99+
 100+ /**
 101+ * The Selenese files to load for testing
 102+ *
 103+ * @param array $seleneseFiles
 104+ */
 105+ protected $seleneseFiles = array();
 106+
 107+ /**
 108+ * This will be true if Selenese files have been loaded.
 109+ *
 110+ * @param boolean $hasSelenese
 111+ */
 112+ protected $hasSelenese = false;
 113+
 114+ /**
 115+ * If this is true, then selenese tests that have already been converted
 116+ * from scripts into test methods.
 117+ *
 118+ * @param boolean $runConvertedTests
 119+ */
 120+ protected $runConvertedTests = true;
 121+
 122+ /**
 123+ * If set to true, selenese files will be loaded.
 124+ *
 125+ * @param boolean $loadSeleneseFiles
 126+ */
 127+ protected $loadSeleneseFiles = true;
 128+
 129+ /**
 130+ * The selenese extensions permitted for testing.
 131+ * Currently, we only use htm.
 132+ *
 133+ * @param array $selenesExtensions
 134+ */
 135+ protected $selenesExtensions = array(
 136+ '.htm',
 137+ );
 138+
 139+ /**
 140+ * The screenshot count. Images captured with @see $this->browserScreenshot
 141+ * will be placed in the screenshot directory @see $this->screenshotPath.
 142+ * Each image will be a png and will the file will be prefixed with this
 143+ * variable. This will make it easier to generate slideshows for test
 144+ * replaying.
 145+ *
 146+ * This will be populated in class instantiation.
 147+ *
 148+ * @param integer $screenshotCount
 149+ */
 150+ protected $screenshotCount = 0;
 151+
 152+ /**
 153+ * The directory to save screenshots for your extension. This is an absolute
 154+ * path like:
 155+ *
 156+ * /www/sites/localhost/wikimedia-commit.localhost.wikimedia.org/extensions/CentralNotice/tests/selenium/screenshots
 157+ *
 158+ * This will be populated in class instantiation.
 159+ *
 160+ * @param string $screenshotPath
 161+ */
 162+ protected $screenshotPath = '';
 163+
 164+ /**
 165+ * The url to the directory where your screenshots are saved for your
 166+ * extension. This is a URL like:
 167+ *
 168+ * http://wikimedia-commit.localhost.wikimedia.org/extensions/CentralNotice/tests/selenium/screenshots/
 169+ *
 170+ * This will be populated in class instantiation.
 171+ *
 172+ * @param string $screenshotUrl
 173+ */
 174+ protected $screenshotUrl = '';
 175+
 176+ /**
 177+ * The test case methods
 178+ *
 179+ * @param array $testCaseMethods
 180+ */
 181+ protected $testCaseMethods = array();
 182+
 183+ /**
 184+ * The base path for the extension tests. This is an absolute path like:
 185+ *
 186+ * /www/sites/localhost/wikimedia-commit.localhost.wikimedia.org/extensions/CentralNotice/tests
 187+ *
 188+ * This will be populated in class instantiation.
 189+ *
 190+ * @param string $unitTestBasePath
 191+ */
 192+ protected $unitTestBasePath = '';
 193+
 194+ /**
 195+ * The webroot for the site. This should correspond to $IP.
 196+ *
 197+ * /www/sites/localhost/wikimedia-commit.localhost.wikimedia.org/
 198+ *
 199+ * This will be populated in class instantiation.
 200+ *
 201+ * @param string $webroot
 202+ */
 203+ protected $webroot = '';
 204+
 205+ /**
 206+ * Test case instantiation
 207+ */
 208+
 209+ /**
 210+ * @param string $name
 211+ * @param array $data
 212+ * @param string $dataName
 213+ * @param array $browser
 214+ * @throws InvalidArgumentException
 215+ */
 216+ final public function __construct( $name = NULL, array $data = array(), $dataName = '', array $browser = array() )
 217+ {
 218+ parent::__construct( $name, $data, $dataName, $browser );
 219+
 220+ $this->init();
 221+
 222+ $this->loadSeleneseFiles();
 223+ // $this->verifySetup();
 224+ // SpecialCentralNoticeTestCase::$seleneseDirectory = $this->unitTestBasePath . '/selenium/selenese';
 225+ }
 226+
 227+ /**
 228+ * Initialize the Test case settings.
 229+ * Set up paths for testing
 230+ */
 231+ final protected function init()
 232+ {
 233+ $this->setUnitTestBasePath();
 234+
 235+ $this->setExtensionName();
 236+
 237+ $this->extensionsTestCaseStamp = date( 'Y-m-d-Hi-s' );
 238+ // Debug::dump($this->extensionsTestCaseStamp, eval(DUMP) . '\$this->extensionsTestCaseStamp', false);
 239+
 240+ if ( empty( $this->unitTestBasePath ) ) {
 241+ $message = 'You must specify a path for $this->unitTestBasePath by properly implementing $this->setUnitTestBasePath()';
 242+ throw new Exception( $message );
 243+ }
 244+
 245+ $this->loadConfiguration();
 246+ }
 247+
 248+ /**
 249+ * getExtensionName
 250+ *
 251+ * @see getExtensionName
 252+ */
 253+ public function getExtensionName()
 254+ {
 255+ return $this->extensionName;
 256+ }
 257+
 258+ /**
 259+ * setExtensionName
 260+ *
 261+ * Set the extension name
 262+ *
 263+ * @see $extensionName
 264+ */
 265+ protected function setExtensionName()
 266+ {
 267+ $this->extensionName = basename( dirname( $this->unitTestBasePath ) );
 268+ }
 269+
 270+ /**
 271+ * Load configuration file
 272+ */
 273+ public function loadConfiguration()
 274+ {
 275+ // Check for customized configuration file
 276+ $file = $this->unitTestBasePath . '/selenium.ini';
 277+
 278+ //Debug::puke( $file, eval(DUMP) . "\$file" );
 279+
 280+ if ( !is_file( $file ) ) {
 281+ // Check for distributed configuration file
 282+ $file .= '.dst';
 283+
 284+ // Debug::dump($file, eval(DUMP) . '\$file', false);
 285+
 286+ if ( !is_file( $file ) ) {
 287+ // Check for distributed configuration file
 288+ $file = false;
 289+ }
 290+ }
 291+
 292+ //Debug::puke( $file, eval(DUMP) . "\$file" );
 293+
 294+ // Load a configuration file if it exists.
 295+ if ( $file ) {
 296+
 297+ $options = parse_ini_file( $file, true );
 298+
 299+ $this->setOptions( $options );
 300+ }
 301+ }
 302+
 303+ /**
 304+ * Check to see if we should load a selenese files for testing.
 305+ *
 306+ * @param string $file The name of the file will correspond to a test
 307+ * method:
 308+ * test-centralnotice-special-banner.htm
 309+ * ->
 310+ * testCentralnoticeSpecialBanner()
 311+ */
 312+ public function loadSeleneseFile( $file )
 313+ {
 314+ $return = true;
 315+
 316+ $label = basename( $file, '.htm' );
 317+ // Debug::dump($label, eval(DUMP) . '\$label', false);
 318+ // $method = ( strtolower( substr( $str, 0, 1 ) ) ) . substr( $str, 1 ) );
 319+ $method = lcfirst( str_replace( ' ', '', ucwords( str_replace( '-', ' ', $label ) ) ) );
 320+ // Debug::dump($method, eval(DUMP) . '\$method', false);
 321+
 322+ // Check to see if we are running converted tests.
 323+ if ( in_array( $method, $this->testCaseMethods ) ) {
 324+
 325+ // Debug::dump($this->runConvertedTests, eval(DUMP) . '\$this->runConvertedTests', false);
 326+ $return = $this->runConvertedTests;
 327+ }
 328+
 329+ return $return;
 330+ }
 331+
 332+ /**
 333+ * Load selenese files for testing.
 334+ */
 335+ public function loadSeleneseFiles()
 336+ {
 337+ $this->testCaseMethods = get_class_methods( $this );
 338+
 339+ // Make sure the directory has been set
 340+ if ( empty( $this->extensionsSeleneseDirectory )) {
 341+ $message = '$this->extensionsSeleneseDirectory cannot be empty.';
 342+ throw new Exception( $message );
 343+ }
 344+
 345+ // Make sure the directory is valid
 346+ if ( !( $this->extensionsSeleneseDirectory )) {
 347+ $message = '$this->extensionsSeleneseDirectory cannot be empty.';
 348+ throw new Exception( $message );
 349+ }
 350+
 351+ //Debug::puke( $this->extensionsSeleneseDirectory, eval(DUMP) . "\$this->extensionsSeleneseDirectory" );
 352+ $files = scandir( $this->extensionsSeleneseDirectory );
 353+ // Debug::dump($this->extensionsSeleneseDirectory, eval(DUMP) . '\$this->extensionsSeleneseDirectory', false);
 354+ //Debug::puke( $files, eval(DUMP) . '\$files' );
 355+
 356+ foreach ( $files as $file ) {
 357+
 358+ // Check the extension of the file to see if it is an htm file.
 359+ $extension = substr( $file, -4 );
 360+
 361+ if ( in_array( $extension, $this->selenesExtensions ) ) {
 362+
 363+ if ( $this->loadSeleneseFile( $file ) ) {
 364+
 365+ $this->seleneseFiles[] = $file;
 366+ }
 367+ }
 368+ }
 369+ //Debug::puke( $this->seleneseFiles, eval(DUMP) . "\$this->seleneseFiles" );
 370+
 371+ $this->hasSelenese = count( $this->seleneseFiles );
 372+ // Debug::dump($this->hasSelenese, eval(DUMP) . '\$this->hasSelenese', true);
 373+ }
 374+
 375+ /**
 376+ * Set options for this test case.
 377+ */
 378+ public function setOptions( $options = array() )
 379+ {
 380+ self::$_options = $options;
 381+
 382+ $this->setExtensionsHost();
 383+
 384+ // If screenshots are enabled, this will create a new directory.
 385+ $this->setScreenshotPath();
 386+
 387+ // If screenshots are enabled, this will create a new directory.
 388+ $this->setSelenesePath();
 389+
 390+ // Debug::dump($this, eval(DUMP) . '\$this', true);
 391+ // Debug::dump($this->screenshotPath, eval(DUMP) . '\$this->screenshotPath', true);
 392+ }
 393+
 394+ /**
 395+ * Set available browsers for this unit test.
 396+ * Set the default browser for this unit test.
 397+ *
 398+ * @deprecated
 399+ */
 400+ protected function setExtensionsBrowsers()
 401+ {
 402+
 403+ $message = __FUNCTION__ . ' is disabled. Set browsers in phpunit.xml';
 404+ throw new Exception( $message );
 405+
 406+ // See if any browsers have been specified in the configuration file.
 407+ if ( !isset( self::$_options['browsers'] ) ) {
 408+ $message = 'You must specify at least one browser to run Selenium tests.';
 409+ throw new Exception( $message );
 410+ }
 411+
 412+ /**
 413+ * Check to see if any browsers are enabled.
 414+ * If they are, add them to @see $this->extensionsBrowsers
 415+ */
 416+ foreach ( self::$_options['browsers'] as $browser => $meta ) {
 417+
 418+ $enable = isset( $meta['enable'] ) ? (boolean) $meta['enable'] : false;
 419+
 420+ if ( $enable ) {
 421+ $this->extensionsBrowsers[ $browser ] = $meta;
 422+ }
 423+ }
 424+
 425+ // If no browsers are enabled, terminate the test.
 426+ if ( empty( $this->extensionsBrowsers ) ) {
 427+ $message = 'You must specify at least one browser to run Selenium tests. Please make sure the browser has been enabled in the configuration file.';
 428+ throw new Exception( $message );
 429+ }
 430+
 431+ // Set the default browser
 432+ // Debug::dump($this->extensionsBrowsersDefault, eval(DUMP) . '\$this->extensionsBrowsersDefault', false);
 433+ $this->extensionsBrowsersDefault = isset( self::$_options['selenium']['browser'] ) ? self::$_options['selenium']['browser'] : $this->extensionsBrowsersDefault;
 434+ // Debug::dump($this->extensionsBrowsersDefault, eval(DUMP) . '\$this->extensionsBrowsersDefault', false);
 435+
 436+ /**
 437+ * If the default browser does not exist in the available browsers,
 438+ * choose the first enabled browser.
 439+ */
 440+ if ( !isset( $this->extensionsBrowsers[ $this->extensionsBrowsersDefault ] ) ) {
 441+ $this->extensionsBrowsersDefault = key( $this->extensionsBrowsers );
 442+ }
 443+
 444+ if ( !isset( $this->extensionsBrowsers[ $this->extensionsBrowsersDefault ]['application'] ) ) {
 445+ $message = 'The browser application';
 446+ throw new Exception( $message );
 447+ }
 448+
 449+ $this->extensionsBrowsersDefaultApplication = $this->extensionsBrowsers[ $this->extensionsBrowsersDefault ]['application'];
 450+
 451+ // Debug::dump($this->extensionsBrowsersDefault, eval(DUMP) . '\$this->extensionsBrowsersDefault', false);
 452+ // Debug::dump($this->extensionsBrowsers, eval(DUMP) . '\$this->extensionsBrowsers', true);
 453+ }
 454+
 455+ /**
 456+ * Set the hostname for this unit test.
 457+ * Set the browser Url for this unit test.
 458+ */
 459+ protected function setExtensionsHost()
 460+ {
 461+ // See if there is a host section set in the configuration
 462+ if ( isset( self::$_options['host'] ) ) {
 463+ $this->extensionsHostname = isset( self::$_options['host']['hostname'] ) ? self::$_options['host']['hostname'] : $this->extensionsHostname;
 464+ $this->extensionsBrowsersUrl = isset( self::$_options['host']['browserUrl'] ) ? self::$_options['host']['browserUrl'] : $this->extensionsBrowsersUrl;
 465+ }
 466+
 467+ //Debug::dump( self::$_options, eval(DUMP) . "self::\$_options" );
 468+
 469+ // Make sure there is a trailing slash on the url
 470+ $this->extensionsBrowsersUrl = rtrim( $this->extensionsBrowsersUrl, '/' ) . '/';
 471+ }
 472+
 473+ /**
 474+ * Get the browser Url for this unit test.
 475+ */
 476+ protected function getExtensionsBrowsersUrl()
 477+ {
 478+ return $this->extensionsBrowsersUrl;
 479+ }
 480+
 481+ /**
 482+ * Set screenshot path
 483+ * Set screenshots for after assertion option.
 484+ * Set screenshots for slideshow option.
 485+ */
 486+ protected function setScreenshotPath()
 487+ {
 488+ // See if there is a selenium section set in the configuration
 489+ if ( isset( self::$_options['selenium'] ) ) {
 490+
 491+ // Set screenshots for slideshow option
 492+ $this->captureScreenshotsForSlideshow = isset( self::$_options['selenium']['screenshots'] ) ? self::$_options['selenium']['screenshots'] : $this->captureScreenshotsForSlideshow;
 493+ $this->captureScreenshotsAfterAssertion = isset( self::$_options['selenium']['screenshotAfterAssertion'] ) ? self::$_options['selenium']['screenshotAfterAssertion'] : $this->captureScreenshotsForSlideshow;
 494+ }
 495+
 496+ if ( $this->captureScreenshotsForSlideshow ) {
 497+
 498+ // Set screenshot path
 499+ $this->screenshotPath = $this->unitTestBasePath . DS . $this->getExtensionName() . '/selenium/screenshots';
 500+
 501+ if ( !is_dir( $this->screenshotPath ) ) {
 502+ $message = 'Please create the directory for screenshots, it does not exist: ' . $this->screenshotPath;
 503+ throw new Exception( $message );
 504+ }
 505+
 506+ if ( !is_writable( $this->screenshotPath ) ) {
 507+ $message = 'Please make sure the directory for screenshots is writable: ' . $this->screenshotPath;
 508+ throw new Exception( $message );
 509+ }
 510+
 511+ // Make sure there is a trailing slash on the path
 512+ $this->screenshotPath = rtrim( $this->screenshotPath, '/' ) . '/';
 513+
 514+ /**
 515+ * Make sure that @see $this->extensionsTestCaseStamp has been set.
 516+ */
 517+ if ( empty( $this->extensionsTestCaseStamp ) ) {
 518+ $message = 'Unable to create a screenshot directory, the extensionsTestCaseStamp has not been set.';
 519+ throw new Exception( $message );
 520+ }
 521+
 522+ $this->screenshotPath .= $this->extensionsTestCaseStamp;
 523+ // Debug::dump($this->screenshotPath, eval(DUMP) . '\$this->screenshotPath', false);
 524+
 525+ if ( !is_dir( $this->screenshotPath ) ) {
 526+ if ( !mkdir( $this->screenshotPath ) ) {
 527+ $message = 'Please make the directory for screenshots is writable: ' . $this->screenshotPath;
 528+ throw new Exception( $message );
 529+ }
 530+ }
 531+
 532+ $pathToTemplate = dirname( __FILE__ ) . '/ExtensionsSeleniumSlideshowTemplate.php';
 533+ if ( !copy( $pathToTemplate, $this->screenshotPath . '/index.php' ) ) {
 534+ $message = 'Unable to copy slideshow template to: ' . $this->screenshotPath;
 535+ throw new Exception( $message );
 536+ }
 537+
 538+ // Begin building relative web root for path to slideshow.
 539+ $relativeWebroot = dirname( __FILE__ );
 540+ //www/github/Mediawiki-Extensions-UnitTest/tests/UnitTest/selenium/screenshots/2012-01-22-1129-18
 541+ //Debug::dump( $relativeWebroot, eval(DUMP) . "\$relativeWebroot" );
 542+ if ( strpos( $this->screenshotPath, $relativeWebroot ) === 0 ) {
 543+ // Debug::dump($this->screenshotUrl, eval(DUMP) . '\$this->screenshotUrl', false);
 544+
 545+ $relativeWebroot = substr( $this->screenshotPath, strlen( $relativeWebroot ) + 1 );
 546+ //Debug::dump( $relativeWebroot, eval(DUMP) . "\$relativeWebroot" );
 547+
 548+ $relativeWebroot = 'extensions' . DS . $this->getExtensionName() . DS . $relativeWebroot;
 549+
 550+ $this->screenshotUrl = $this->extensionsBrowsersUrl . $relativeWebroot;
 551+ // Debug::dump($this->screenshotUrl, eval(DUMP) . '\$this->screenshotUrl', false);
 552+ }
 553+ //Debug::dump( $this->screenshotUrl, eval(DUMP) . "\$this->screenshotUrl" );
 554+ //Debug::puke( $this->screenshotPath, eval(DUMP) . "\$this->screenshotPath" );
 555+
 556+ $message = "\n" . 'Go to slide show: ' . $this->screenshotUrl . "\n";
 557+ echo( $message );
 558+ }
 559+ else {
 560+
 561+ // Disable screenshots on failures
 562+ $this->captureScreenshotOnFailure = false;
 563+ }
 564+ }
 565+
 566+ /**
 567+ * Set selenese path
 568+ */
 569+ protected function setSelenesePath()
 570+ {
 571+ // See if there is a selenium section set in the configuration
 572+ if ( isset( self::$_options['selenium'] ) ) {
 573+
 574+ // Set screenshots for slideshow option
 575+ $this->loadSeleneseFiles = isset( self::$_options['selenium']['selenese'] ) ? self::$_options['selenium']['selenese'] : $this->loadSeleneseFiles;
 576+ }
 577+
 578+ if ( $this->loadSeleneseFiles ) {
 579+
 580+ // Set selenese path
 581+ $this->extensionsSeleneseDirectory = $this->unitTestBasePath . DS . $this->getExtensionName() . '/selenium/selenese';
 582+
 583+ if ( !is_dir( $this->extensionsSeleneseDirectory ) ) {
 584+ $message = 'Please create the directory for selenese, it does not exist: ' . $this->extensionsSeleneseDirectory;
 585+ throw new Exception( $message );
 586+ }
 587+
 588+ // Make sure there is a trailing slash on the path
 589+ $this->extensionsSeleneseDirectory = rtrim( $this->extensionsSeleneseDirectory, '/' ) . '/';
 590+
 591+ // Debug::dump($this->extensionsSeleneseDirectory, eval(DUMP) . '\$this->extensionsSeleneseDirectory', false);
 592+ }
 593+ }
 594+
 595+ /**
 596+ * Set unitTestBasePath
 597+ *
 598+ * Implement this prototype with the following line:
 599+ *
 600+ * @example $this->unitTestBasePath = realpath(dirname(dirname(__FILE__)));
 601+ *
 602+ */
 603+ abstract protected function setUnitTestBasePath();
 604+
 605+ /**
 606+ * Test case setup
 607+ *
 608+ * Only one browser may be tested at a time.
 609+ */
 610+ protected function setUp()
 611+ {
 612+ // Debug::dump($this->extensionsBrowsersUrl, eval(DUMP) . '\$this->extensionsBrowsersUrl', true);
 613+
 614+ // Set the base url.
 615+ $this->setBrowserUrl( $this->extensionsBrowsersUrl );
 616+
 617+ }
 618+
 619+ /**
 620+ * Run Selenese files if they have been loaded.
 621+ *
 622+ */
 623+ protected function runSeleneseFiles()
 624+ {
 625+ if ( !$this->loadSeleneseFiles ) {
 626+
 627+ // Running selenese files is disabled.
 628+ return;
 629+ }
 630+
 631+ foreach ( $this->seleneseFiles as $seleneseFile ) {
 632+
 633+ $this->runSelenese( $this->extensionsSeleneseDirectory . $seleneseFile );
 634+ }
 635+ }
 636+
 637+ /**
 638+ * Browser functions
 639+ */
 640+
 641+ /**
 642+ * Browser logout
 643+ */
 644+ public function browserLogout() {
 645+ $this->open( '/index.php?title=Special:UserLogout' );
 646+ }
 647+
 648+ /**
 649+ * Browser screen shot
 650+ *
 651+ * @param string $function The function name in which the screenshot issued.
 652+ */
 653+ public function browserScreenshot( $function ) {
 654+
 655+ if ( !$this->captureScreenshotsForSlideshow ) {
 656+ // Screenshots for slideshows has been disabled.
 657+ return;
 658+ }
 659+
 660+ // Increment the screenshot count
 661+ $this->screenshotCount++;
 662+
 663+ $file = $this->screenshotPath . '/' . sprintf( "%05d", $this->screenshotCount ) . '_' . $function . '.png';
 664+
 665+ $directory = realpath( dirname( $file ) );
 666+
 667+ // Verify the directory exists for a screenshot
 668+ if ( !is_writable( $directory ) ) {
 669+ $message = 'The directory is not writable for screenshots: ' . $directory;
 670+ throw new Exception( $message );
 671+ }
 672+
 673+ $this->captureEntirePageScreenshot( $file );
 674+
 675+ }
 676+
 677+ /**
 678+ * Get the take screenshot after assertion flag
 679+ *
 680+ * @param boolean $capture Set to true to take a screenshot after assertion.
 681+ */
 682+ protected function setCaptureScreenshotsAfterAssertion( $capture = true )
 683+ {
 684+ $this->captureScreenshotsAfterAssertion = (boolean) $capture;
 685+ }
 686+
 687+ /**
 688+ * Get the take screenshot after assertion flag
 689+ *
 690+ * @return boolean
 691+ */
 692+ protected function getCaptureScreenshotsAfterAssertion()
 693+ {
 694+ return $this->captureScreenshotsAfterAssertion;
 695+ }
 696+
 697+ /**
 698+ * Template Method that is called after Selenium actions.
 699+ *
 700+ * @param string $action
 701+ * @since Method available since Release 3.1.0
 702+ */
 703+ protected function defaultAssertions( $action )
 704+ {
 705+
 706+ // By default, screenshots will be made after each assertion.
 707+ if ( $this->getCaptureScreenshotsAfterAssertion() ) {
 708+ $this->browserScreenshot( __FUNCTION__ );
 709+ }
 710+ }
 711+
 712+ /**
 713+ * getData
 714+ *
 715+ * This is used to generate posted form data
 716+ *
 717+ * Everything should be returned as strings, in the array, since that is how
 718+ * they will be sent by the form.
 719+ *
 720+ * Anything set in $data will be converted to a string.
 721+ *
 722+ * If a value is null, in $data, it will be removed from $return.
 723+ *
 724+ * @param array $return This is a reference to return.
 725+ * @param array $data Anything set in this array will be returned.
 726+ *
 727+ * @return array
 728+ */
 729+ public function getData( &$return, $data = array() ) {
 730+
 731+ if ( is_array( $data ) ) {
 732+
 733+ foreach( $data as $key => $value ) {
 734+
 735+ // Remove values from return if $value is null.
 736+ if ( is_null( $value ) ) {
 737+
 738+ if ( isset( $return[ $key ] ) ) {
 739+
 740+ unset( $return[ $key ] );
 741+ }
 742+ }
 743+ else {
 744+ $return[ $key ] = (string) $value;
 745+ }
 746+ }
 747+ }
 748+ }
 749+}
Property changes on: trunk/extensions/UnitTest/ExtensionsSeleniumTestCase.php
___________________________________________________________________
Added: svn:eol-style
1750 + native
Added: svn:mime-type
2751 + text/plain
Added: svn:keywords
3752 + Author Date HeadURL Header Id Revision
Index: trunk/extensions/UnitTest/ExtensionsSeleniumSlideshowTemplate.php
@@ -0,0 +1,106 @@
 2+<?php
 3+/**
 4+ * Wikimedia Foundation
 5+ *
 6+ * LICENSE
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 19+ */
 20+/**
 21+ * Selenium testing for Extensions
 22+ *
 23+ * Extend this class
 24+ *
 25+ */
 26+
 27+/**
 28+ * extensionPath
 29+ *
 30+ * @var string $extensionPath
 31+ */
 32+$extensionPath = realpath( dirname( dirname( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) ) ) );
 33+
 34+// Make sure the include path is a parsed as a proper directory.
 35+$extensionPath = is_dir( $extensionPath ) ? $extensionPath : false;
 36+
 37+if ( $extensionPath ) {
 38+
 39+ require_once $extensionPath . '/UnitTest/ExtensionsSeleniumSlideshow.php';
 40+
 41+ $slideshow = new ExtensionsSeleniumSlideshow( __FILE__ );
 42+}
 43+else {
 44+
 45+ echo 'The include path is invalid for the slideshow: ' . __FILE__;
 46+ return;
 47+}
 48+?>
 49+<html>
 50+ <head>
 51+ <title><?php echo $slideshow->getTitle() ?></title>
 52+ <script type="text/javascript" src="/resources/jquery/jquery.js"></script>
 53+ <script type="text/javascript" src="/resources/jquery/jquery.cycle.all.js"></script>
 54+ <style type="text/css">
 55+ ul#slideshow {width: 90%;border:solid;position:relative;overflow:hidden;height: 90%}
 56+ ul#slideshow li {font-size:1.4em;padding:20px;opacity:0;position:absolute}
 57+ #pagination a { border: 1px solid #ccc; background: silver; text-decoration: none; margin: 0 5px; padding: 3px 5px; }
 58+ #pagination a.activeSlide { background: yellow }
 59+ #pagination a:focus { outline: none; }
 60+ </style>
 61+ <script type="text/javascript">
 62+ $(document).ready(function() {
 63+
 64+ $('#slideshow').cycle({
 65+ fx: 'fade',
 66+ speed: 300,
 67+ timeout: 3000,
 68+ next: '#slideshow',
 69+ pause: 1,
 70+ pager: '#pagination',
 71+ before: function(data) {
 72+ var name = $(this).attr('title');
 73+ $('#slideshow_title').html(name);
 74+ }
 75+ });
 76+ });
 77+ </script>
 78+ </head>
 79+ <body>
 80+
 81+ <h1><?php echo $slideshow->getTitle() ?></h1>
 82+ <h2>Slideshow</h2>
 83+ <h3 id="slideshow_title"></h3>
 84+
 85+ <p>Note: Hover over image to pause. Click to advance.</p>
 86+
 87+ <div id="pagination"></div>
 88+
 89+ <?php $images = $slideshow->run() ?>
 90+
 91+ <?php if ( $slideshow->hasImages() ): ?>
 92+
 93+ <ul id="slideshow">
 94+
 95+ <?php foreach ( $images as $image ): ?>
 96+ <li title="<?php echo $image ?>"><img src="<?php echo $image ?>" alt="<?php echo $image ?>" /></li>
 97+ <?php endforeach; ?>
 98+
 99+ </ul>
 100+
 101+ <?php else: ?>
 102+
 103+ <p>No screenshots were generated for the slideshow.</p>
 104+
 105+ <?php endif; ?>
 106+ </body>
 107+</html>
Property changes on: trunk/extensions/UnitTest/ExtensionsSeleniumSlideshowTemplate.php
___________________________________________________________________
Added: svn:eol-style
1108 + native
Added: svn:mime-type
2109 + text/plain
Added: svn:keywords
3110 + Author Date HeadURL Header Id Revision
Index: trunk/extensions/UnitTest/Debug.constants.php
@@ -0,0 +1,107 @@
 2+<?php
 3+/**
 4+ * Wikimedia Foundation
 5+ *
 6+ * LICENSE
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 19+ */
 20+
 21+/*****************************************************************************{/
 22+
 23+/**
 24+ * COMMA
 25+ *
 26+ * A comma
 27+ */
 28+define( 'COMMA', "," );
 29+
 30+/**
 31+ * NL
 32+ *
 33+ * The UNIX new line
 34+ */
 35+define( 'NL', "\n" );
 36+
 37+/**
 38+ * TAB
 39+ *
 40+ * A horizontal tab
 41+ *
 42+ */
 43+define( 'TAB', "\t" );
 44+
 45+/**
 46+ * BR
 47+ *
 48+ * The html new line
 49+ */
 50+define( 'BR', '<br />' );
 51+
 52+/**
 53+ * PREo
 54+ *
 55+ * The html open pre tag
 56+ */
 57+define( 'PREo', '<pre style="text-align: left;">' );
 58+
 59+/**
 60+ * PREc
 61+ *
 62+ * The html close pre tag
 63+ */
 64+define( 'PREc', '</pre>' );
 65+
 66+/**
 67+ * HR
 68+ *
 69+ * The html Horizontal Rule
 70+ */
 71+define( 'HR', '<hr />' );
 72+
 73+/**
 74+ * _
 75+ *
 76+ * A space
 77+ */
 78+define( '_', ' ' );
 79+
 80+/**
 81+ * DS
 82+ *
 83+ * The directory separator.
 84+ */
 85+define( 'DS', DIRECTORY_SEPARATOR );
 86+
 87+/**
 88+ * PS
 89+ *
 90+ * Path Separator
 91+ */
 92+define( 'PS', PATH_SEPARATOR );
 93+
 94+/**
 95+ * PN
 96+ *
 97+ * Paamayim Nekudotayim
 98+ */
 99+define( 'PN', '::' );
 100+
 101+/**
 102+ * DUMP
 103+ *
 104+ * This is used bu Debug:dump() in an eval() call. This causes the variable dump
 105+ * to output line and file information.
 106+ */
 107+define( 'DUMP', 'return __FILE__ . _ . PN . _ . date(\'r\') . _ . NL;' );
 108+
Property changes on: trunk/extensions/UnitTest/Debug.constants.php
___________________________________________________________________
Added: svn:eol-style
1109 + native
Added: svn:mime-type
2110 + text/plain
Added: svn:keywords
3111 + Author Date HeadURL Header Id Revision
Index: trunk/extensions/UnitTest/ExtensionsSeleniumSlideshow.php
@@ -0,0 +1,297 @@
 2+<?php
 3+/**
 4+ * Wikimedia
 5+ *
 6+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 7+ */
 8+
 9+/**
 10+ * @see Debug
 11+ */
 12+require_once 'Debug.php';
 13+
 14+/**
 15+ * ExtensionsSeleniumSlideshow
 16+ *
 17+ * Slideshow generator for Extensions and Selenium testing
 18+ */
 19+class ExtensionsSeleniumSlideshow
 20+{
 21+
 22+ /**
 23+ * The extension for the unit test
 24+ *
 25+ * @param string $extension
 26+ */
 27+ protected $extension = '';
 28+
 29+ /**
 30+ * The images extensions permitted for generation.
 31+ * Currently, Selenium only generates png images.
 32+ *
 33+ * @param array $imageExtensions
 34+ */
 35+ protected $imageExtensions = array(
 36+ '.png',
 37+ );
 38+
 39+ /**
 40+ * The image directory for the slideshow.
 41+ *
 42+ * @param string $imageDirectory
 43+ */
 44+ protected $imageDirectory = '';
 45+
 46+ /**
 47+ * The images for the slideshow.
 48+ *
 49+ * @param array $images
 50+ */
 51+ protected $images = array();
 52+
 53+ /**
 54+ * If the slideshow has images, this will hold the count of images
 55+ *
 56+ * @param integer $hasImages
 57+ */
 58+ protected $hasImages = 0;
 59+
 60+ /**
 61+ * Messages displayed for the user.
 62+ *
 63+ * @param array $messages
 64+ */
 65+ protected $messages = array();
 66+
 67+ /**
 68+ * The slideshow file
 69+ *
 70+ * @param string $slideshowFilePath
 71+ */
 72+ protected $slideshowFilePath = '';
 73+
 74+ /**
 75+ * The slideshow title
 76+ *
 77+ * @param string $title
 78+ */
 79+ protected $title = 'Selenium Smoke Test';
 80+
 81+ /**
 82+ * The webRoot to the mediawiki instance.
 83+ *
 84+ * @param string $webRoot
 85+ */
 86+ protected $webRoot = '';
 87+
 88+ /**
 89+ *
 90+ *
 91+ * @param string $file The file running the slideshow
 92+ * @param string $matches The matches found from the image path
 93+ */
 94+ public function __construct( $file )
 95+ {
 96+ $this->setSlideshowFilePath( $file );
 97+
 98+ // Set the webRoot of the site
 99+ $this->setWebRoot();
 100+
 101+ $this->parseFilePath();
 102+
 103+ // The image directory
 104+ $this->imageDirectory = realpath( dirname( $this->getSlideshowFilePath() ) );
 105+ }
 106+
 107+ /**
 108+ * Generate a slideshow
 109+ */
 110+ public function run()
 111+ {
 112+
 113+ $this->loadImages();
 114+
 115+ return $this->images;
 116+ }
 117+
 118+ ############################################################################
 119+ #
 120+ # Paths
 121+ #
 122+ ############################################################################
 123+
 124+ /**
 125+ * Parse slideshow file path
 126+ */
 127+ public function parseFilePath()
 128+ {
 129+ ///www/sites/localhost/wikimedia-commit.localhost.wikimedia.org/extensions/UnitTest/tests/UnitTest/selenium/screenshots/2012-01-22-1154-14/
 130+
 131+ $pattern = '/(?P<IP>.*)\/extensions\/(?P<extension>\w+)\/tests\/selenium\/screenshots\/(?P<stamp>.*)/';
 132+ $pattern = '/(?P<IP>.*)\/extensions\/(?P<extension>\w+)\/tests\/(?P<extensionUnitTest>\w+)\/selenium\/screenshots\/(?P<stamp>.*)/';
 133+ //$pattern = '/(?P<IP>.*)\/extensions\/(?P<extension>\w+)\/tests\/(?P<extensionUnitTest>\w+)/selenium\/screenshots\/(?P<stamp>.*)/';
 134+ $matches = array();
 135+ preg_match( $pattern, dirname( $this->getSlideshowFilePath() ), $matches );
 136+
 137+ //Debug::dump( $this->getSlideshowFilePath(), eval(DUMP) . '\$this->getSlideshowFilePath()' );
 138+ //Debug::dump( $matches, eval(DUMP) . '\$matches' );
 139+
 140+ $IP = isset( $matches['IP'] ) ? $matches['IP'] : '';
 141+ //Debug::puke( $IP, eval(DUMP) . '\$IP' );
 142+ $this->extension = isset( $matches['extension'] ) ? $matches['extension'] : '';
 143+ $this->stamp = isset( $matches['stamp'] ) ? $matches['stamp'] : '';
 144+
 145+
 146+ if ( $IP != $this->getWebRoot() ) {
 147+ $message = 'The web root does not match what was found based on the extension gallery path.';
 148+ $message .= "\n" . 'IP: ' . $IP;
 149+ $message .= "\n" . 'this->getWebRoot(): ' . $this->getWebRoot();
 150+ throw new Exception( $message );
 151+ }
 152+
 153+ // Add extension to title
 154+ if ( $this->extension != '' ) {
 155+ $this->appendTitle( ': ' . $this->extension );
 156+ }
 157+
 158+ // Add stamp to title
 159+ if ( $this->stamp != '' ) {
 160+ $this->appendTitle( ' - ' . $this->stamp );
 161+ }
 162+
 163+ }
 164+
 165+ /**
 166+ * Get the slideshow file path
 167+ */
 168+ public function getSlideshowFilePath()
 169+ {
 170+ return $this->slideshowFilePath;
 171+ }
 172+
 173+ /**
 174+ * Set the slideshow file path
 175+ */
 176+ public function setSlideshowFilePath( $file )
 177+ {
 178+ $this->slideshowFilePath = realpath( $file );
 179+ }
 180+ /**
 181+ * Get the web root
 182+ */
 183+ public function getWebRoot()
 184+ {
 185+ return $this->webRoot;
 186+ }
 187+
 188+ /**
 189+ * Set the web root
 190+ */
 191+ public function setWebRoot()
 192+ {
 193+ $this->webRoot = realpath( dirname( dirname( dirname( __FILE__ ) ) ) );
 194+ //Debug::puke( $this->webRoot, eval(DUMP) . "\$this->webRoot" );
 195+ }
 196+
 197+
 198+ ############################################################################
 199+ #
 200+ # Slideshow image handling
 201+ #
 202+ ############################################################################
 203+
 204+ /**
 205+ * Load images for the slideshow
 206+ */
 207+ public function loadImages()
 208+ {
 209+
 210+ $files = scandir( $this->imageDirectory );
 211+
 212+ foreach ( $files as $file ) {
 213+
 214+ // Check the extension of the file to see if it is an image.
 215+ $extension = substr( $file, -4 );
 216+
 217+ if ( in_array( $extension, $this->imageExtensions ) ) {
 218+
 219+ $this->images[] = $file;
 220+ }
 221+ }
 222+
 223+ $this->hasImages = count( $this->images );
 224+ }
 225+
 226+ /**
 227+ * Check to see if the slideshow has images loaded.
 228+ *
 229+ * @return integer Returns a count of the images
 230+ */
 231+ public function hasImages()
 232+ {
 233+ return $this->hasImages;
 234+ }
 235+
 236+ ############################################################################
 237+ #
 238+ # Slideshow title
 239+ #
 240+ ############################################################################
 241+
 242+ /**
 243+ * Append a string to the slideshow title.
 244+ *
 245+ * @param string $append
 246+ *
 247+ * @return ExtensionsSeleniumSlideshow
 248+ */
 249+ public function appendTitle( $append )
 250+ {
 251+
 252+ $this->title .= $append;
 253+
 254+ return $this;
 255+ }
 256+
 257+ /**
 258+ * Get the slideshow title.
 259+ *
 260+ * @return string
 261+ */
 262+ public function getTitle()
 263+ {
 264+ return $this->title;
 265+ }
 266+
 267+ ############################################################################
 268+ #
 269+ # Slideshow messages
 270+ #
 271+ ############################################################################
 272+
 273+ /**
 274+ * Add a message for a user.
 275+ *
 276+ * @param string $message
 277+ *
 278+ * @return ExtensionsSeleniumSlideshow
 279+ */
 280+ public function addMessage( $message )
 281+ {
 282+
 283+ $this->messages[] = $message;
 284+
 285+ return $this;
 286+ }
 287+
 288+ /**
 289+ * Get the slideshow messages.
 290+ *
 291+ * @return string
 292+ */
 293+ public function getMessages()
 294+ {
 295+ return $this->messages;
 296+ }
 297+}
 298+
Property changes on: trunk/extensions/UnitTest/ExtensionsSeleniumSlideshow.php
___________________________________________________________________
Added: svn:eol-style
1299 + native
Added: svn:mime-type
2300 + text/plain
Added: svn:keywords
3301 + Author Date HeadURL Header Id Revision
Index: trunk/extensions/UnitTest/Debug.php
@@ -0,0 +1,124 @@
 2+<?php
 3+/**
 4+ * Wikimedia Foundation
 5+ *
 6+ * LICENSE
 7+ *
 8+ * This program is free software; you can redistribute it and/or modify
 9+ * it under the terms of the GNU General Public License as published by
 10+ * the Free Software Foundation; either version 2 of the License, or
 11+ * (at your option) any later version.
 12+ *
 13+ * This program is distributed in the hope that it will be useful,
 14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 16+ * GNU General Public License for more details.
 17+ *
 18+ * @author Jeremy Postlethwaite <jpostlethwaite@wikimedia.org>
 19+ */
 20+
 21+/**
 22+ * Constants used by the debugging statements.
 23+ */
 24+require_once 'Debug.constants.php';
 25+
 26+/**
 27+ * Debug
 28+ *
 29+ * This class contains methods to dump variables to the screen and to optionally
 30+ * terminate the application with a stack trace.
 31+ */
 32+class Debug
 33+{
 34+
 35+ ############################################################################
 36+ #
 37+ # utility
 38+ #
 39+ ############################################################################
 40+
 41+ /**
 42+ * Formatted variable dumper with option to terminate script.
 43+ *
 44+ * Including eval(DUMP) in your label will allow you to see line and file
 45+ * information during debugging.
 46+ *
 47+ * <code>
 48+ * <?php
 49+ *
 50+ * // To Display the server variables and kill the script
 51+ * Debug::dump( $_SERVER, eval(DUMP) . 'Server Variables', true );
 52+ *
 53+ * // To Display the server variables and allow the script to resume
 54+ * Debug::dump( $_SERVER, eval(DUMP) . 'Server Variables', false );
 55+ * Debug::dump( $_SERVER, eval(DUMP) . 'Server Variables' );
 56+ *
 57+ * ?>
 58+ * </code>
 59+ *
 60+ * @param mixed $variable The variable to dump.
 61+ * @param string $label The label to pass to the output.
 62+ * @param boolean $die If true, script will terminate.
 63+ */
 64+ public static function dump( $variable, $label, $die = false )
 65+ {
 66+ global $wgCommandLineMode;
 67+
 68+ // An 80 character rule.
 69+ $rule80 = '--------------------------------------------------------------------------------';
 70+
 71+ // Pre dump formatting
 72+ $pre = ( $wgCommandLineMode ) ? PHP_EOL . $rule80 . PHP_EOL . $label . PHP_EOL . PHP_EOL : '<div style="clear: both">' . HR . PHP_EOL . $label . PHP_EOL . HR . PREo . PHP_EOL;
 73+
 74+ // Post dump formatting
 75+ $post = ( $wgCommandLineMode ) ? PHP_EOL : PHP_EOL . PREc . _ . HR . '</div>' . PHP_EOL;
 76+
 77+ echo $pre;
 78+ if ( is_string( $variable ) ) {
 79+ print_r( $variable );
 80+ }
 81+ else {
 82+ var_dump( $variable );
 83+ }
 84+ echo $post;
 85+
 86+ // @codeCoverageIgnoreStart
 87+ if ( $die === true ) {
 88+ die( 'Terminating at: ' . eval( DUMP ) . 'From: ' . $label . PHP_EOL );
 89+ }
 90+ // @codeCoverageIgnoreEnd
 91+ }
 92+
 93+ /**
 94+ * Puke a stack trace.
 95+ *
 96+ * By default, calling puke will make the application die.
 97+ *
 98+ * <code>
 99+ * <?php
 100+ *
 101+ * // To Display the server variables and kill the script
 102+ * Debug::puke( $_SERVER, eval(DUMP) . 'Server Variables' );
 103+ *
 104+ * ?>
 105+ * </code>
 106+ *
 107+ * @param mixed $variable The variable to dump.
 108+ * @param string $label The label to pass to the output.
 109+ * @param boolean $die If true, script will terminate.
 110+ */
 111+ public static function puke( $variable, $label, $die = true )
 112+ {
 113+ // Dump the variable to the screen.
 114+ // Throw and catch an exception and dump the stack trace.
 115+ try {
 116+
 117+ Debug::dump( $variable, $label, false );
 118+ throw new Exception( $label );
 119+
 120+ } catch ( Exception $e ) {
 121+
 122+ Debug::dump( $e->getTraceAsString(), eval( DUMP ), $die );
 123+ }
 124+ }
 125+}
Property changes on: trunk/extensions/UnitTest/Debug.php
___________________________________________________________________
Added: svn:eol-style
1126 + native
Added: svn:mime-type
2127 + text/plain
Added: svn:keywords
3128 + Author Date HeadURL Header Id Revision

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r109762Adding UnitTest Extensionjpostlethwaite21:45, 22 January 2012

Status & tagging log