Month: August 2015

Implement Testing on Yii Framework 1

How to Implement Testing on Yii Framework 1

So you’ve finally realised to verify the quality of the code in releases and make sure everything is working before you deploy to production you need to test.

First we’ll use phpunit for unit testing.

Installing PHPUnit with Composer

We will only handle installing the required packages with composer because PEAR and PHP Archives are old news. Check out composer first, if you haven’t used it.

Before we get going make sure your composer is updated


composer self-update

First of all it is easier to use the global composer and install phpunit globally:


composer global require "phpunit/phpunit=4.8.*"
composer global require "phpunit/phpunit-selenium": ">=1.2"

But make sure you update composer.json


 {
      "require": {
          "php": ">=5.3.2",
          "yiisoft/yii": "1.1.*",
          "yiiext/migrate-command": "*",
          "phpunit/phpunit": "4.8.*",
          "phpunit/phpunit-selenium": ">=1.2"
      }
  }

But you will still get warnings like:

PHP Warning:  include(PHP_Invoker.php): failed to open stream: No such file or directory in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432
PHP Warning:  include(): Failed opening 'PHP_Invoker.php' for inclusion in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432
PHP Warning:  include(PHPUnit_Extensions_Database_TestCase.php): failed to open stream: No such file or directory in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432
PHP Warning:  include(): Failed opening 'PHPUnit_Extensions_Database_TestCase.php' for inclusion in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432
PHP Warning:  include(PHPUnit_Extensions_Story_TestCase.php): failed to open stream: No such file or directory in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432
PHP Warning:  include(): Failed opening 'PHPUnit_Extensions_Story_TestCase.php' for inclusion in /var/www/site/vendor/yiisoft/yii/framework/YiiBase.php on line 432

So we actually need more stuff, namely dbunit, phpunit-story and php-invoker:
So add the following to composer.json


 "phpunit/php-invoker": "*",
 "phpunit/dbunit": ">=1.2",
 "phpunit/phpunit-story": "1.*"

If you installed globally you need to install using:

composer global require {package}

But when running the tests from a task runner or continuous integration like jenkins, it is best to use the path to the composer phpunit in vendor.

Installed, Now let us write our first unit test

The folder where the test suite (class consisting of test functions) is <code>/protected/tests/unit</code>.

The file name needs to end in Test.php so for a test class: DummyTest.php.

Now the contents of the file:

<?php
 class DummyTest extends CTestCase{
   public function testTrue(){
     $var = true;
     $this->assertTrue($var);
   }
 }
?>

Running the Test

To run the tests you need to be within the test folder:


cd protected/tests
//run all the tests
../../../vendor/bin/phpunit unit
//run the single test suite
../../../vendor/bin/phpunit unit/DummyTest.php

The test results (output) is not the best.
A single dot . means a test passed.
A letter F means the test failed.

Setting Things up and Tearing things Down

Usually you will need certain things to happen before running a test. Such as: instantiation of an object or a connection to a database. To this we use setUp(). Which is a method called once before each test. Often calling the parent setUp function is good practice. Conversely, callingtearDown() is run after a test is run to free up resources.

setUp() and tearDown()

are run before and after each test case whether or not the test succeeds.

There are also

setUpBeforeClass() and tearDownAfterCall()

which are only called once regardless of test cases.

Fixtures

What are Fixtures? They are a state of being of the test. They are mainly used when databases are involved.