Permalink
Browse files

* Added phpunit-based testing framework.

  • Loading branch information...
1 parent 6d19e4b commit c3bdff423fefbcfe5d43120d1eba9d30be5c8a08 qiang.xue committed Jun 12, 2009
Showing with 26 additions and 19 deletions.
  1. +1 −0 CHANGELOG
  2. +11 −9 framework/test/CDbFixtureManager.php
  3. +7 −5 framework/test/CDbTestCase.php
  4. +7 −5 framework/test/CWebTestCase.php
View
@@ -6,6 +6,7 @@ Version 1.1a to be released
---------------------------
- New: Refactored scenario-based validation and massive assignments (Qiang)
- New: Added CDbSchema::checkIntegrity() and resetSequence() (Qiang)
+- New: Added phpunit-based testing framework (Qiang)
Version 1.0.7 to be released
----------------------------
@@ -27,8 +27,9 @@
* If the init script does not exist, the table will be emptied.
*
* Fixtures must be stored under the {@link basePath} directory. The directory
- * may contain a file named "init.php" which will be executed once at the beginning
- * of the test execution.
+ * may contain a file named "init.php" which will be executed once to initialize
+ * the database. If this file is not found, all available fixtures will be loaded
+ * into the database.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
@@ -79,6 +80,7 @@ public function init()
parent::init();
if($this->basePath===null)
$this->basePath=Yii::getPathOfAlias('application.tests.fixtures');
+ $this->prepare();
}
/**
@@ -99,9 +101,8 @@ public function getDbConnection()
/**
* Prepares the fixtures for the whole test.
- * This method should be called when tests start and should only be called once.
- * The method will execute the database init script if it is available.
- * It will then load every fixture found under {@link basePath}.
+ * This method is invoked in {@link init}. It executes the database init script
+ * if it exists. Otherwise, it will load all available fixtures.
*/
public function prepare()
{
@@ -111,10 +112,11 @@ public function prepare()
if(is_file($initFile))
require($initFile);
-
- foreach($this->getFixtures() as $fixture)
- $this->loadFixture($fixture);
-
+ else
+ {
+ foreach($this->getFixtures() as $fixture)
+ $this->loadFixture($fixture);
+ }
$this->checkIntegrity(true);
}
@@ -39,12 +39,13 @@
class CDbTestCase extends CTestCase
{
/**
- * @var array a list of fixtures that should be loaded for all test cases.
+ * @var array a list of fixtures that should be loaded before each test method executes.
* The array keys are fixture names, and the array values are either AR class names
* or table names. If table names, they must begin with a colon character (e.g. 'Post'
* means an AR class, while ':Post' means a table name).
+ * Defaults to false, meaning fixtures will not be used at all.
*/
- public $fixtures=array();
+ public $fixtures=false;
/**
* PHP magic method.
@@ -54,7 +55,7 @@ class CDbTestCase extends CTestCase
*/
public function __get($name)
{
- if(($rows=$this->getFixtureManager()->getRows($name))!==false)
+ if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
return $rows;
else
throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
@@ -69,7 +70,7 @@ public function __get($name)
*/
public function __call($name,$params)
{
- if(isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
+ if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
return $record;
else
throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
@@ -91,6 +92,7 @@ public function getFixtureManager()
protected function setUp()
{
parent::setUp();
- $this->getFixtureManager()->load($this->fixtures);
+ if(is_array($this->fixtures))
+ $this->getFixtureManager()->load($this->fixtures);
}
}
@@ -24,12 +24,13 @@
class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
{
/**
- * @var array a list of fixtures that should be loaded for all test cases.
+ * @var array a list of fixtures that should be loaded before each test method executes.
* The array keys are fixture names, and the array values are either AR class names
* or table names. If table names, they must begin with a colon character (e.g. 'Post'
* means an AR class, while ':Post' means a table name).
+ * Defaults to false, meaning fixtures will not be used at all.
*/
- public $fixtures=array();
+ public $fixtures=false;
/**
* PHP magic method.
@@ -39,7 +40,7 @@ class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
*/
public function __get($name)
{
- if(($rows=$this->getFixtureManager()->getRows($name))!==false)
+ if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
return $rows;
else
throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
@@ -54,7 +55,7 @@ public function __get($name)
*/
public function __call($name,$params)
{
- if(isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
+ if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
return $record;
else
return parent::__call($name,$params);
@@ -76,6 +77,7 @@ public function getFixtureManager()
protected function setUp()
{
parent::setUp();
- $this->getFixtureManager()->load($this->fixtures);
+ if(is_array($this->fixtures))
+ $this->getFixtureManager()->load($this->fixtures);
}
}

0 comments on commit c3bdff4

Please sign in to comment.