Monthly Archive for June, 2008

Generating PHP test coverage and ccache

Today I had a weird error while generating PHP code coverage for PHPT tests. If you’re writing PHP extensions, it’s quite common to use compiler caches like ccache. They cache compile results in a way that speeds up multiple compilation of the same source, something which happens quite a lot if you’re working on coding some C extension. So I have cc, gcc and g++ dynamically linked to the ccache binary, which does the caching after proxying the compile request to the actual compiler. This works great, but when generating GCC code coverage (gcov), things don’t work as expected. When running make lcov, the following happened at the end:

Generating php_lcov.info
Capturing coverage data from lcov_data/
Found gcov version: 4.1.2
Scanning lcov_data/ for .gcda files ...
ERROR: no .gcda files found in lcov_data/!
make: *** [php_lcov.info] Error 255

The .gcda files are the ones which contain the useful coverage data used by lcov, but they just weren’t there! It took a while to figure out that disabling ccache fixed the problem. Actually the problem itself isn’t fixed, so if anyone has any idea why ccache and gcov data don’t like eachother, let me know in the comments! But for now, at least there’s a way around it, just disable ccache :-)

Dutch PHP Conference 2008, afterwards.

I just got back from the Dutch PHP Conference at the RAI in Amsterdam, and I had a great time! The 2 days conference (organized by the great people of Ibuildings and Zend) in Amsterdam were packed with fun stuff, interesting talks and people, and a cool football match (although I’m totally not into sports :-))

At thursday evening, we arrived in Amsterdam by car and first checked into our hotel. Afterwards there was still some time left to go to the city centre, so we did a bit of sightseeing. We knew quite some guys were going to a place called Fidelio, so after a good meal we went over there. As it was pretty late then, quite some people were already gone, but we still found Sebastian, Lorna, Derick, Scott, Stefan and some PHP London guys over there. It was closing time soon, so we decided to go to the hotel then.

Next day I chose the Zend Framework tutorial by Matthew Weier O’Phinney. After a short introduction and an overview of the Framework, we looked a bit deeper into specific components of the framework. Topics that were covered are how to use and write action and view helpers, the plugin architecture, extending Zend Framework classes, creating custom decorators, validators and filters, forms, how to test your code and much more! Matthew is a great speaker and the tutorial touched a lot of advanced topics, so it was definitely worth it :-)

That evening, there was a pre-conference social at the Werck bar in the city centre. We first went to diner at a great vegetarian place called “De Bolhoed” and got just in to see the start of the football match with the netherlands and france. At about half time, I decided to have it a bit and ended up with Matthew Weier O’Phinney, Remi Woler and Terry Chay talking about scalability issues and social network architectures. Really interesting stuff! The Dutch football players won the match with 4-1, so you can imagine a big party was about to start. We decided to take a walk to the central station so we could see (and feel) some of the victory happyness, afterwards we took the metro back to our hotel.

The last (and the main conference) day started with meeting some friends, such as Michelangelo and Andries. The first keynote by Zeev Suraski talked about a bit of history of PHP and where it is today. It was a funny and interesting talk with a lot of nice facts about PHP and the PHP community I didn’t know. Then Marco Tabini took the stage with his keynote “PHP and the taste of mayo”. After a pretty funny introduction, Marco moved on to keeping it simple in PHP (just as preparing mayo should be simple). He also talked about the revenue generated per visitor in different types of websites. As always, Marco delivered a good talk which makes you think about some topics.

After lunch, I accidentally missed Gaylord Aulke’s talk “An Infrastructure for Team Based PHP Development”, but instead had a very interesting talk with Terry Chay. We talked about the viral workflow of a social networking site, data analysing techniques and photo cameras. Even though he states in the closing keynote he’s not a good photographer, he has very thorough knowledge on how cameras work. He’s great in explaining difficult topics in a way you actually could understand it :-)

Next up, Lorna Michell went to the stage with her talk “PHP Deployment with subversion”. While she said in advance she was very nervous about it, she actually did great, and I very much enjoyed her way of presenting the topic. Her overview of what is possible for deploying PHP projects was nice. After all, if you throw in references to Super Mario and Nabaztag’s, what could go wrong? ;-)

Finally, Terry Chay prepared for the closing keynote, titled “The internet is an Ogre”. After making the analogy of the internet being big ugly fat ogre with layers, he continued on Stability, Scalability, Speed and Security and why you should handle them in that order. It’s always fun to hear Terry’s ideas, and while he might use some explicit language to get his point across, I mostly agree with what he’s saying.

This was a great conference. I got the chance to meet old friends, friends I only knew from IRC or online, but also meet interesting new people. The speakers presented some new ideas and it was very cool to see PHP be so much “alive”. A big thanks to all organizers and speakers for making this possible. I can easily say it was the best PHP conference I’ve attended yet, and I’m very much looking forward to going back next year! See you!

Testing Databases, streams and files with PHPT

Writing PHPT tests is pretty easy to learn. Basically, you execute php code, and define what output is expected from the code. That’s fine for testing string manipulating functions or some other basic function, but what do you do if databases or streams need to be tested? What if you need files or directories to execute code upon?

For databases, things can either be a bit difficult or not. Testing MySQL functionality is still a bit difficult, currently this is done mostly by fetching environment variables. You can set variables for MYSQL_TEST_HOST, MYSQL_TEST_PORT, MYSQL_TEST_USER, MYSQL_TEST_PASSWD, MYSQL_TEST_ENGINE, MYSQL_TEST_SOCKET to define connection parameters for your MySQL tests (for PDO_MYSQL they’re named a bit different, but you get the idea). An example of getting the hostname is:

$host = getenv("MYSQL_TEST_HOST") ? getenv("MYSQL_TEST_HOST") : "localhost";

Testing sqlite on the other hand is a whole lot easier. If you know that sqlite supports in-memory databases, you can pretty much get everything tested without a database dependency or files on the filesystem. Just open the database handle like this:

// procedural
$db = sqlite_open(':memory:');
// or OO-style
$db = new SQLiteDatabase(':memory:');

PHP streams are a similar subject when it comes to PHPT testing. If you want to test functions that interact with a stream handler, just use the php://memory built-in stream, like this:

$fp = fopen("php://memory", "r+");
// Now just use it as a regular PHP stream
fwrite($fp, "foobar");

If you do need to create files or directories for testing (for example for testing the SPL DirectoryIterator classes), best is to create them in the directory where the tests are located. Give them a descriptive name, and be sure to clean up after you’re done! This is done with a --CLEAN-- section in a PHPT test. Remember that the --FILE-- and --CLEAN-- parts of a PHPT test are called totally separate, so any variables that live in the --FILE-- part are lost in the --CLEAN-- part. This is an example to make it all a bit more clear:

--TEST--
Test is_file() function: basic functionality
--FILE--
<?php
$file_path = dirname(__FILE__);
$file_name = $file_path . DIRECTORY_SEPARATOR . "is_file_basic.tmp";
$handle = fopen($file_name, "w");
var_dump( is_file($file_name) );
fclose($file_name);
?>
--CLEAN--
<?php
// Rebuild file path, because we can't use the $file_name variable anymore.
$file_path = dirname(__FILE__);
$file_name = $file_path . DIRECTORY_SEPARATOR . "is_file_basic.tmp";
unlink($file_name);
?>
--EXPECT--
bool(true)

EmPHPower for PHP

I’ve been following Lukas‘ posts and news about EmPHPower for a while now, with great interest. To quote the mission statement:

emPHPower is a mediator and catalyst that empowers members of the community to follow their own ideas

This seems like a great initiative to me. Basically with the same views and motivations, we (Mike and me) have started PHPBelgium, to advocate the use of PHP with developers, companies and educators.

While our initiative is mostly regional (Belgium and to some extent, the Netherlands), emPHPower aims to be a central place to advocate and promote the use of PHP. It wants to connect PHP Core devs, companies and end users by providing a general platform (as a mediator) for them.

I fully support such initiatives (as I’ve been doing same things but for Belgium then) and hope this works out and actually will turn into something that helps every party involved. If there is something that can be done, I’d be glad to help out. In the end, this’ll create more visibility for PHP as a serious alternative, provide a place to go to as a company that wants to use/sponsor/develop PHP, and much more. PHP TestFest is an example that I believe could fall under organization of emPHPower.

Please note that emPHPower is currently just an idea. By the end of the year, if things look feasible, Lukas will actually start and take things further for emPHPower.

If you want to have a short overview of what emPHPower aims to be, have a look at this presentation

Using DateInterval class in PHP

DateInterval is (amongst others) a relatively new addition to the date extension in PHP 5.3. It allows to describe an interval in time, and you can perform actions with that interval. Basically, it allows to calculate with dates in a very easy way, and do even more fun stuff with it.

Unfortunately no documentation exists today for DateInterval and friends, but PHP is open-source so you can easily have a peek at the code to see what’s happening under the engine. This is more or less what the documentation for DateInterval and some helper functions could look like:

Prototype:

DateInterval DateInterval::__construct (string $interval)

Creates a DateInterval instance, based on the interval provided.

Parameters:
- $interval: ISO 8601 style time interval notation. (see Wikipedia)

Return Values:
Returns DateInterval object on success or FALSE on failure.

Example:

// Interval. This acutally means 3 years, 6 months, 
// 4 days, 12 hours, 30 minutes and 5 seconds.
$interval = 'P3Y6M4DT12H30M5S';
$i = new DateInterval( $interval );

This alone isn’t very exciting, but the fun stuff begins if you use some extra date functions with it:

// let's assume $i is still around from the previous example
$format = 'Y-m-d H:i:s';
$d = new DateTime('2008-01-01 12:25');
date_add( $d, $i );
echo $d->format( $format ) . PHP_EOL;
date_sub( $d, $i );
echo $d->format( $format ) . PHP_EOL;
 
$d2 = new DateTime('2008-01-01 12:25');
$diff = date_diff( $d, $d2 );
date_sub( $d2, $diff );
echo $d2->format( $format ) . PHP_EOL;

There’s a lot more possible (have a look at the DatePeriod class too), but that’s for another time. This is a pretty new group of classes within PHP, so expect these to expand in functionality in the future.