PHPUnit

Installation on Debian/Unix:

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

Or install from composer,

{
    "require-dev": {
        "phpunit/phpunit": "^5.4"
    }
}

OR
composer global require-dev phpunit/phpunit

For global access,

composer global install

Files will be downloaded to ~/.composer/vendor/bin/
So, we need to set the PATH variable for global access,

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

For update package,

composer global update

How phpunit works in Yii:

All the test files need to be follow the unique naming convension,
file name should be end up with 'Test' : databaseTest.php
class name shoud be the same as filename and it should extends from phpunit testcase class,

ex:
include_once  ('framework/database.php'); - The original class file

class databaseTest extends PHPUnit_Framework_TestCase {

protected $connection;

function setup  () {

$this->connection = new database ();
}

function testDbConnection  () {

$this->connection->connect (DATABAE_INFO);
$objCreated = $this->connection->getDb ();

$this->assertNotEquals ($objCreated, 'null');
}
}

phpunit first run the setup function and then look for the all functions start from 'test' to execute.

Run test cases:

phpunit command always look instructions from phpunit.xml file unless we specify the

test file which need to execute. When phpunit run from the project root,

phpunit tests/databaseTest.php

OR run all the test files,

phpunit tests
phpunit

sample phpunit.xml:
     
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./phpUnitTutorial/Test/</directory>
        </testsuite>
    </testsuites>
</phpunit>

assertions available at:
http://phpunit.de/manual/4.1/en/appendixes.assertions.html

Popular posts from this blog

Kubernetes for Micro Services

Laravel 5.3

Node-RED