> YII 类库手册 > CDbTestCase
system.test
继承 abstract class CDbTestCase » CTestCase » PHPUnit_Framework_TestCase » PHPUnit_Framework_Assert
实现 PHPUnit_Framework_Test, Countable, PHPUnit_Framework_SelfDescribing
源自 1.1
版本 $Id: CDbTestCase.php 3426 2011-10-25 00:01:09Z alexander.makarow $
源码
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.

受保护属性

属性 类型 描述 定义在
backupGlobals boolean Enable or disable the backup and restoration of the $GLOBALS array. PHPUnit_Framework_TestCase
backupGlobalsBlacklist array PHPUnit_Framework_TestCase
backupStaticAttributes boolean Enable or disable the backup and restoration of static attributes. PHPUnit_Framework_TestCase
backupStaticAttributesBlacklist array PHPUnit_Framework_TestCase
fixtures array a list of fixtures that should be loaded before each test method executes. CDbTestCase
preserveGlobalState boolean Whether or not this test should preserve the global state when PHPUnit_Framework_TestCase
runTestInSeparateProcess boolean Whether or not this test is to be run in a separate PHP process. PHPUnit_Framework_TestCase

公共方法

方法 描述 定义在
__call() PHP magic method. CDbTestCase
__construct() Constructs a test case with the given name. PHPUnit_Framework_TestCase
__get() PHP magic method. CDbTestCase
addToAssertionCount() Adds a value to the assertion counter. PHPUnit_Framework_TestCase
any() Returns a matcher that matches when the method it is evaLuated for PHPUnit_Framework_TestCase
anything() Returns a PHPUnit_Framework_Constraint_IsAnything matcher object. PHPUnit_Framework_Assert
arrayHasKey() Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object. PHPUnit_Framework_Assert
assertArrayHasKey() Asserts that an array has a specified key. PHPUnit_Framework_Assert
assertArrayNotHasKey() Asserts that an array does not have a specified key. PHPUnit_Framework_Assert
assertAttributeContains() Asserts that a haystack that is stored in a static attribute of a class PHPUnit_Framework_Assert
assertAttributeContainsOnly() Asserts that a haystack that is stored in a static attribute of a class PHPUnit_Framework_Assert
assertAttributeEmpty() Asserts that a static attribute of a class or an attribute of an object PHPUnit_Framework_Assert
assertAttributeEquals() Asserts that a variable is equal to an attribute of an object. PHPUnit_Framework_Assert
assertAttributeGreaterThan() Asserts that an attribute is greater than another value. PHPUnit_Framework_Assert
assertAttributeGreaterThanOrEqual() Asserts that an attribute is greater than or equal to another value. PHPUnit_Framework_Assert
assertAttributeInstanceOf() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertAttributeInternalType() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertAttributeLessThan() Asserts that an attribute is smaller than another value. PHPUnit_Framework_Assert
assertAttributeLessThanOrEqual() Asserts that an attribute is smaller than or equal to another value. PHPUnit_Framework_Assert
assertAttributeNotContains() Asserts that a haystack that is stored in a static attribute of a class PHPUnit_Framework_Assert
assertAttributeNotContainsOnly() Asserts that a haystack that is stored in a static attribute of a class PHPUnit_Framework_Assert
assertAttributeNotEmpty() Asserts that a static attribute of a class or an attribute of an object PHPUnit_Framework_Assert
assertAttributeNotEquals() Asserts that a variable is not equal to an attribute of an object. PHPUnit_Framework_Assert
assertAttributeNotInstanceOf() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertAttributeNotInternalType() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertAttributeNotSame() Asserts that a variable and an attribute of an object do not have the PHPUnit_Framework_Assert
assertAttributeNotType() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertAttributeSame() Asserts that a variable and an attribute of an object have the same type PHPUnit_Framework_Assert
assertAttributeType() Asserts that an attribute is of a given type. PHPUnit_Framework_Assert
assertClasshasAttribute() Asserts that a class has a specified attribute. PHPUnit_Framework_Assert
assertClassHasStaticAttribute() Asserts that a class has a specified static attribute. PHPUnit_Framework_Assert
assertClassNotHasAttribute() Asserts that a class does not have a specified attribute. PHPUnit_Framework_Assert
assertClassNotHasStaticAttribute() Asserts that a class does not have a specified static attribute. PHPUnit_Framework_Assert
assertContains() Asserts that a haystack contains a needle. PHPUnit_Framework_Assert
assertContainsOnly() Asserts that a haystack contains only values of a given type. PHPUnit_Framework_Assert
assertEmpty() Asserts that a variable is empty. PHPUnit_Framework_Assert
assertEqualXMLStructure() Asserts that a hierarchy of DOMNodes matches. PHPUnit_Framework_Assert
assertEquals() Asserts that two variables are equal. PHPUnit_Framework_Assert
assertFalse() Asserts that a condition is false. PHPUnit_Framework_Assert
assertFileEquals() Asserts that the contents of one file is equal to the contents of another PHPUnit_Framework_Assert
assertFileExists() Asserts that a file exists. PHPUnit_Framework_Assert
assertFileNotEquals() Asserts that the contents of one file is not equal to the contents of PHPUnit_Framework_Assert
assertFileNotExists() Asserts that a file does not exist. PHPUnit_Framework_Assert
assertGreaterThan() Asserts that a value is greater than another value. PHPUnit_Framework_Assert
assertGreaterThanOrEqual() Asserts that a value is greater than or equal to another value. PHPUnit_Framework_Assert
assertInstanceOf() Asserts that a variable is of a given type. PHPUnit_Framework_Assert
assertInternalType() Asserts that a variable is of a given type. PHPUnit_Framework_Assert
assertLessThan() Asserts that a value is smaller than another value. PHPUnit_Framework_Assert
assertLessThanOrEqual() Asserts that a value is smaller than or equal to another value. PHPUnit_Framework_Assert
assertNotContains() Asserts that a haystack does not contain a needle. PHPUnit_Framework_Assert
assertNotContainsOnly() Asserts that a haystack does not contain only values of a given type. PHPUnit_Framework_Assert
assertNotEmpty() Asserts that a variable is not empty. PHPUnit_Framework_Assert
assertNotEquals() Asserts that two variables are not equal. PHPUnit_Framework_Assert
assertNotInstanceOf() Asserts that a variable is not of a given type. PHPUnit_Framework_Assert
assertNotInternalType() Asserts that a variable is not of a given type. PHPUnit_Framework_Assert
assertNotNull() Asserts that a variable is not NULL. PHPUnit_Framework_Assert
assertNotRegExp() Asserts that a string does not match a given regular expression. PHPUnit_Framework_Assert
assertNotSame() Asserts that two variables do not have the same type and value. PHPUnit_Framework_Assert
assertNotTag() This assertion is the exact opposite of assertTag(). PHPUnit_Framework_Assert
assertNotType() Asserts that a variable is not of a given type. PHPUnit_Framework_Assert
assertNull() Asserts that a variable is NULL. PHPUnit_Framework_Assert
assertObjectHasAttribute() Asserts that an object has a specified attribute. PHPUnit_Framework_Assert
assertObjectNotHasAttribute() Asserts that an object does not have a specified attribute. PHPUnit_Framework_Assert
assertRegExp() Asserts that a string matches a given regular expression. PHPUnit_Framework_Assert
assertSame() Asserts that two variables have the same type and value. PHPUnit_Framework_Assert
assertSelectCount() Assert the presence, absence, or count of elements in a document matching PHPUnit_Framework_Assert
assertSelectEquals() assertSelectEquals("#binder .name", "Chuck", true, $xml); // any? PHPUnit_Framework_Assert
assertSelectRegExp() assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any? PHPUnit_Framework_Assert
assertStringEndsNotWith() Asserts that a string ends not with a given prefix. PHPUnit_Framework_Assert
assertStringEndsWith() Asserts that a string ends with a given prefix. PHPUnit_Framework_Assert
assertStringEqualsFile() Asserts that the contents of a string is equal PHPUnit_Framework_Assert
assertStringMatchesFormat() Asserts that a string matches a given format string. PHPUnit_Framework_Assert
assertStringMatchesFormatFile() Asserts that a string matches a given format file. PHPUnit_Framework_Assert
assertStringNotEqualsFile() Asserts that the contents of a string is not equal PHPUnit_Framework_Assert
assertStringNotMatchesFormat() Asserts that a string does not match a given format string. PHPUnit_Framework_Assert
assertStringNotMatchesFormatFile() Asserts that a string does not match a given format string. PHPUnit_Framework_Assert
assertStringStartsNotWith() Asserts that a string starts not with a given prefix. PHPUnit_Framework_Assert
assertStringStartsWith() Asserts that a string starts with a given prefix. PHPUnit_Framework_Assert
assertTag() Evaluate an HTML or XML string and assert its structure and/or contents. PHPUnit_Framework_Assert
assertThat() Evaluates a PHPUnit_Framework_Constraint matcher object. PHPUnit_Framework_Assert
assertTrue() Asserts that a condition is true. PHPUnit_Framework_Assert
assertType() Asserts that a variable is of a given type. PHPUnit_Framework_Assert
assertXmlFileEqualsXmlFile() Asserts that two XML files are equal. PHPUnit_Framework_Assert
assertXmlFileNotEqualsXmlFile() Asserts that two XML files are not equal. PHPUnit_Framework_Assert
assertXmlStringEqualsXmlFile() Asserts that two XML documents are equal. PHPUnit_Framework_Assert
assertXmlStringEqualsXmlString() Asserts that two XML documents are equal. PHPUnit_Framework_Assert
assertXmlStringNotEqualsXmlFile() Asserts that two XML documents are not equal. PHPUnit_Framework_Assert
assertXmlStringNotEqualsXmlString() Asserts that two XML documents are not equal. PHPUnit_Framework_Assert
at() Returns a matcher that matches when the method it is evaluated for PHPUnit_Framework_TestCase
atLeastOnce() Returns a matcher that matches when the method it is evaluated for PHPUnit_Framework_TestCase
attribute() Returns a PHPUnit_Framework_Constraint_Attribute matcher object. PHPUnit_Framework_Assert
attributeEqualTo() Returns a PHPUnit_Framework_Constraint_IsEqual matcher object PHPUnit_Framework_Assert
classHasAttribute() Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object. PHPUnit_Framework_Assert
classHasStaticAttribute() Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher PHPUnit_Framework_Assert
contains() Returns a PHPUnit_Framework_Constraint_TraversableContains matcher PHPUnit_Framework_Assert
containsOnly() Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher PHPUnit_Framework_Assert
count() Counts the number of test cases executed by run(TestResult result). PHPUnit_Framework_TestCase
equalTo() Returns a PHPUnit_Framework_Constraint_IsEqual matcher object. PHPUnit_Framework_Assert
exactly() Returns a matcher that matches when the method it is evaluated for PHPUnit_Framework_TestCase
fail() Fails a test with the given message. PHPUnit_Framework_Assert
fileExists() Returns a PHPUnit_Framework_Constraint_FileExists matcher object. PHPUnit_Framework_Assert
getAnnotations() Returns the annotations for this test. PHPUnit_Framework_TestCase
getCount() Return the current assertion count. PHPUnit_Framework_Assert
getExpectedException() PHPUnit_Framework_TestCase
getFixtureData() 返回the named fixture data CDbTestCase
getFixtureManager() 返回the database fixture manager CDbTestCase
getFixtureRecord() 返回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
getMock() Returns a mock object for the specified class. PHPUnit_Framework_TestCase
getMockBuilder() Returns a builder object to create mock objects using a fluent interface. PHPUnit_Framework_TestCase
getMockForAbstractClass() Returns a mock object for the specified abstract class with all abstract PHPUnit_Framework_TestCase
getName() Gets the name of a TestCase. PHPUnit_Framework_TestCase
getNumAssertions() Returns the number of assertions performed by this test. PHPUnit_Framework_TestCase
getResult() PHPUnit_Framework_TestCase
getStatus() Returns the status of this test. PHPUnit_Framework_TestCase
getStatusMessage() Returns the status message of this test. PHPUnit_Framework_TestCase
getTestResultObject() PHPUnit_Framework_TestCase
greaterThan() Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object. PHPUnit_Framework_Assert
greaterThanOrEqual() Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps PHPUnit_Framework_Assert
hasFailed() Returns whether or not this test has failed. PHPUnit_Framework_TestCase
identicalTo() Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object. PHPUnit_Framework_Assert
isEmpty() Returns a PHPUnit_Framework_Constraint_IsEmpty matcher object. PHPUnit_Framework_Assert
isFalse() Returns a PHPUnit_Framework_Constraint_IsFalse matcher object. PHPUnit_Framework_Assert
isInstanceOf() Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object. PHPUnit_Framework_Assert
isNull() Returns a PHPUnit_Framework_Constraint_IsNull matcher object. PHPUnit_Framework_Assert
isTrue() Returns a PHPUnit_Framework_Constraint_IsTrue matcher object. PHPUnit_Framework_Assert
isType() Returns a PHPUnit_Framework_Constraint_IsType matcher object. PHPUnit_Framework_Assert
lessThan() Returns a PHPUnit_Framework_Constraint_LessThan matcher object. PHPUnit_Framework_Assert
lessThanOrEqual() Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps PHPUnit_Framework_Assert
logicalAnd() Returns a PHPUnit_Framework_Constraint_And matcher object. PHPUnit_Framework_Assert
logicalNot() Returns a PHPUnit_Framework_Constraint_Not matcher object. PHPUnit_Framework_Assert
logicalOr() Returns a PHPUnit_Framework_Constraint_Or matcher object. PHPUnit_Framework_Assert
logicalXor() Returns a PHPUnit_Framework_Constraint_Xor matcher object. PHPUnit_Framework_Assert
markTestIncomplete() Mark the test as incomplete. PHPUnit_Framework_Assert
markTestSkipped() Mark the test as skipped. PHPUnit_Framework_Assert
matches() Returns a PHPUnit_Framework_Constraint_StringMatches matcher object. PHPUnit_Framework_Assert
matchesRegularExpression() Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object. PHPUnit_Framework_Assert
never() Returns a matcher that matches when the method it is evaluated for PHPUnit_Framework_TestCase
objectHasAttribute() Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object. PHPUnit_Framework_Assert
onConsecutiveCalls() PHPUnit_Framework_TestCase
once() Returns a matcher that matches when the method it is evaluated for PHPUnit_Framework_TestCase
readAttribute() Returns the value of an attribute of a class or an object. PHPUnit_Framework_Assert
resetCount() Reset the assertion counter. PHPUnit_Framework_Assert
returnArgument() PHPUnit_Framework_TestCase
returnCallback() PHPUnit_Framework_TestCase
returnValue() PHPUnit_Framework_TestCase
run() Runs the test case and collects the results in a TestResult object. PHPUnit_Framework_TestCase
runBare() Runs the bare test sequence. PHPUnit_Framework_TestCase
setBackupGlobals() Calling this method in setUp() has no effect! PHPUnit_Framework_TestCase
setBackupStaticAttributes() Calling this method in setUp() has no effect! PHPUnit_Framework_TestCase
setDependencies() Sets the dependencies of a TestCase. PHPUnit_Framework_TestCase
setDependencyInput() Sets PHPUnit_Framework_TestCase
setExpectedException() 设置 PHPUnit_Framework_TestCase
setInIsolation() 设置 PHPUnit_Framework_TestCase
setName() Sets the name of a TestCase. PHPUnit_Framework_TestCase
setPreserveGlobalState() 设置 PHPUnit_Framework_TestCase
setResult() 设置 PHPUnit_Framework_TestCase
setRunTestInSeparateProcess() 设置 PHPUnit_Framework_TestCase
setUpBeforeClass() This method is called before the first test of this test class is run. PHPUnit_Framework_TestCase
setUseErrorHandler() 设置 PHPUnit_Framework_TestCase
setUseoutputBuffering() 设置 PHPUnit_Framework_TestCase
stringContains() Returns a PHPUnit_Framework_Constraint_StringContains matcher object. PHPUnit_Framework_Assert
stringEndsWith() Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object. PHPUnit_Framework_Assert
stringStartsWith() Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object. PHPUnit_Framework_Assert
syntheticFail() Fails a test with a synthetic error. PHPUnit_Framework_Assert
tearDownAfterClass() This method is called after the last test of this test class is run. PHPUnit_Framework_TestCase
throwException() PHPUnit_Framework_TestCase
toString() Returns a string representation of the test case. PHPUnit_Framework_TestCase

受保护方法

方法 描述 定义在
assertPostConditions() Performs assertions shared by all tests of a test case. PHPUnit_Framework_TestCase
assertPreConditions() Performs assertions shared by all tests of a test case. PHPUnit_Framework_TestCase
createResult() Creates a default TestResult object. PHPUnit_Framework_TestCase
dataToString() PHPUnit_Framework_TestCase
getDataSetAsString() Gets the data set description of a TestCase. PHPUnit_Framework_TestCase
getMockClass() Mocks the specified class and returns the name of the mocked class. PHPUnit_Framework_TestCase
getMockFromWsdl() Returns a mock object based on the given WSDL file. PHPUnit_Framework_TestCase
handleDependencies() PHPUnit_Framework_TestCase
iniSet() This method is a wrapper for the ini_set() function that automatically PHPUnit_Framework_TestCase
onNotSuccessfulTest() This method is called when a test method did not execute successfully. PHPUnit_Framework_TestCase
prepareTemplate() Performs custom preparations on the process isolation template. PHPUnit_Framework_TestCase
runTest() Override to run the test and assert its state. PHPUnit_Framework_TestCase
setExpectedExceptionFromAnnotation() PHPUnit_Framework_TestCase
setLocale() This method is a wrapper for the setlocale() function that automatically PHPUnit_Framework_TestCase
setUp() Sets up the fixture before executing a test method. CDbTestCase
setUseErrorHandlerFromAnnotation() PHPUnit_Framework_TestCase
setUseOutputBufferingFromAnnotation() PHPUnit_Framework_TestCase
tearDown() Tears down the fixture, for example, close a network connection. PHPUnit_Framework_TestCase
verifyMockObjects() Verifies the mock object expectations. PHPUnit_Framework_TestCase

属性详细

fixtures 属性
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.

方法详细

__call() 方法
public mixed __call(string $name, string $params)
$name string method name
$params string method parameters
{return} mixed the property value
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() 方法
public mixed __get(string $name)
$name string the property name
{return} mixed the property value
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() 方法
public array getFixtureData(string $name)
$name string the fixture name (the key value in fixtures).
{return} array the named fixture data
public function getFixtureData($name)
{
    return 
$this->getFixtureManager()->getRows($name);
}

getFixtureManager() 方法
public CDbFixtureManager getFixtureManager()
{return} CDbFixtureManager the database fixture manager
public function getFixtureManager()
{
    return 
Yii::app()->getComponent('fixture');
}

getFixtureRecord() 方法
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.
public function getFixtureRecord($name,$alias)
{
    return 
$this->getFixtureManager()->getRecord($name,$alias);
}

setUp() 方法
protected void setUp()
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.

上一篇:
下一篇: