O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Tip: Having too many fixture files could increase the test time dramatically. For this<br />
reason, you should only provide fixture files for those tables whose content may<br />
change during the test. Tables that serve as look-ups <strong>do</strong> not change and thus <strong>do</strong> not<br />
need fixture files.<br />
In the next two sections, we will describe how to make use of the fixtures managed by<br />
CDbFixtureManager in unit tests and functional tests.<br />
Testes Unitários<br />
Because the <strong>Yii</strong> testing framework is built on top of PHPUnit, it is recommended that you<br />
go through thePHPUnit <strong>do</strong>cumentation first to get the basic understanding on how to write<br />
a unit test. We summarize in the following the basic principles of writing a unit test in <strong>Yii</strong>:<br />
• A unit test is written in terms of a class XyzTest which extends from CTestCase or<br />
CDbTestCase, whereXyz stands for the class being tested. For example, to test the<br />
Post class, we would name the corresponding unit test as PostTest by convention.<br />
The base class CTestCase is meant for generic unit tests, while CDbTestCase is<br />
suitable for testing active record m o d e l c l a s s e s .<br />
BecausePHPUnit_Framework_TestCase is the ancestor class for both classes, we<br />
can use all methods inherited from this class.<br />
• The unit test class is saved in a PHP file named as XyzTest.php. By convention, the<br />
unit test file may be stored under the directory protected/tests/unit.<br />
• The test class mainly contains a set of test methods named as testAbc, where Abc is<br />
often the name of the class method to be tested.<br />
• A test method usually contains a sequence of assertion statements (e.g. assertTrue,<br />
assertEquals) which serve as checkpoints on validating the behavior of the target<br />
class.<br />
In the following, we mainly describe how to write unit tests for active record model classes.<br />
We will extend our test classes from CDbTestCase because it provides the database<br />
fixture support that we introduced in the previous section.<br />
Assume we want to test the Comment model class in the blog demo. We start by creating<br />
a class namedCommentTest and saving it as protected/tests/unit/CommentTest.php:<br />
class CommentTest extends CDbTestCase<br />
{<br />
public $fixtures=array(<br />
'posts'=>'Post',<br />
'comments'=>'Comment',<br />
);<br />
}<br />
......