Stivlo'st in Asia

Programming and Travel

Browsing Posts in PHP

Unit testing is useful to test individual parts of the program, to check if they respect the specifications. Actually as any type of testing, can only point out bugs, but can’t prove that the program is correct.

Despite that, they are very useful during development, as TDD (Test Driven Development) suggests, it can be a good idea to write the test before the actual code. It clears up the specification of the method and may point out that we have to refine the specification a little before start coding, for instance about handling of special cases. When our code pass all the test the job is done, or at least done for that method.

PHP doesn’t have the awesome tools that come with Java, but in this respect there is a simpler implementation of Unit Testing that is still quite useful, called PHPUnit.

Installation

PHPUnit Manual describes installation in details, but actually on Ubuntu installation is much simpler. It’s enough to install package phpunit and the framework will be installed in /usr/share/php/PHPUnit.

Writing a test

Writing a test is as simple as defining a class, that by convention we call as the class we want to test with Test suffix. For example if we need to test a class named Customer, our unit test class will be called CustomerTest.

require_once "Customer.php";
require_once "PHPUnit/Framework.php"; //since /usr/share/php is in the path

class CustomerTest extends PHPUnit_Framework_Test {

    private $customer;    

    //anything that should be set up before running the tests
    public static function setUpBeforeClass() {
        $this->customer = new Customer();
    }

    //a test method for myMethod()
    public function testMyMethod() {
        $result = $this->customer->myMethod();
        $this->assertEquals(43, $result);
    }

}

Running the tests

To run the test from the command line, is enough to type:

phpunit CustomerTest.php

This is the basics to start using tests, but there is much more information in the online manual.

When using php setlocale() function it’s important to check the return value. I used to call it without checking it as in:

setlocale(LC_ALL, 'it_IT.UTF-8');

Without figuring out that if the locale it_IT.UTF-8 is not present in the system, the setlocale function fails and returns false.

The first idea I had to try to add the locale is calling:

$ dpkg-reconfigure locales

But nothing happens and the prompt is returned, because in my case the default question priority didn’t allow any question to be asked (like: what locales do you want to rebuild?). So I tried

$ dpkg-reconfigure -p low locales

With no improvements at all. So I resorted to the manual way editing the file /var/lib/locales/supported.d/local

and adding the entries I wanted in this case:

en_US.UTF-8 UTF-8
it_IT.UTF-8 UTF-8

So this time the reconfigure worked as I wanted:

# dpkg-reconfigure locales
Generating locales...
en_US.UTF-8... done
it_IT.UTF-8... done
Generation complete.

Today I try to enable debugging of PHP with Netbeans on the windows platform. For this reason I’ve downloaded the thread safe version of xdebug and placed it in my extension directory, in my case C:\easyphp3\php\ext

As explained in “Introducing xdebug” to activate it I added the following in my php.ini

zend_extension_ts=${path}\php\ext\php_xdebug-2.0.4-5.2.8.dll
xdebug.remote_enable=1

After restarting apache, my phpinfo() now confirms me that xdebug is enabled.

From Netbeans if the current project is set as Main, you can choose Debug  > Debug Main Project, or press Ctrl-F5, or click  the debug icon.

At this point I can place breakpoints, watches and so on with the familiar Netbeans debugger interface.

Excerpt from http://www.vim.org/tips/tip.php?tip_id=12

To insert space characters whenever the tab key is pressed, set the
‘expandtab’ option:

set expandtab

With this option set, if you want to enter a real tab character use
Ctrl-V<Tab> key sequence.

To control the number of space characters that will be inserted when
the tab key is pressed, set the ‘tabstop’ option. For example, to
insert 4 spaces for a tab, use:

set tabstop=4

After the ‘expandtab’ option is set, all the new tab characters entered
will be changed to spaces. This will not affect the existing tab
characters. To change all the existing tab characters to match the
current tab settings, use

:retab

PHP / Java Bridge is an interesting technology to allow to connect PHP with a Java virtual machine. On the official site they claim “It is more than 50 times faster than local RPC via SOAP, requires less resources on the web-server side, and it is faster and more reliable than communication via the Java Native Interface.”

http://php-java-bridge.sourceforge.net/pjb/

STEP 1: Compiling and installing Apache

$ tar xfvz apache_1.3.37.tar.gz
$ tar xfvz mod_ssl-2.8.28-1.3.37.tar.gz
$ cd mod_ssl-2.8.28-1.3.37
$ ./configure –with-apache=../apache_1.3.37
$ cd ../apache_1.3.37
$ SSL_BASE=/usr ./configure –prefix=/usr/local –enable-module=so \
–enable-module=speling –enable-module=rewrite –enable-module=ssl
$ make
$ make certificate TYPE=dummy
# make install

STEP 2: Compiling and installing php module

$ cd ..
$ tar xfvj php-5.2.1.tar.bz2
$ cd php-5.2.1
$ ./configure –enable-memory-limit \
–disable-url-fopen-wrapper –with-gd –enable-dbase –enable-exif \
–enable-ftp –with-gettext –with-iconv –enable-mbstring \
–with-jpeg-dir=/usr/lib –with-zlib-dir=/usr/lib –with-kerberos \
–with-png-dir=/usr/lib –enable-gd-native-ttf –with-freetype-dir=/usr/lib \
–with-openssl –with-xml –with-dom –disable-cgi –with-mcrypt \
–with-xsl –enable-javascript –with-mcal \
–with-sablot-js –with-readline \
–with-mysql –with-mysqli \
–with-config-file-path=/usr/local/conf/php-module \
–disable-cli –with-apxs=/usr/local/bin/apxs \
–with-curl
$ make
$ make install
# mkdir /usr/local/conf/php-module
# cp php.ini-recommended /usr/local/conf/php-module/php.ini
$ cd ..

STEP 3: Compiling and installing php cgi

$ cd ../php-5.2.1
$ ./configure –enable-memory-limit \
–disable-url-fopen-wrapper –with-gd –enable-dbase –enable-exif \
–enable-ftp –with-gettext –with-iconv –enable-mbstring \
–with-jpeg-dir=/usr/lib –with-zlib-dir=/usr/lib –with-kerberos \
–with-png-dir=/usr/lib –enable-gd-native-ttf –with-freetype-dir=/usr/lib \
–with-openssl –with-xml –with-dom –disable-cgi –with-mcrypt \
–enable-xslt –with-xslt-sablot –enable-javascript –with-mcal \
–with-sablot-js –with-readline \
–with-mysql –with-mysql \
–with-config-file-path=/usr/local/conf/php-cli \
–disable-cgi \
–with-curl
$ make
# make install
# mkdir /usr/local/conf/php-cli
# cp php.ini-recommended /usr/local/conf/php-cli/php.ini
$ cd ..

STEP 4: Installing PHP-Java Bridge

$ tar xfvz php-java-bridge_4.0.1.tar.gz
$ cd php-java-bridge-4.0.1
$ phpize && ./configure \
–with-java=/usr/local/jdk1.6.0,/usr/local/jdk1.6.0/jre \
&& make
# sh install.sh

Edit /usr/local/conf/php-module/php.ini to add the following lines:

extension_dir = “/usr/local/lib/php/extensions/no-debug-non-zts-20060613/”
extension=java.so

[Java]
java.class.path =
java.servlet = On
java.hosts=”localhost:8080″
java.persistent_connections=Off
;java.home = c:\jdk
;java.library = c:\jdk\jre\bin\hotspot\jvm.dll
;java.library.path = .\

STEP 5: Deploy JavaBridge.war

In order to use the JavaBridge extension the JavaBridge.war have to be deployed in tomcat

$ cp JavaBridge.war /usr/local/tomcat/webapps/

If Tomcat is running it will automatically deploy the application under webapps.

Powered by WordPress Web Design by SRS Solutions © 2010 Stivlo'st in Asia Design by SRS Solutions