Run PHP tests in your Perl test suite

| 5 Comments

Sometimes you've got a big codebase that isn't just Perl. Maybe you've got PHP mixed in with it, and you want to test the PHP along with all the Perl code, too. Perl's prove program doesn't care if the testing results it parses are from Perl, PHP or even static files, so long as they're in the TAP format. However, actually getting prove to run those PHP programs takes a little doing. Fortunately, Test::Harness 3.xx has hooks for source handlers.

David Wheeler has written about running PostgreSQL tests under prove in his blog and I stole from his code shamelessly to create TAP::Parser::SourceHandler::PHP, released to the CPAN this morning.

So now, to run the Perl .t files and the PHP .phpt files all in one swell foop, here's what we do at work:

prove -r \
    -I/home/alester/proj/Lib \
    --source=Perl --ext=.t \
    --source=PHP \
    --ext=.phpt \
    --php-option=include_path=/home/alester/proj/Class \
    --php-option=extension=.phpt

That --source=PHP tells prove to load up TAP::Parser::SourceHandler::PHP. The --php-option tells prove to pass those options through to the SourceHandler. If we had PostgreSQL tests or MySQL tests, we could use the SourceHandlers for those that David Wheeler has written as well.

Now we can test everything all in one run, and we get all the benefits of Test::Harness 3.xx, like parallel tests and TAP archiving and so on.

5 Comments

You can also just skip that, name everything .t and add "#!/usr/bin/env php" to the top of the PHP files, and "#!/usr/bin/env perl" to the top of the Perl files.

That not a bad idea but it isn't portable. It wouldn't work on Windows or systems that don't have env.

TAP::Parser is great. I've used it to run TAP producing binaries written in C++ (using libtap++). It works great. I love it's flexibility.

Well, "#!perl" and "#!php" should work everywhere. But I don't have a Windows system (or one with PHP) to test this on.

If "just" modifying the shebang line and giving the files inaccurate extensions would have been an adequate solution, then that's what I would have done. However, it's not.

Leave a comment

Job hunting for programmers


Land the Tech Job You Love, Andy Lester's guide to job hunting for programmers and other technical professionals, is available in PDF, ePub and .mobi formats, all DRM-free, as well as good old-fashioned paper.