Yii Framework Official Guide Series Supplemental version 39--test: Unit Test units testing

Source: Internet
Author: User
Tags yii



Because the YII test framework is built on PHPUnit, it is recommended that you read through the PHPUnit document before you understand 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 a Xyztest class that inherits from Ctestcase or Cdbtestcase, where 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 generic unit test class, and cdbtestcase only applies to the test AR model class. As a result PHPUnit_Framework_TestCase是这两个类的父类 , we can inherit all the methods from this class.

    • Unit test classes are stored in PHP files as xyztest.php. For convenience, unit test files are usually saved in the protected/tests/unit文件夹下 .

    • The test class consists primarily of a series of TESTABC methods, where ABC is usually the class method to be tested.

    • The test method typically contains a series of assertion statements (e.g. assertTrue , assertEquals ) as breakpoints that validate the behavior of the target class.

Below we mainly describe how to write unit tests for AR model classes. Our test class will inherit from Cdbtestcase because it provides database-specific state support, and in the previous section we discussed the database-specific state in detail.

Suppose we want to test the comment model class in a 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 of specific states (fixtures) to be used for this test. This array represents a mapping from a specific state name to a model class or a specific state table name (e.g. posts Post ). Note When mapping to a specific state table name, you should precede the data table name with a colon prefix (e.g. ost ) to distinguish it from mapping to the model class. When using the model class name, The corresponding table will be treated as a specific state table. As we described earlier, when a test method executes, a particular state table will be reset to some known state each time.

A specific state name allows us to access specific state data in a convenient way in the test method. The following code shows a typical use 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 ' Fixtur E Data Row$post = $this->posts (' sample1 ');

Note: If a particular state declaration uses its data table name (e.g. 'posts'=>':Post' ), then the third use method above will be invalid because we already have no information associated with the model class.

Next, we're going to write testApprove方法在Comment模型类中测试approve方法 . The code is very straightforward: first we insert a comment to be reviewed, and then we validate the audit comment by extracting the data from the database, and finally we call the approve method and pass the audit.

Public Function Testapprove () {//Insert a comment in Pending status $comment =new comment; $comment->setattributes (' content ' = ' comment 1 ', ' status ' =>comment::status_pending, ' C Reatetime ' =>time (), ' author ' = ' Me ', ' email ' =>[email protected] ', ' PostID ' + $this->post    s[' 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 are 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 39--test: Unit Test testing content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.