Yii Framework official guide series supplement 39-Test: UnitTesting)

Source: Internet
Author: User
Because the Yii testing framework is built based on PHPUnit, we recommend that you read the PHPUnit document before understanding how to write a unit test. Next we will briefly summarize the basic principles for writing a unit test in Yii: a unit test...



Because the Yii testing framework is built based on PHPUnit, we recommend that you read the PHPUnit document before understanding how to write a unit test. Below we will briefly summarize the basic principles for writing a unit test in Yii:

  • A unit test is written in the form of XyzTest inherited from CTestCase or CDbTestCase. Xyz represents the class to be tested. for example, 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 applicable to the AR model Test class. becausePHPUnit_Framework_TestCase is the parent class of the two classes., We can inherit all methods from this class.

  • Unit test classes are stored in the php file in the form of XyzTest. php. for convenience, unit test files are usually stored inUnder the protected/tests/unit folder.

  • The test class mainly contains a series of testAbc methods, of which Abc is usually the class method to be tested.

  • The test method usually contains a series of asserted statements (e.g.assertTrue,assertEqualsAs a breakpoint to verify the target class behavior.

The following describes how to write unit tests for AR model classes. our test class will inherit from CDbTestCase because it provides support for specific database statuses. in the previous section, we have discussed in detail the specific database statuses.

Suppose we want to test the Comment model class in the blog case, we can first create a class named CommentTest, and then save itprotected/tests/unit/CommentTest.php:

class CommentTest extends CDbTestCase{    public $fixtures=array(        'posts'=>'Post',        'comments'=>'Comment',    );    ......}

In this class, we specify the member variablefixturesIt is an array containing the specific status (fixtures) used for this test. This array indicates the ing from a specific state name to a model class or a specific state table name (e.g.postsToPost). Note that when ing to a specific state table name, you should add the colon prefix (e.g. ost). When the model class name is used, the corresponding table will be considered as a specific state table. As we described earlier, when a test method is executed, a specific status table will be reset to some known states each time.

The specific status name allows us to access specific status data in a convenient way in the test method. the following code shows a typical usage method:

// 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 state declaration uses its data table name (e.g.'posts'=>':Post'), The third method will be invalid because we have no association information with the model class.

Next, we will writeTest the approve method in the Comment model class.The code is very simple and clear: first, we insert a comment to be reviewed; then, we pull the data from the database to verify this review comment; 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 Yii Framework official guide series Supplement Version 39-Test: Unit Testing content. For more information, see PHP Chinese network (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.