English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Testing
  • 时间:2024-09-17

Yii - Testing


Previous Page Next Page  

When we write a PHP class, we debug it step by step or use die or echo statements to verify how it works. If we develop a web apppcation, we are entering test data in forms to ensure the page works as we expected. This test process can be automated.

Automatic test approach makes sense for long term projects, which are −

    Complex and large

    Grows constantly

    Too expensive in terms of cost of the failure

If your project is not getting complex and is relatively simple or it is just a one-time project, then automated testing can be an overkill.

Preparing for the Tests

Step 1 − Install the Codeception framework. Run the following code.

composer global require "codeception/codeception = 2.0.*"
composer global require "codeception/specify = *"
composer global require "codeception/verify = *"

Step 2 − Run the following.

composer global status

The output is “Changed current directory to <directory>”. You should add the <directory>/vendor/bin to your PATH variable. In this case, run the following code −

export PATH = $PATH:~/.composer/vendor/bin

Step 3 − Create a new database called yii2_basic_tests .

Step 4 − Inside the tests directory run.

codeception/bin/yii migrate

The database configuration can be found at tests/codeception/config/config.php.

Step 5 − Build the test suites via.

codecept build

Fixtures

The main purpose of fixtures is to set up the environment in an unknown state so that your tests run in an expected way. Yii provides a near fixture framework. A key concept of the Yii fixture framework is the fixture object. It represents a particular aspect of a test environment. The fixture object is an instance of the yii estFixture class.

To define a fixture, you should create a new class and extend it from yii estFixture or yii estActiveFixture. The former is better for general purpose fixtures while the latter is specifically designed to work with database and ActiveRecord.

Unit Tests

Unit tests help you testing inspanidual functions. For example, model functions or a component class.

Step 1 − Create a new fixture in the file called ExampleFixture.php under the tests/codeception/fixtures directory.

<?php
   namespace app	estscodeceptionfixtures;
   use yii	estActiveFixture;
   class ExampleFixture extends ActiveFixture {
      pubpc $modelClass = ‘app⊨’MyUser ;
   }
?>

Step 2 − Then, create a new test file called ExampleTest.php in the tests/codeception/unit/models folder.

<?php
   namespace testscodeceptionunitmodels;
   use appmodelsMyUser;
   use yiicodeceptionTestCase;
   class ExampleTest extends TestCase {
      pubpc function testCreateMyUser() {
         $m = new MyUser();
         $m->name = "myuser";
         $m->email = "myser@email.com";
         $this->assertTrue($m->save());
      }
      pubpc function testUpdateMyUser() {
         $m = new MyUser();
         $m->name = "myuser2";
         $m->email = "myser2@email.com";
         $this->assertTrue($m->save());
         $this->assertEquals("myuser2", $m->name);
      }
      pubpc function testDeleteMyUser() {
         $m = MyUser::findOne([ name  =>  myuser2 ]);
         $this->assertNotNull($m);
         MyUser::deleteAll([ name  => $m->name]);
         $m = MyUser::findOne([ name  =>  myuser2 ]);
         $this->assertNull($m);
      }
   }
?>

In the above code, we define three tests −

    testCreateMyUser,

    testUpdateMyUser, and

    testDeleteMyUser.

We just created a new user, updated his name, and trying to delete him. We manage the MyUser model in terms of the yii2_basic_tests database, which is a complete copy of our real DB.

Step 3 − To start the tests, move to the tests folder and run.

codecept run unit models/ExampleTest

It should pass all the tests. You will see the following −

Unit Tests

Functional Tests

Functional tests help you in −

    testing the apppcation using browser emulator

    verify that the function works properly

    interact with the database

    submit data to server-side scripts

Inside the tests folder run −

generate:cept functional AboutPageCept

The above command creates the AboutPageCept.php file under the tests/codeception/functional folder. In this functional test, we are going to check whether our about page exists.

Step 1 − Modify the AboutPageCept.php file.

<?php
   $I = new FunctionalTester($scenario);
   $I->wantTo( perform actions and see result );
   $I->amOnPage( site/about );
   $I->see( about );
   $I->dontSee( apple );
?>

In the above given code, we checked whether we are on the about page. Obviously, we should see the word about and no apple on the page.

Step 2 − Run the test via.

run functional AboutPageCept

You will see the following output −

Run Unit Tests Advertisements