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

Javascript Replace Function

var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)(x*)/, function(match, p1, p2, p3, offset, string) {
// p1 is nondigits, p2 digits, and p3 non-alphanumerics
return [match, p1, p2, p3, offset, string].join(' - ');
});
console.log(newString);

jQuery, Array IndexOf function and FadeIn and FadeOut!

if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length;

var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0)
from += len;

for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}

jQuery.fn.fadeIn = function()
{
this.show();
}

jQuery.fn.fadeOut = function()
{
this.hide();
}

Import a large sql dump file to a MySQL database from command line

cmanios

Today I had to import a very large SQL dump file (6 Gb) to a MySQL database using windows command line. If you are using linux it is the same. The process is the following:

  1. Open a command prompt (or shell in Linux) with administrative privilleges
  2. If you are in Windows set character set to unicode. Linux is using UTF-8 by default.
    chcp 65001
  3. Connect to a mysql instance using command line
    $PATH_TO_MYSQLmysql.exe -h 192.168.1.1 --port=3306 -u root -p

    if you are in localhost you do not need host and port

    $PATH_TO_MYSQLmysql.exe -u root -p
  4. You are now in mysql shell. Set network buffer length to a large byte number. The default value may throw errors for such large data files
    set global net_buffer_length=1000000;
  5. Set maximum allowed packet size to a large byte number.The default value may throw errors for such large data files.
    set global max_allowed_packet=1000000000;
  6. Disable foreign key…

View original post 78 more words

How to add/set Environment Variable in Ubuntu

There are two ways to set Environment Variables in Ubuntu.

1.    This method will add/set environment variable just for current user session. For example you want to add “/home/imrankabir/.composer/vendor/bin/” to your environment variables.

export PATH=${PATH}:/home/imrankabir/.composer/vendor/bin/

2.    To add/set environment variable permanently, open .profile hidden file in your ubuntu home directory. This will look some thing like this

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n “$BASH_VERSION” ]; then
# include .bashrc if it exists
if [ -f “$HOME/.bashrc” ]; then
. “$HOME/.bashrc”
fi
fi

# set PATH so it includes user’s private bin if it exists
if [ -d “$HOME/bin” ] ; then
PATH=”$HOME/bin:$PATH”
fi

Now add following line to this file (may be before “# if running bash” commented line) and save the file.

PATH=$PATH:/home/imrankabir/.composer/vendor/bin

Enjoy!