PHPUnit Test, PHPDocs, and PHP Standards Test with Lint

How to use Unit Test in PHP, PHP Docs and Standard Test by Lint.

Arithmetic.php

<?php

/**
 * This file contain Arithmetic class
 */

namespace imrankabir;

/**
 * This class is to perform basic arithmetic operations
 *
 * @package Arithmetic
 * @author Imran Kabir <imran.kabir@rolustech.net>
 * @copyright (c) 2016, Imran Kabir
 * @license http://rolustech.net Rolustech
 * @link http://rolustech.com Rolustech.com
 */

class Arithmetic
{
    
    /**
     * To add two values
     * 
     * @param int $a
     * @param int $b
     * @return int
     */
    
    public function add($a, $b)
    {
        return $a + $b;
    }
    
    /**
     * To subtract two values
     * 
     * @param int $a
     * @param int $b
     * @return int
     */
    
    public function sub($a, $b)
    {
        return $a - $b;
    }
    
    /**
     * To Multiply two values
     * 
     * @param int $a
     * @param int $b
     * @return int
     */
    
    public function multiply($a, $b)
    {
        return $a * $b;
    }
    
    /**
     * To divide two values
     * 
     * @param int $a
     * @param int $b
     * @return int
     */
    
    public function divide($a, $b)
    {
        return $a / $b;
    }
}

ArithmeticTest.php

<?php

namespace imrankabir; 

use PHPUnit_Framework_TestCase as TestCase; 
use imrankabir; 

class ArithmeticTest extends TestCase 
{ 
    public function testAdd() 
    { 
         $a = new Arithmetic();
         
         $this->assertEquals(10, $a->add(4, 6));
    }
    
    public function testSub()
    {
        $a = new Arithmetic();
        
        $this->assertEquals(-2, $a->sub(4, 6));
    }
    
    public function testMul()
    {
        $a = new Arithmetic();
        
        $this->assertEquals(24, $a->multiply(4, 6));
    }
    
    public function testDivide()
    {
        $a = new Arithmetic();
        
        $this->assertEquals(2, $a->divide(4, 2));
    }
}

To check Coding Conventions: Lint

Installation commands
sudo apt-get install php-pear
sudo apt-get install php-codesniffer

Test Command
phpcs –standard=PSR2 Arithmetic.php

PHPUnit

phpunit –bootstrap Arithmetic.php tests/ArithmeticTest.php

PHPDocs

phpdoc -f Arithmetic.php -t docs

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s