[pLog-svn] r4078 - in plog/branches/lifetype-1.1.1: . class/test class/test/helpers class/test/tests/ui

oscar at devel.lifetype.net oscar at devel.lifetype.net
Tue Oct 3 18:57:02 GMT 2006


Author: oscar
Date: 2006-10-03 18:57:01 +0000 (Tue, 03 Oct 2006)
New Revision: 4078

Added:
   plog/branches/lifetype-1.1.1/class/test/helpers/consolereporter.class.php
Modified:
   plog/branches/lifetype-1.1.1/class/test/testrunner.class.php
   plog/branches/lifetype-1.1.1/class/test/tests/ui/articlecategoriesui_test.class.php
   plog/branches/lifetype-1.1.1/runtests.php
Log:
Since the test suite is relatively big, it is not possible to run it in a browser anymore (PHP will kill the process if executing for over the time_limit setting) so it's now possible to execute it from the command line. Just call runtests.php as follows:

php runtests.php suite1 suite2 suite3 ...

Use 'all' to run all tests in one go and no parameters to get a list of the available suites.


Added: plog/branches/lifetype-1.1.1/class/test/helpers/consolereporter.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/test/helpers/consolereporter.class.php	2006-10-02 21:49:16 UTC (rev 4077)
+++ plog/branches/lifetype-1.1.1/class/test/helpers/consolereporter.class.php	2006-10-03 18:57:01 UTC (rev 4078)
@@ -0,0 +1,106 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/test/PHPUnit/TestResult.php" );
+	include_once( PLOG_CLASS_PATH."class/test/PHPUnit/TestListener.php" );	
+
+	/**
+	 * \ingroup Test
+	 *
+	 * Reporter class that generates output suitable for a terminal console
+	 */
+	class ConsoleReporter
+	{
+		var $_result;
+		
+		function ConsoleReporter( $result )
+		{
+			$this->_result = $result;
+		}
+		
+		function _getHeader()
+		{
+			return("\nFailed tests\n------------\n");
+		}
+		
+		function _getFooter()
+		{
+			return( $this->_getStats());
+		}
+		
+		function _cleanGroupName( $group )
+		{
+			$group = strtolower( $group );
+			$group = str_replace( "_test", "", $group );
+			return( $group );
+		}
+		
+		function _getPassed( $test, $group = "" )
+		{
+			return( "[".$this->_cleanGroupName($group)."] ".$test->getName()." => PASSED\n" );
+		}
+		
+		function _getFailed( $test, $group = "" )
+		{
+			return( "[".$this->_cleanGroupName($group)."] ".$test->_failedTest->getName()." => FAILED: ".$test->_thrownException."\n" );
+		}
+		
+		function _getStats()
+		{
+return( "
+Test Stats
+----------
+* Number of tests: ".$this->_result->runCount()."
+* Number of tests passed: ".($this->_result->runCount() - $this->_result->failureCount())."
+* Number of tests failed: ".$this->_result->failureCount()."\n"
+);
+		}
+		
+		function generate()
+		{
+			$groups = $this->_prepare();
+			
+	        $result = $this->_getHeader();
+	
+			foreach( $groups as $group => $tests ) {
+				foreach( $tests as $test ) {
+					if( isset( $test->_failedTest )) {
+						$result .= $this->_getFailed( $test, $group );
+					}
+					/*else {
+						$result .= $this->_getPassed( $test, $group );
+					}*/
+				}
+			}
+
+	        $result .= $this->_getFooter();
+			return( $result );
+		}
+		
+		function _prepare()
+		{
+			$groups = Array();
+	        foreach ($this->_result->_passedTests as $passedTest) {
+				$groups[get_class($passedTest)][] = $passedTest;
+	        }				
+	        foreach ($this->_result->_failures as $failedTest) {
+				$groups[get_class($failedTest->_failedTest)][] = $failedTest;
+	        }
+	
+			return( $groups );
+		}
+	}
+	
+	/**
+	 * A listener class to do "live" reporting to the console
+	 */
+	class ConsoleReporterListener extends PHPUnit_TestListener
+	{
+	      function startTest(&$test) {
+	          print( "Executing test [".ConsoleReporter::_cleanGroupName(get_class($test))."] ".$test->getName()." ... " );
+	      }		
+	
+	      function endTest(&$test) {
+	          print( "DONE\n" );
+	      }	
+	}
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.1.1/class/test/testrunner.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/test/testrunner.class.php	2006-10-02 21:49:16 UTC (rev 4077)
+++ plog/branches/lifetype-1.1.1/class/test/testrunner.class.php	2006-10-03 18:57:01 UTC (rev 4078)
@@ -61,6 +61,7 @@
 		var $files;
 		var $suite;
 		var $excludeFolders;
+		var $listener;
 		
 		/**
 		 * Constructor.
@@ -81,8 +82,20 @@
 			$this->excludeFolders = explode( ",", EXCLUDE_FOLDERS_LIST );
 			
 			$this->files = $this->_findClasses( $this->folders, $this->pattern );
+			
+			$this->listener = NULL;
 		}
 		
+		/** 
+		 * Adds a test listener
+		 *
+		 * @see PHPUnit_TestResult::addListener
+		 */
+		function addListener( &$listener )
+		{
+			$this->listener = $listener;
+		}
+		
 		/**
 		 * Runs a test suite, or all of them if no test suite name is given
 		 *
@@ -91,15 +104,16 @@
 		 * test suites were run and their results. Please use the HtmlReporter class to obtain
 		 * a nicer report.
 		 */
-		function run( $suite = "all" )
-		{
+		function run( $suite = Array( "all" ))
+		{			
 			// process all the classes and add them to the test suite
 			$this->suite = new PHPUnit_TestSuite();			
 			foreach( $this->files as $file ) {
 				// build the class name
 				$className = str_replace( ".class.php", "", basename( $file ));				
 				// load the class file
-				if( $suite == "all" || $suite == $className || $suite == str_replace( "_test", "", $className )) {
+				//if( $suite == "all" || $suite == $className || $suite == str_replace( "_test", "", $className )) {
+				if( in_array( "all", $suite ) || in_array( $className, $suite ) || in_array( str_replace( "_test", "", $className ), $suite )) {
 					// add the current suite only if we're either loading them all or if
 					// the current one is the one we want to load
 					include_once( $file );
@@ -109,8 +123,12 @@
 			}			
 			
 			// after adding all the tests, run the suite and return the result
-			$result = PHPUnit::run( $this->suite );
-			
+	        $result = new PHPUnit_TestResult();	
+			if( $this->listener !== NULL ) {			
+				$result->addListener( $this->listener );
+			}
+	        $this->suite->run( $result );
+
 			return( $result );
 		}
 		

Modified: plog/branches/lifetype-1.1.1/class/test/tests/ui/articlecategoriesui_test.class.php
===================================================================
--- plog/branches/lifetype-1.1.1/class/test/tests/ui/articlecategoriesui_test.class.php	2006-10-02 21:49:16 UTC (rev 4077)
+++ plog/branches/lifetype-1.1.1/class/test/tests/ui/articlecategoriesui_test.class.php	2006-10-03 18:57:01 UTC (rev 4078)
@@ -68,7 +68,7 @@
 			
 			// find the category in the db...
 			$cats = new ArticleCategories();
-			$cat = $cats->getCategoryByName( "test-category", $this->blog->getId());
+			$cat = $cats->getCategoryByName( "test_category", $this->blog->getId());
 			
 			// ...and delete it via the UI
 			$this->assertUIScript(

Modified: plog/branches/lifetype-1.1.1/runtests.php
===================================================================
--- plog/branches/lifetype-1.1.1/runtests.php	2006-10-02 21:49:16 UTC (rev 4077)
+++ plog/branches/lifetype-1.1.1/runtests.php	2006-10-03 18:57:01 UTC (rev 4078)
@@ -1,45 +1,75 @@
-<html>
-	<head>
-		<title>LifeType Test Suite</title>
-	</head>
-	<body>
 <?php
 
 	define( "INCLUDE_PLUGIN_TESTS", true );
 
 	if (!defined( "PLOG_CLASS_PATH" )) {
-	    define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
+	   define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
 	}
 
 	include_once( PLOG_CLASS_PATH."class/test/testrunner.class.php" );
-	include_once( PLOG_CLASS_PATH."class/test/helpers/htmlreporter.class.php" );	
+	include_once( PLOG_CLASS_PATH."class/test/helpers/htmlreporter.class.php" );
+	include_once( PLOG_CLASS_PATH."class/test/helpers/consolereporter.class.php" );	
 	include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
-	
-	// get the suite name from the request or run all of them if no parameter specified
-	$request = HttpVars::getRequest();
-	$suiteName = isset( $request["suite"] ) ? strtolower($request["suite"]) : "";
-	
+
 	// if plugins should also be included when testing, let's load them now
 	$folders = Array( TEST_CLASS_FOLDER ); 
 	if( INCLUDE_PLUGIN_TESTS ) {
 		include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
-		
+	
 		$pm =& PluginManager::getPluginManager();
 		$plugins = $pm->getPluginListFromFolder();
-		
+	
 		foreach( $plugins as $plugin ) {
 			$folders[] = PLOG_CLASS_PATH."plugins/".$plugin."/class/tests";
 		}
 	}
-
+	
 	// create a new TestRunner class, which will take care of loading all our
 	// tests cases, instantiate them and tell PHPUnit to run the tests specified
 	// in the request
-	$r = new TestRunner( $folders ); 
+	$r = new TestRunner( $folders );	
+
+	// check if we're running from the command line
+	$commandLine = isset( $argv );
+
+	if( $commandLine ) {
+		if( count( $argv ) < 2 ) {
+			// if running from command line and we have no parameters, show the list of available suites			
+			print("\nPlease specify the name of a test suite to run, or 'all' to run all tests in one batch\n\n" );
+			foreach( $r->getTestSuites() as $suite ) {
+				print( "* [".$suite->getSuiteName()."]: " );
+				foreach( $suite->getTestMethods() as $method ) {
+					print( $method.", ");
+				}
+				print( "\n" );
+			}
+		}
+		else {
+			// run the given suite
+			$r->addListener( new ConsoleReporterListener );
+			$result = $r->run( $argv );
+
+			// check the results when ready
+			$reporter = new ConsoleReporter( $result );
+			print($reporter->generate());			
+		}	
+	}
+	else {
+?>
+<html>
+	<head>
+		<title>LifeType Test Suite</title>
+	</head>
+	<body>
+<?php
+
+	// get the suite name from the request or run all of them if no parameter specified
+	$request = HttpVars::getRequest();
+	$suiteName = isset( $request["suite"] ) ? strtolower($request["suite"]) : "";
 	
 	if( $suiteName == "" ) {
 		// no suite name was specified, let's just show what we have
-		$suites = $r->getTestSuites();		
+		$suites = $r->getTestSuites();
 ?>
 <h1>Available Test Suites</h1>
 <p>Plese select a test suite to run or click "All" to run all the test suites together.</p>
@@ -65,7 +95,7 @@
 <?php
 	}
 	else {
-		$result = $r->run( $suiteName );
+		$result = $r->run( Array( $suiteName ));
 	
 		// check the results when ready
 		$reporter = new HTMLReporter( $result );
@@ -73,4 +103,5 @@
 	}
 ?>
 </body>
-</html>
\ No newline at end of file
+</html>
+<?php } ?>
\ No newline at end of file



More information about the pLog-svn mailing list