Yii – Testing

  • Post author:
  • Post category:Yii
  • Post comments:1 Comment

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 application, 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\test\Fixture class.

To define a fixture, you should create a new class and extend it from yii\test\Fixture or yii\test\ActiveFixture. 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 individual 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\tests\codeception\fixtures;
   use yii\test\ActiveFixture;
   class ExampleFixture extends ActiveFixture {
      public $modelClass = β€˜appβŠ¨β€™MyUser';
   }
?>

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

<?php
   namespace tests\codeception\unit\models;
   use app\models\MyUser;
   use yii\codeception\TestCase;
   class ExampleTest extends TestCase {
      public function testCreateMyUser() {
         $m = new MyUser();
         $m->name = "myuser";
         $m->email = "myser@email.com";
         $this->assertTrue($m->save());
      }
      public function testUpdateMyUser() {
         $m = new MyUser();
         $m->name = "myuser2";
         $m->email = "myser2@email.com";
         $this->assertTrue($m->save());
         $this->assertEquals("myuser2", $m->name);
      }
      public 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 βˆ’

Functional Tests

Functional tests help you in βˆ’

  • testing the application 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 βˆ’

This Post Has One Comment

Leave a Reply