W3cubDocs

/Yii 1.1

CDbTestCase

Package system.test
Inheritance abstract class CDbTestCase » CTestCase
Since 1.1
Source Code framework/test/CDbTestCase.php
CDbTestCase is the base class for test cases about DB-related features.

CDbTestCase provides database fixture management with the help of CDbFixtureManager. By declaring fixtures property, one can ensure the specified tables have the expected fixture state when executing each test method. In addition, CDbTestCase provides two ways to access the fixture data.

For example, assume we declare fixtures to be:
public $fixtures=array(
    'posts' => 'Post',
    'comments' => 'Comment',
);


We can access the original fixture data rows using $this->posts $this->posts['first post']. We can also retrieve an ActiveRecord instance corresponding to a fixture data row using $this->posts('first post'). Note, here 'first post' refers to a key to a row in the original fixture data.

Protected Properties

Property Type Description Defined By
fixtures array a list of fixtures that should be loaded before each test method executes. CDbTestCase

Public Methods

Method Description Defined By
__call() PHP magic method. CDbTestCase
__get() PHP magic method. CDbTestCase
getFixtureData() Returns the named fixture data CDbTestCase
getFixtureManager() Returns the database fixture manager CDbTestCase
getFixtureRecord() Returns the ActiveRecord instance corresponding to the specified alias in the named fixture. False is returned if there is no such fixture or the record cannot be found. CDbTestCase

Protected Methods

Method Description Defined By
setUp() Sets up the fixture before executing a test method. CDbTestCase

Property Details

fixtures property

protected array $fixtures;

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.

Method Details

__call() method

public mixed __call(string $name, string $params)
$name string method name
$params string method parameters
{return} mixed the property value
Source Code: framework/test/CDbTestCase.php#73 (show)
public function __call($name,$params)
{
    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)."'.");
}

PHP magic method. This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.

__get() method

public mixed __get(string $name)
$name string the property name
{return} mixed the property value
Source Code: framework/test/CDbTestCase.php#57 (show)
public function __get($name)
{
    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)."'.");
}

PHP magic method. This method is overridden so that named fixture data can be accessed like a normal property.

getFixtureData() method

public array getFixtureData(string $name)
$name string the fixture name (the key value in fixtures).
{return} array the named fixture data
Source Code: framework/test/CDbTestCase.php#93 (show)
public function getFixtureData($name)
{
    return 
$this->getFixtureManager()->getRows($name);
}

getFixtureManager() method

public CDbFixtureManager getFixtureManager()
{return} CDbFixtureManager the database fixture manager
Source Code: framework/test/CDbTestCase.php#84 (show)
public function getFixtureManager()
{
    return 
Yii::app()->getComponent('fixture');
}

getFixtureRecord() method

public CActiveRecord getFixtureRecord(string $name, string $alias)
$name string the fixture name (the key value in fixtures).
$alias string the alias of the fixture data row
{return} CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture. False is returned if there is no such fixture or the record cannot be found.
Source Code: framework/test/CDbTestCase.php#104 (show)
public function getFixtureRecord($name,$alias)
{
    return 
$this->getFixtureManager()->getRecord($name,$alias);
}

setUp() method

protected void setUp()
Source Code: framework/test/CDbTestCase.php#114 (show)
protected function setUp()
{
    
parent::setUp();
    if(
is_array($this->fixtures))
        
$this->getFixtureManager()->load($this->fixtures);
}

Sets up the fixture before executing a test method. If you override this method, make sure the parent implementation is invoked. Otherwise, the database fixtures will not be managed properly.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CDbTestCase