Because the Yii testing framework is built on PHPUnit, it is recommended that you read through the PHPUnit documentation before understanding how to write a unit test. Let's briefly summarize the basic principles of writing a unit test in Yii:
A unit test is written in the form of the XyzTest class inherited from CTestCase or CDbTestCase, where Xyz represents the test to be Class. For example, if we want to test the Post class, we will name the test class PostTest accordingly. The base class CTestCase is a general unit test class, while CDbTestCase is only suitable for testing AR model classes. Since PHPUnit_Framework_TestCase is the two The parent class of the class
, we can inherit all methods from this class.
Unit test classes are saved in PHP files in the form of XyzTest.php. For convenience, unit test files are usually saved in the protected/tests/unit folder
.
The test class mainly contains a series of testAbc methods, where Abc is usually the class method to be tested.
The test method usually contains a A series of assertion statements (e.g. assertTrue
, assertEquals
) serve as breakpoints to verify the behavior of the target class.
Below we mainly explain how to create AR Model class to write unit tests. Our test class will inherit from CDbTestCase because it provides database-specific state support. We have discussed database-specific state in detail in the previous chapter.
Suppose we want to test For the Comment model class in the blog case, you can first create a class named CommentTest, and then save it as protected/tests/unit/CommentTest.php
:
class CommentTest extends CDbTestCase { public $fixtures=array( 'posts'=>'Post', 'comments'=>'Comment', ); ...... }
in this class , we specify the member variable fixtures
as an array containing the specific state (fixtures) to be used for this test. This array represents the mapping from a specific state name to a model class or a specific state table name (e.g. from posts
to Post
). Note that when mapping to a specific state table name, it should be in the data Table names are prefixed with a colon (e.g. <img src="https://img.php.cn/upload/article/000/000/194/d8454295409f69752ae1c667ca9e3c2e-0.gif" alt="Yii Framework Official Guide Series Supplement 39 - Testing: Unit Testing" class="wp-smiley"> ost
) to distinguish them from mapping to model classes. When using model class names, the corresponding table will be treated as a specific state table. As we described before, a certain state table will be reset to some known state each time a test method is executed.
Specific state names allow us to access specific state data in a very convenient way in test methods. The following code shows typical usage:
// return all rows in the 'Comment' fixture table $comments = $this->comments; // return the row whose alias is 'sample1' in the `Post` fixture table $post = $this->posts['sample1']; // return the AR instance representing the 'sample1' fixture data row $post = $this->posts('sample1');
Note: If a specific status declaration uses its data table name (e.g.
'posts'=>'Yii Framework Official Guide Series Supplement 39 - Testing: Unit Testingost'
), then the third usage method above will be invalid because we have already There is no longer any information related to the model class.
Next, we have to write the testApprove method to test the approve method
in the Comment model class. The code is very simple and clear: First we Insert a comment to be reviewed; then verify this review comment by retrieving data from the database; finally we call the approve method and pass the review.
public function testApprove() { // insert a comment in pending status $comment=new Comment; $comment->setAttributes(array( 'content'=>'comment 1', 'status'=>Comment::STATUS_PENDING, 'createTime'=>time(), 'author'=>'me', 'email'=>[email protected]', 'postId'=>$this->posts['sample1']['id'], ),false); $this->assertTrue($comment->save(false)); // verify the comment is in pending status $comment=Comment::model()->findByPk($comment->id); $this->assertTrue($comment instanceof Comment); $this->assertEquals(Comment::STATUS_PENDING,$comment->status); // call approve() and verify the comment is in approved status $comment->approve(); $this->assertEquals(Comment::STATUS_APPROVED,$comment->status); $comment=Comment::model()->findByPk($comment->id); $this->assertEquals(Comment::STATUS_APPROVED,$comment->status); }
The above is the content of Yii Framework Official Guide Series Supplement 39 - Testing: Unit Testing. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!