PHPUnit Testing

How to find the reason for a risky test in PHPUnit ?

Recently I started phpUnit testing, and I found some of the test are flagging as risky. Most of the time I able figure bout reason but some time it very time consuming and also there isn’t any messages from PHPUnit.

PHPUnit 4.4.5 by Sebastian Bergmann.

Configuration read from phpunit.xml.dist

R.................R.R..R

Time: 14,17 seconds, Memory: 52,50Mb

OK, but incomplete, skipped, or risky tests!
Tests: 24, Assertions: 52, Risky: 4.

Finally I found a soultion and it is very simple( How I can forgot about -v flag !! )

 phpunit -v|--verbose              Output more verbose information.

Stay tuned, more testing related post probably writing 🙂

Link

Reset global variables that grow out of control during imports

https://developer.wordpress.org/reference/classes/wp_importer/stop_the_insanity/

Check if session is started or not in php

To avoid Cannot send session cookie - headers already sent warning which is cause by calling session_start() function without checking if the session is already start or not.

You can find following warning in error log file :

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /xxx/xxx/abc.php:8) in /xxx/xxx/test.php on line 11

Recommended way for versions of PHP >= 5.4.0

if ( session_status() == PHP_SESSION_NONE ) {
    session_start();
}

Source: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

if ( session_id() == '' ) {
    session_start();
}

Get php class file path from class name – ReflectionClass

During development of common library for WordPress plugins and themes ( WordPress development helper library ), I just want to find from which plugin it is called ( I just want to debug the code, but I used in many plugins and i am not able to track from which plugin it is actually loading ).

Then  I found about ReflectionClass,  reports information about a class.

You can read more detail about ReflectionClass from php.net.

Here is how to get file path from class name

$autoloader_reflector = new ReflectionClass("RT_WP_Autoload");
$class_file_nanme = $autoloader_reflector->getFileName();
echo dirname($class_file_nanme);

This is same as reflection api in java, There are some good reflection examples to get you started reflection api in java at http://docs.oracle.com/javase/tutorial/reflect/index.html

Generic autoloader for classes named in WordPress coding style


if ( ! class_exists( 'RT_WP_Autoload' ) ){
 /**
  * Generic autoloader for classes named in WordPress coding style.
  */
  class RT_WP_Autoload {
    /**
     * @var string Current directory absolute path
     */
    public $dir;

    /**
     * @param string $dir optional, default value is current
     */
    function __construct( $dir = '' ) {
      if ( ! empty( $dir ) ){
        $this->dir = trailingslashit( $dir );
      } else {
        /** __DIR__ is actually equivalent to 
         * realpath(dirname(__FILE__)) and Added in PHP 5.3.0
         * Bellow line will also work 
         * for php  5.2.4 (WordPress required php  5.2.4 or greater )
         */
        $this->dir = trailingslashit( ( @__DIR__ == '__DIR__' ) && define( '__DIR__', realpath( dirname( __FILE__ ) ) ) );
      }
      spl_autoload_register( 
          array( $this, 'spl_autoload_register' ) );
     }

    /**
     * @param string $class_name
     */
    function spl_autoload_register( $class_name ) {
      $class_path = $this->dir . strtolower( 
       str_replace( '_', '-', $class_name ) ) . '/class-' 
       . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
      
      if ( file_exists( $class_path ) ){
        include $class_path;
      }
    }
  }
}

/**
 * Initiate Loader class
 */

$obj_rt_wp_loader = new RT_WP_Autoload();