Writing PHPT tests on your mac

This is a short tutorial on how to set up a Mac with Mac OS X to prepare for writing PHPT tests. This preparation includes installing some extra software that enables you to checkout and compile the PHP_5_3 branch of the PHP source without problems. Furthermore we’ll install some tools that make it possible to get an easy overview of untested PHP code with code coverage reports.

This overview assumes you’re working on the latest version of Mac OS X, being 10.5 (Leopard). Some terminal experience also comes in handy!

1. Installation of XCode

Apple’s XCode is used to create a development environment on your mac. For example, the GCC compiler, automake and other build tools are installed with XCode. Notice: you do need XCode version 3 or higher. The files needed for the installation can be found on the Mac OS X installation DVD or on the Apple Developer Connection website (http://developer.apple.com)

2. Installation of MacPorts

MacPorts is a package manager that will ease the installation, configuration and updating of open-source software on your mac. We’ll use MacPorts to install some tools that are needed in our development environment. You can find the MacPorts installation files on http://www.macports.org. By default, the MacPorts binaries are installed in /opt/local/bin. To make working with the terminal easier, we’ll add the directory to our PATH variable. Let’s open Apple’s Terminal.app and type:

# echo 'export PATH=/opt/local/bin:$PATH' >> ~/.profile
# source ~/.profile

Trough MacPorts, we’ll install a lexer used by PHP (re2c) and a tool to download files (wget):

# sudo port install re2c
# sudo port install wget

3. Lcov installation

Next up, we’ll install lcov. Lcov is a graphical front-end for gcov, a code coverate testing tool for the GCC compiler. Lcov isn’t available in the MacPorts repository, so we’ll need to install this manually:

# sudo mkdir -p /usr/local/src; cd /usr/local/src
# sudo wget http://downloads.sourceforge.net/ltp/lcov-1.6.tar.gz
# sudo tar -xzvf lcov-1.6.tar.gz
# cd lcov-1.6

Due to a non-compatible ‘install’ binary on Mac OS X, the installation can’t be completed with the -D switch. This has no implications on the actual resulting installation, but it does need to be changed to suppress the error messages:

# sudo nano /usr/local/src/lcov-1.6/bin/install.sh

Go to line 34 (install -D $SOURCE $TARGET) and remove the -D switch. Afterwards you can install lcov with the following command:

# sudo make install

4. Installing the right flex version

Now we need to install a alternate flex version. By default, Mac OS X has the flex lexical parser installed, but the installed version is not compatible with PHP. Thus we’ll install an older flex on an alternate location, which then can be used for compiling PHP:

# cd /usr/local/src
# sudo wget http://dfn.dl.sourceforge.net/sourceforge/flex/flex-2.5.4a.tar.gz
# sudo tar -xzvf flex-2.5.4a.tar.gz
# cd flex-2.5.4
# ./configure && make
# sudo make install

5. Installing the right autoconf version

If you compile PHP, you’re advised to use autoconf version 2.13. Since Mac OS X comes with autoconf version 2.61 by default, we’ll install an older autoconf version, which again can be used when we’re compiling PHP:

# cd /usr/local/src
# sudo wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.13.tar.gz
# sudo tar -xzvf autoconf-2.13.tar.gz
# cd autoconf-2.13
# ./configure && make
# sudo make install

Let’s change our PATH variable. That way, the right lcov, flex and autoconf binaries will be found by PHP:

# echo 'export PATH=/usr/local/bin:/opt/local/bin:$PATH' >> ~/.profile
# source ~/.profile

6. Downloading the PHP source

We create a directory to put all our sources and other related files:

# mkdir ~/phptest; cd ~/phptest/

Now we can check out the PHP source code using anonymous SVN:

# svn checkout http://svn.php.net/repository/php/php-src/branches/PHP_5_3 php-src-5.3 
# cd ~/phptest/php-src-5.3
# ./buildconf

7. Compiling and testing of PHP

Everything is now set up to write PHPT tests or just explore and hack the PHP source code. If you want to configure and compile PHP, use the following commands:

# ./configure && make && make test

If you also want to enable code coverage results, use the following command:

# ./configure --enable-gcov && make && make lcov

Optional, you can also pass the TESTS variabele to the make lcov command to only run a specific test-case (of course, this speeds up compiling the PHP source):

# make lcov TESTS=ext/foo/tests/bar.phpt

Next up, have a look at the lcov_html/ directory, where you can find the HTML code coverage results. Open your browser to view the code coverage, and write some PHPT tests to increase the overall coverage!

8. Some links to get you started on testing

PHP-QA: http://qa.php.net
PHP TestFest: http://wiki.php.net/qa/testfest
PHPT: http://phpt.info/

Update (09/07/2010): I’ve updated the article to reflect the change from CVS to SVN as version control system of the PHP project.

2 responses to “Writing PHPT tests on your mac”

  1. @olleolleolle: You’re right of course. This article was written in preparation of a TestFest 2 years ago, hence the use of CVS. I’ve updated the article to describe subversion as VCS.