From c3bdff423fefbcfe5d43120d1eba9d30be5c8a08 Mon Sep 17 00:00:00 2001 From: "qiang.xue" Date: Fri, 12 Jun 2009 12:53:43 +0000 Subject: [PATCH] * Added phpunit-based testing framework. --- CHANGELOG | 1 + framework/test/CDbFixtureManager.php | 20 +++++++++++--------- framework/test/CDbTestCase.php | 12 +++++++----- framework/test/CWebTestCase.php | 12 +++++++----- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3c2930d93..2b2dab460 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 ---------------------------- diff --git a/framework/test/CDbFixtureManager.php b/framework/test/CDbFixtureManager.php index 2ad1cf556..2d54bd510 100644 --- a/framework/test/CDbFixtureManager.php +++ b/framework/test/CDbFixtureManager.php @@ -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 * @version $Id$ @@ -79,6 +80,7 @@ class CDbFixtureManager extends CApplicationComponent parent::init(); if($this->basePath===null) $this->basePath=Yii::getPathOfAlias('application.tests.fixtures'); + $this->prepare(); } /** @@ -99,9 +101,8 @@ class CDbFixtureManager extends CApplicationComponent /** * 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 @@ class CDbFixtureManager extends CApplicationComponent 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); } diff --git a/framework/test/CDbTestCase.php b/framework/test/CDbTestCase.php index 0972de7e8..c9f2b8f5e 100644 --- a/framework/test/CDbTestCase.php +++ b/framework/test/CDbTestCase.php @@ -39,12 +39,13 @@ Yii::import('system.test.CTestCase'); 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 @@ class CDbTestCase extends CTestCase */ 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 @@ class CDbTestCase extends CTestCase protected function setUp() { parent::setUp(); - $this->getFixtureManager()->load($this->fixtures); + if(is_array($this->fixtures)) + $this->getFixtureManager()->load($this->fixtures); } } \ No newline at end of file diff --git a/framework/test/CWebTestCase.php b/framework/test/CWebTestCase.php index 4e0f3a9a8..4f39289ef 100644 --- a/framework/test/CWebTestCase.php +++ b/framework/test/CWebTestCase.php @@ -24,12 +24,13 @@ require_once('PHPUnit/Extensions/SeleniumTestCase.php'); 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 @@ class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase */ 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 @@ class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase protected function setUp() { parent::setUp(); - $this->getFixtureManager()->load($this->fixtures); + if(is_array($this->fixtures)) + $this->getFixtureManager()->load($this->fixtures); } }