How to install PHPUnit on shared host

PHPUnit is a great framework for unit testing. It comes with PEAR which is quite easy to install if you own your own server but can be a headache if you are on a shared host. You need a SSH access to your server for this tutorial (French OVH pro shared hosting provides it)

First PEAR

Follow these steps if you don’t have PEAR installed

  1. Download a copy of http://pear.php.net/go-pear.phar and put it in a /pear/download directory on your server using FTP
  2. Using SSH navigate to this directory and execute this command : php go-pear.phar If you get any warning about wrong or missing signature try to use this command : php – d phar.require_hash=0 go-pear.phar
  3. If the PHP version is < 5.3.0, you’ll probably get an error. Execute cd /usr/local/bin and then dir. It’ll show you different versions of PHP installed on the server. You can pick the one you want. Go back to /pear/download and replace php by the right one in the previous command (step 2)
  4. Answer the question and set the configuration to the right directories : almost all should start by /pear except php binaries and some others

 Second check PEAR config

The second step is to check PEAR config using pear config-show. PEAR folders should start by /pear otherwise you’ll get an error when trying to install the packages because of missing write rights. You can edit a configuration element using pear config-set (use pear help config-set for details)

Installing the packages

Now you have to download all the stuff to your server to process to the offline installation.

  1. Download a copy of http://pear.phpunit.de/channel.xml and save it in /pear/download under phpunit.xml
  2. Download a copy of http://pear.symfony-project.com/channel.xml and save it in /pear/download under symfony.xml
  3. Download PHPUnit package and its dependencies. For instance, if you go to http://pear.phpunit.de/ and check for PHPUnit dependencies you will find pear.phpunit.de/File_Iterator, pear.phpunit.de/Text_Template, pear.phpunit.de/PHP_CodeCoverage, pear.phpunit.de/PHP_Timer, pear.phpunit.de/PHPUnit_MockObject, pear.symfony-project.com/YAML. So you will have to download all these packages. For each one pick the latest release.
  4. Upload all you have downloaded to /pear/download using FTP
  5. Navigate using SSH to /pear/download
  6. Add both phpunit and symfony channels executing pear channel-add phpunit.xml and pear channel-add symfony.xml
  7. Install packages starting with ones with no dependencies using pear install –offline –nodeps package_name

You’re done with the installation

Using PHPUnit

In your PHP code you have to do theses steps before starting your tests :

  1. Add /pear to PHP include_path with set_include_path(get_include_path() . PATH_SEPARATOR . ‘/pear’);
  2. Include PHPUnit autoload function require(‘PHPUnit/Autoload.php’);

Enjoy unit testing !

Strange directory / folder name ending with \r or ? on Linux

Yesterday I’ve been struggling on a strange problem. Browsing a folder ~/pear on my shared host down via SSH, I’ve found this :

username@ssh1:~/pear$ dir
bin docs download php php\r

If you look carefully you’ll see two php directories, and the last one is ending with \r. I’ve tried to remove it using :

rm -r php

resulting in removing the first one :

username@ssh1:~/pear$ dir
bin docs download php\r

I got stunned when I tried :

username@ssh1:~/pear$ cd php
-ssh: cd: php: No such file or directory

I got my answer 2 minutes ago : the php directory is created by a home-made shell script written using Notepad++ on Windows. As you may know Windows uses \r\n for line break whereas Linux only uses \n. So my script were interpreted as requesting the creation of php\r instead of a common php. This strange folder can be accessed using cd php?

If you cannot use dos2unix conversion tool, you can open the file with pico and save it using Ctrl+O and then you press Alt+D if [DOS Mode] is displayed before filename. This will convert the file into Linux format and save you a headache!

var_dump() for Javascript

Every PHP dev who has already debugged a script must have used the var_dump() function. This function lets you see what values a variable or array has with them. Unfortunately such a function does not exist in Javascript 😦 Here is an advanced one supporting:

  • String
  • Numbers
  • Function (the code is displayed)
  • Object
  • Boolean
  • Array
Example:
<script type="text/javascript">
  function hello_world(){alert("Hello World!");}
  foo = [];
  foo["yes"] = 1;
  foo["no"] = false;
  foo["alert"] = alert;
  foo["hello world"] = hello_world;
  var_dump(foo);

  function var_dump(obj, level) {
    var dump = "(<i>" + (typeof obj) + "</i>) : ";
    var level_nbsp = "";
    if(typeof level == "undefined"){var level = 1;}
    for(i = 0; i < 5*level; i++){level_nbsp += "&nbsp;";}
    switch(typeof obj){
      case "string":
        dump += obj + "<br/>\n";
        break;
      case "boolean":
        dump += (obj?"true":"false") + "<br/>\n";
        break;
      case "number":
      case "function":
        dump += obj.toString() + "<br/>\n";
        break;
      default:
        dump += "<br>\n";
        for(var key in obj){dump += level_nbsp +"<b>" + key + "</b> " + var_dump(obj[key], level + 1);}
        break;
    }
    if(level == 1){
      var dump_area = document.createElement('dump_area');
      dump_area.innerHTML = dump;
      document.body.appendChild(dump_area);
    }
    else{return dump;}
  }
</script>
The previous code will display:
(object) : 
     yes (number) : 1
     no (boolean) : false
     alert (function) : function alert() { [native code] }
     hello world (function) : function hello_world(){alert("Hello World!");}
Hope this helps.
If you’re a PHP developer, this article about an upgraded version of the PHP var_dump() function may interest you.

Upgraded var_dump() for PHP

Do you know the <pre> HTML tag? It defines preformatted text. Text in a pre element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks. Along with PHP var_dump() function, this tag is useful to display in a nice manner the code returned by the function. But the problem is that you have to write the following lines while debugging :

<?php
 echo '<pre>';
 var_dump($var);
 echo '</pre>';
?>

The solution is to define a function like this one :

<?php
 function var_trace($var){
   echo '<pre>';
   var_dump($var);
   echo '</pre>';
 }
 var_trace($var);
?>