Revision
5
Author
emsmith
Date
2006-08-09 11:58:41 -0700 (Wed, 09 Aug 2006)

Log Message

Code coverage and profiling hooks, code coverage toggle, and force a result class to be passed to the constructor

Modified Paths

Diff

Modified: library/trunk/lib/test/unit.abstract.php (4 => 5)


--- library/trunk/lib/test/unit.abstract.php	2006-08-09 18:56:49 UTC (rev 4)
+++ library/trunk/lib/test/unit.abstract.php	2006-08-09 18:58:41 UTC (rev 5)
@@ -30,6 +30,8 @@
 abstract class CCL_TestUnit
 {
 
+	//------------ Overrideable -----------
+
 	/**
 	 * a name for the testcase - if left blank by extending class use classname
 	 * @var $name string
@@ -37,16 +39,26 @@
 	protected $name;
 
 	/**
+	 * test message - transient per test
+	 * @var $message string
+	 */
+	protected $message;
+
+	//--------- Manipulate before sending ---------
+
+	/**
 	 * result class to ping
 	 * @var $result object
 	 */
-	protected $result;
+	private $result;
 
+	//------------ Public Toggles -----------
+
 	/**
-	 * test message - transient per test
-	 * @var $message string
+	 * turn on code coverage
+	 * @var $coverage bool
 	 */
-	protected $message;
+	public $coverage = FALSE;
 
 	//----------------------------------------------------------------
 	//             Setup
@@ -57,31 +69,19 @@
 	 *
 	 * the constructor just sets up the testcase name
 	 *
-	 * @param object $reporter instanceof CC_Reporter subclass
+	 * @param object $result instanceof CCL_TestResult or subclass
 	 * @return void
 	 */
-	public function __construct()
+	public function __construct(CCL_TestResult $result)
 	{
 		if (is_null($this->name))
 		{
 			$this->name = get_class($this);
 		}
-		$this->result = new CCL_TestResult();
+		$this->result = $result;
 		return;
 	}
 
-	/**
-	 * public function getResult
-	 *
-	 * returns the result class, only really useful for attaching observers
-	 *
-	 * @return object
-	 */
-	public function getResult()
-	{
-		return $this->result;
-	}
-
 	//----------------------------------------------------------------
 	//             Overrideable by Subclass
 	//----------------------------------------------------------------
@@ -159,6 +159,7 @@
 		preg_match('/\$this->assert\((.*),/', $file[$info[0]['line'] - 1], $matches);
 		$code = $matches[1];
 		unset($info, $file, $matches);
+		// actually do the assert
 		$this->result->startAssert($code);
 		if ($item == TRUE)
 		{
@@ -184,7 +185,9 @@
 	 */
 	final public function run($test = NULL)
 	{
+		$this->result->mark('start');
 		$this->result->startTestUnit($this->name);
+		
 		if (is_null($test))
 		{
 			foreach ($this->listTests() as $method)
@@ -197,18 +200,19 @@
 			$this->runTest($test);
 		}
 		$this->result->endTestUnit();
+		$this->result->mark('stop');
 		return;
 	}
 
 	/**
-	 * final protected function runTest
+	 * private function runTest
 	 *
-	 * run a single test from this class in isolate environment
+	 * run a single test from this class in isolated environment
 	 *
 	 * @param string $method name of a test method to run
 	 * @return void
 	 */
-	final protected function runTest($method)
+	private function runTest($method)
 	{
 		if (!in_array($method, $this->listTests()))
 		{
@@ -216,10 +220,16 @@
 				. get_class($this) . ' or is not a test method');
 			return;
 		}
+		$this->result->mark($method);
 		set_error_handler(array($this, 'errorHandler'));
 		$this->message = NULL;
+		$do_coverage = (extension_loaded('xdebug') && $this->coverage);
 		$this->setUp();
 		$this->result->startTest($method);
+		if ($do_coverage)
+		{
+			xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
+		}
 		try
 		{
 			$this->$method();
@@ -241,6 +251,12 @@
 		{
 			$this->result->errorTest($e);
 		}
+		if ($do_coverage)
+		{
+			$coverage = xdebug_get_code_coverage();
+			xdebug_stop_code_coverage();
+			$this->result->codeCoverage($coverage, $method);
+		}
 		restore_error_handler();
 		$this->result->endTest();
 		$this->tearDown();