Yii Framework Official Guide Series 38--define a specific state (fixtures)

Source: Internet
Author: User
Tags yii



Automated tests need to be executed many times. To ensure that the testing process is repeatable, we want to test it in some known state, which we call a specific state . For example, testing an article creation feature in a blog application, each time we test, the table associated with the article (for example, table Post , table Comment ) should be restored to a specific state. The PHPUnit documentation is a good description of the general construction of a particular state. This section focuses on how to build a database-specific state like the example just described.

Setting the specific state of the build database is probably one of the most time-consuming parts of testing an application with a database for back-end support. The Cbbfixturemanager application component introduced by YII can alleviate this problem effectively. When a set of tests is carried out, it basically does the following things:

    • It resets the test-related data to a known state before all tests are run.

    • Before a single test runs, it resets the specific table to the known state.

    • During the execution of a test method, it provides the provider of the row data that is supplied to a particular state.

Please use the Cdbfixturemanager that we configured in the application configuration as follows.


Return Array (' Components    ' =>array ('        fixture ' =>array ('            class ' = ') ' System.test.CDbFixtureManager ',        ),    );

We then protected/tests/fixtures provide a specific status data under the directory. This directory can be configured by configuring the Cdbfixturemanager::basepath in the application configuration file property to a different directory. Specific state data is a combination of multiple PHP files called a specific state file. Each specific state file returns an array that represents the initial row of a particular table of data. The file name and table name are the same. The following is the Post storage of the table's specific state data in Post.php the The example in the file.


<?phpreturn Array ('    sample1 ' =>array ('        title ' = ' = ' Test post 1 ',        ' content ' = ' test post content 1 ',        ' createtime ' =>1230952187,        ' Authorid ' =>1, ' sample2 '    =>array (        ' title ' = ' = ') Test post 2 ',        ' content ' = ' test post Content 2 ',        ' createtime ' =>1230952287,        ' Authorid ' =>1,    ),);

As we can see, there are two rows of data returned. Each row represents an array whose key is the field name of the table, and its value is the corresponding field value. The index of each row is a string called a row alias (for example: simple1 , simple2 ). Later, when we wrote the test script, We can easily invoke this line of data through its aliases. We'll cover this in more detail in the next section.

You may have noticed. We did not specify the value of the field in the specific state described above id . This is because the id field has been defined as the self-increment primary key, and its value is automatically generated when we insert new data.

When Cdbfixturemanager is first referenced, it examines all of the specific state files and then uses them to reset the corresponding table. It resets the table by emptying the table, resetting the value of the table primary Key's self-increment sequence, and then inserting the data rows from the specific state file into the table.

Sometimes, we may not want to reset each table described in a particular state file before a set of tests, because it can take a lot of time to reset too many specific state files. In this case, we can write a PHP script to customize this initialization process. This should be stored in a directory that holds a specific state file. and named init.php . When Cdbfixturemanager detects the existence of this script, it executes the script instead of resetting each table.

It is also possible not to use the default method to reset the table, for example, emptying the table and inserting specific state data. If this is the case, we can write an initialization script for the specified state file. This script must be named Table name + .init.php . For example: Post the initialization script file for the table is Post.init.php . When Cdbfixturemanager found this script, It will execute the script instead of resetting the table in the default way.

Tip: Too many specific state files greatly extend the test time. Therefore, you should only provide specific state files for those tables in the test where the data will change. The tables that you do as lookup services do not change, so you do not need a specific state file.

In the next two sections, we'll talk about how to use specific states that are managed by Cdbfixturemanager in unit and functional testing.

The above is the Yii Framework Official Guide Series 38--definition specific status (fixtures) content, more relevant content please focus on topic.alibabacloud.com (www.php.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.