Category: PHP 5

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:

 class DummyTest extends CTestCase{
   public function testTrue(){
     $var = true;

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.


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

How to ace a PHP job test in South Africa

Initial Preparation

First of all do a project in PHP that utilises the database. I am talking about pure PHP and not a framework. Remember to turn make sure you set the following configuration settings.

display_errors = 1;

You can always view the php documentation which is always of great help.

 Get ready for some specialised questions

The off-the-wall questions

  • What do you think of garden gnomes? Your answer should include some form of analysis and deduction.
  • Why are man-hole covers a circle? The answer is because it is not possible for the cover to fall into the hole that way.

PHP Specific Questions

Increment and Decrement Operators

Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

Question 1: ++$a * $a++ + $a = ? (Where $a = 5)

Answer 1: 6 * 5 + 5 = 35

Bitwise Operators

Example Name Result
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")

Question 2: echo 5 & 7;

Answer 2:

5 = 0101
7 = 0111

Now AND the bits

Answer = 0101 (5)

Question 3: echo 12 ^ 9;

Answer 3:

12 = 1100
9  = 1001
Now XOR the bits
Answer = 0101 (5)

Question 4:$x = 5; echo $x >> 2;
Answer 4:

5 = 0101
Shift bits 2 steps to the right

Answer = 0001 (1)

Learn some functions of the PHP library

array_shift  - Shifts an element off the beginning of an array

array_combine - Creates an associative array by using 1 for keys and 1 for values

final keyword - Prevents overriding and extension of the class

interfaces - Interfaces specify which methods a class must implement, but does not specify the code for the methods.

suppression of errors

Learn some permutations, combinations and factorial

I'm not good at these and if anyone has a tutorial or content to add please free to comment.

Here are some resources that may help, if you find out how to do these please comment below.

Circular Permutations

Example Permutation and Combination Question

Maths is Fun

If ABCDEFGH are sitting at a table, where the configuration of the seats is 1 at each head of the table, and 3 on either side. If A can't sit next to B and F can't sit G, how many different configurations are there?

How many different numberplates can be made, if the numberplate must contain only digits and no signle digit can be repeated if there are 5 spaces for digits on the numberplate?

Please leave comments if you have anything else to add.

PHP job test

PHP echo does not work on Server to Test

So you are trying to test a solo PHP script on a webserver and it is not working?

It is not printing anything at all.

Here is a checklist to help you sort it out:

1. Make sure your server supports PHP

2. Make sure the PHP module is enabled

3. Make sure the script filename has no gaps and is named .php

4. If you view source and can see PHP tags you know PHP is not enabled

5. Make sure that the script has closing tags as well:

<?php ....your code.... ?>

6. Try: