Thursday, 10 December 2015

PHPUnit Test : Write a Test Method Very Quickly For Your Actual Method!


I have written a single method 'testPhpUnit' inside Utility.php which will run 2 times through the data provider method 'providerTestPhpUnit'.

One will be succeeded and another will fail.

/**
 * @file
 * Unit tests (PHPUnit) to test your methods.
 */

/**
 * Class UnitTestUtilityValidate.
 */
class UnitTestUtilityValidate extends PHPUnit_Framework_TestCase {
/**
   * Test method.
   *
   * @param string $test_string
   *   The string to be tested.
   * @param string $expected_string
   *   The string we are expecting.
   *
   * @dataProvider providerTestPhpUnit
   */
  public function testPhpUnit($test_string, $expected_string) {
    $this->assertEquals($test_string, $expected_string);
  }
​
  /**
   * Data provider to testPhpUnit().
   *
   * @see testPhpUnit
   */
  public function providerTestPhpUnit() {
​
    
    $arr_csv_data[0][0] = xyz::actualMethodToBeTest('Hello');
    $arr_csv_data[0][1] = 'Hello';
​
    $arr_csv_data[1][0] = xyz::actualMethodToBeTest('Hello1');
    $arr_csv_data[1][1] = 'Hello2';
​
    return $arr_csv_data;
  }
}

/**
 * Class xyz.
 * 
 * Actual class where your actual method will be implemented.
 */
Class xyz {
  /**
   * Actual method to be tested.
   *
   * @param string $val
   *   The string we are testing.
   *
   * return string
   *   The output value that we need to test against the expected value.
   */
  public static function actualMethodToBeTest(string $val) {
    return $val;
  }
}


$ phpunit -c phpunit.xml --filter 'UnitTestUtilityValidate::testPhpUnit' UnitTestUtilityValidate /www/demodrupal/sites/all/modules/custom/utility/unit_tests/UnitTestUtilityValidate.php


Thursday, 27 March 2014

Installing and Configuring SonarQube for Drupal

Put your technical debt under control
SonarQube is an open source software quality management tool, dedicated to continuously analyze and measure source code quality. Using SonarQube throughout the whole development project life cycle drastically improves visibility for every stakeholder. This gained visibility allows to manage risks, reduce maintenance costs and improve agility by implementing a real quality first approach. Teams can now seamlessly embrace quality with fun.

Installing SonarQube
Download and unzip the SonarQube distribution 
wget http://dist.sonar.codehaus.org/sonarqube-4.0.zip -O /var/lib/sonar-4.0.zip
cd /var/lib/
unzip sonar-4.0.zip
mv sonarqube-4.0 sonar
cd /var/lib/sonar/conf/
vim sonar.properties
#----- Credentials
# Permissions to create tables and indexes must be granted to JDBC user.
# The schema must be created first. change this password according to your database password.
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url:
 jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
#----- Connection pool settings
sonar.jdbc.maxActive: 20
sonar.jdbc.maxIdle: 5
sonar.jdbc.minIdle: 2
sonar.jdbc.maxWait: 5000
sonar.jdbc.minEvictableIdleTimeMillis: 600000
sonar.jdbc.timeBetweenEvictionRunsMillis: 30000
# Delay (in seconds) between processing of notification queue
sonar.notifications.delay=60

Install Sonar PHP Plugin
cp sonar-php-plugin-0.2.jar /var/lib/sonar/extensions/plugins
Installing SonarQube Runner :
Follow the below steps for configuring SonarQube Runner to analyze your source code.
unzip sonar-runner-dist-2.3.zip
mv sonar-runner-2.3 /var/lib/sonar-runner/
vim sonar-runner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
#sonar.login=admin
#sonar.password=admin
N.B. Default sonar web user/password : admin/admin

Link your project
Go to your project's root directory and add a new file with this name sonar-project.properties and add the below text and change according to your project. In my case project root directory is /var/www/myproject
# Required metadata
sonar.projectKey=myproject
sonar.projectName=My project
sonar.projectVersion=
1.0-SNAPSHOT
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional. If not set, SonarQube starts looking for source code
# from the directory containing the sonar-project.properties file.
sonar.sources=profiles/myprofile
# The value of the property must be the key of the language.
sonar.language=php
# Encoding of the source code
sonar.sourceEncoding=UTF-8
# Additional parameters
sonar.my.property=value
After creating "sonar-project.properties" file run sonar-runner as shown below (make sure sonar is running) --
/var/lib/sonar-runner/bin/sonar-runner start -X
In case you are facing any issue then try with the below command --
/var/lib/sonar-runner/bin/sonar-runner -Dsonar.projectKey=myproject -Dsonar.projectName=MyProject -Dsonar.projectVersion=1.0-SNAPSHOT -Dsonar.sources=/var/www/myproject/profiles/myprofile -Dsonar.language=php -X
Start/Stop SonarQube
cd /var/lib/sonar/bin/linux-x86-64/
./sonar.sh stop
./sonar.sh start
Browse the results at http://<ip>:9000
N.B. SONAR is becoming SONARQUBE

Reference Pages


If this post is useful to you then please give a comments. Thanks.

Thursday, 15 November 2012

MVC or MVP ?

MVC and MVP both terms are correct but they have there own flavor. These are two different architectural pattern used in building a robust decoupled system in modern days. If you are building an application in a decoupled manner then definitely you will get following advantages --

1. It will decrease your maintenance cost.
2. Alteration and Modification will not hamper other modules thus making the developers life easy.
3. It will allow you to do parallel development and thus reducing the development time significantly.

and so on...

So basically MVC stands for Model View Controller and MVP stands for Model View Presenter.
The main difference between the two is how the manager (controller/presenter) sits in the overall architecture. 

MVC pattern puts the controller as the main ‘guy’ in charge for running the show. All application request comes through straight to the controller, and it will decide what to do with the request. One of the old patter used to achieve separation of concerns.

MVP (Supervising Controller) on the other hand, doesn't mind for the View to take on a bigger role. View is the first object instantiated in the execution pipeline, which then responsible for passing any events that happens on itself to the Presenter. Advanced form of MVC.




Individual responsibilities of each layers --

MVC : 

View:
1. Renders data
2. Receives events
3. Have basic validations
4. Can interact with Model directly

Controller:
1. Helps view in complex design making tasks
2. Communicate with model
3. Performs complex validation tasks

Model:
1. Communicate with Database Layer and send appropriate data to the requester (View/Controller)

MVP :

View: 
1. Renders data
2. Receives events
3. Have basic validations


Controller:
1. Helps view in complex design making tasks
2. Communicate with model
3. Performs complex validation tasks

Model:
1. Communicate with Database Layer and send appropriate data to the requester (Controller)



Interface


An interface has the following properties:
  • An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
  • An interface cannot be instantiated directly.
  • Interfaces can contain events, indexers, methods, and properties.
  • Interfaces contain no implementation of methods.
  • Classes and structs can implement more than one interface.
  • An interface itself can inherit from multiple interfaces.
     
Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions:
  • A class or struct can implement more than one interface.
  • When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations, as shown in the following example.
     
Example:
 
public class Car : IEquatable<Car>
{
    public string Make {get; set;}
    public string Model { get; set; }
    public string Year { get; set; }

    // Implementation of IEquatable<T> interface
    public bool Equals(Car car)
    {
        if (this.Make == car.Make &&
            this.Model == car.Model &&
            this.Year == car.Year)
        {
            return true;
        }
        else
            return false;
    }
}


The IEquatable(Of T) interface announces to the user of the object that the object can determine whether it is equal to other objects of the same type, and the user of the interface does not have to know how this is implemented.