Yii Framework Official Guide Series 38 - Defining Specific States (Fixtures)

黄舟
Release: 2023-03-05 18:36:01
Original
1340 people have browsed it



Automated tests need to be executed many times. In order to ensure that the test process is repeatable, we really want to test in some known states , this state we call Specific state. For example, when testing the article creation feature in a blog application, every time we perform the test, the table related to the article (for example. Post table, Comment table) should be restored to a specific state. The PHPUnit documentation has a good description of the general construction of a specific state. This section mainly introduces how to create a specific state like the example just described. In this way, you can build a specific state of the database.

Setting the specific state of building the database is probably one of the most time-consuming parts of testing applications with database backend support. The CBbFixtureManager application component introduced by Yii can effectively alleviate this problem. One question. When running a set of tests, it basically does the following:

  • Before all tests are run, it resets the test-related data to a knowable state.

  • Before a single test is run, it resets specific tables to a knowable state.

  • During the execution of a test method, it provides An access interface that provides row data in a specific state.

Please use the CDbFixtureManager we configured in the application configuration as follows.


return array(
    'components'=>array(
        'fixture'=>array(
            'class'=>'system.test.CDbFixtureManager',
        ),
    ),
);
Copy after login

Then we provide a specific state data under the directory protected/tests/fixtures. This directory can be configured by CDbFixtureManager::basePath in the application configuration file Properties are specified as other directories. State-specific data is composed of multiple PHP files called state-specific files. Each state-specific file returns an array representing the initial row of a specific table of data. The file name is the same as the table name. .The following is an example of storing specific status data of the Post table in a file named Post.php.


<?php
return array(
    &#39;sample1&#39;=>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,
    ),
);
Copy after login

As we can see, the above returns two rows of data. Each row represents an array, the key of which is the field name of the table, and the value of which is the corresponding field value. Each row represents an array. The row indices are all strings called row alias (for example: simple1, simple2). Later when we write the test script, we This row of data can be conveniently called by its alias. We will cover this in detail in the next section.

You may have noticed that we did not specify the id field in the above specific state Value. This is because the id field has been defined as an auto-incrementing primary key, and its value will also be automatically generated when we insert new data.

When CDbFixtureManager is referenced for the first time , it goes through all the specific state files and uses them to reset the corresponding table. It does this by clearing the table, resetting the auto-increment sequence of the table's primary key, and then inserting rows from the specific state file into the table. Table.

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

It's okay if you don't like to use the default way to reset the table, for example: clear the table and then insert specific state data. If this is the case, we can specify Write an initialization script for a specific state file. This script must be named table name + .init.php. For example: The initialization script file for the Post table is Post.init. php. When CDbFixtureManager finds this script, it will execute this script instead of using the default method to reset the table.

Tip: Too many specifics State files greatly extend test time. Therefore, you should only provide specific state files for tables whose data will change during the test. Tables that serve as lookup services will not change and therefore do not require specific state files.

In the next two sections, we will talk about how to use specific states managed by CDbFixtureManager in unit tests and functional tests.

The above is the Yii Framework Official Guide Series 38 - Defining Specific States (Fixtures) content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template