views:

215

answers:

1

I have the following folder structure:

/main
    /loader.php
    /build.xml
    /components
        /package1
            /class1.php
        /package2
            /class2.php
    /tests
        /package1
            /class1.test.php
        /package2
            /class2.test.php

When I run the web application I load loader.php at first and include other components by calling Loader::load( 'package_name' ). Then all neccessary files are included. The good thing here is that I don't need to include loader.php within the class files because I can rely on having a working instance of Loader.

The Unit Test classes simulate this behaviour by including all neccessary classes explicitly. So there is also no problem with phing and PHPUnit.

But now I want to generate a coverage report with phing and Xdebug. The problem here is that phing seems to load every single PHP file to create the coverage database. Unfortunately it stops because it cannot find the Loader class that is used in the PHP files.

I could easily add an include statement to every class file, but I wonder whether there is a way to include files only if code coverage analysis is inspecting the file?

Other idea: I could also configure the coverage analysis in a way that it scans the unit tests directory and therefore finds all neccessary includes. Then I'd need to filter classes that match to a pattern like /Test$/i or so. Possible?

+1  A: 

Hi,

I looked for ages for something similar.

In the end I ended up with the changes below. Basically you tell php cli to prepend a php file which contains your loading logic.

In php.ini of my cli I've set the following:

auto_prepend_file = autoload.php

I made sure that the file was on my include path (/usr/share/php in my case) and put following lines in it (I use Zend Framework which is also on my include path):

require_once "Zend/Loader/Autoloader.php"; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace('Model_');

Now, what you could do is define your __autoload function and define what needs to be autoloaded, but you get the idea.

It's an ugly hack, but it got things done for me.

Wkr Jeroen