Asked  1 Year ago    Answers:  5   Viewed   17 times

I'm currently using Zend Framework in conjunction with PHPUnit to conduct unit testing on an application. When Hudson executes the PHPUnit shell command, the maximum PHP memory limit is reached sometime during code coverage generation. I currently have a total of 41 tests with 334 assertions.

I have successfully eliminated this error by raising the memory_limit setting to 768M using the -d memory_limit=768M switch; however, I am worried that as the complexity increases along with the total number of tests/assertions, I will not have enough memory to generate the HTML for code coverage statistics.

OS: CentOS 5.5
Control Panel: WHM/cPanel
CI Server: Hudson

/usr/local/bin/phpunit 
  --verbose  
  -d memory_limit=512M  
  --log-junit ../../build/logs/phpunit.xml   
  --coverage-clover ../../build/logs/coverage/clover.xml   
  --coverage-html ../../build/logs/coverage-html/  

Fatal error: Allowed memory size of 536870912 bytes exhausted

Before committing my changes and letting Hudson handle the rest, I use Windows 7 for development. The memory usage never exceeded 340MB while running the same command within W7.

 Answers

2

By reducing the number of files included within code coverage, as well as increasing the overall memory limit in PHP, I was able to basically kill this error. The entire Zend Framework was being included within code coverage, which is very large.

Thursday, April 1, 2021
 
Exoon
 
1

The fragment part of the URL is never sent to the server via GET requests (or any kind of HTTP request for that matter), the only way you can get it is if you write a Javascript snippet that parses the URL and sends the fragment back to the server via Ajax for instance.

This can't be done with PHP alone.

Thursday, April 1, 2021
 
1

The main advantage of using the Registry with a config is to avoid polluting the global namespace. Say, you want to include a third party lib and your app and the lib both define a constant DB_HOST. No good.

In addition, many of the factories in Zend Framework utilize the config object to create instances. Zend_Db is a good example of this. You just pass it $this->database and it will take what it needs to instantiate your adapter.

You can also extend the registry with custom functionality, e.g. finder methods or stuff like that. Check the pattern description by Fowler.

Thursday, April 1, 2021
1

This is an ugly hack, but it works in PHPUnit 3.6. We already have our own custom test case base class that all others extend. If it doesn't matter when the files get added to the whitelist you could do this using a fake test case just to handle this part.

First, bootstrap.php calls BaseTestCase::addXXXToCodeCoverageWhitelist() as many times as necessary to populate an internal array of files to add later. Next, the first test to be executed adds those files to the code coverage filter via the TestResult.

abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
    private static $_codeCoverageFiles = array();

    public static function addDirectoryToCodeCoverageWhitelist($path) {
        self::addFilesToCodeCoverageWhitelist(self::getFilesForDirectory($path));
    }

    public static function addFileToCodeCoverageWhitelist($path) {
        self::addFilesToCodeCoverageWhitelist(array($path));
    }

    public static function addFilesToCodeCoverageWhitelist(array $paths) {
        self::$_codeCoverageFiles = array_merge(self::$_codeCoverageFiles, $paths);
    }

    public static function getFilesForDirectory($path) {
        $facade = new File_Iterator_Facade;
        return $facade->getFilesAsArray($path, '.php');
    }

    private static function setCodeCoverageWhitelist(PHP_CodeCoverage $coverage = null) {
        if ($coverage && self::$_codeCoverageFiles) {
            $coverage->setProcessUncoveredFilesFromWhitelist(true); // pick your poison
            $coverage->filter()->addFilesToWhitelist(self::$_codeCoverageFiles);
            self::$_codeCoverageFiles = array();
        }
    }

    public function runBare() {
        self::setCodeCoverageWhitelist($this->getTestResultObject()->getCodeCoverage());
        parent::runBare();
    }
}

Update: For anyone that used the blacklist to keep framework classes from showing up in assertion failure stack traces as we did, add the following methods to the above class and call them from your bootstrap.php. This requires setAccessible() from PHP 5.3.

    public static function ignoreDirectoryInStackTraces($path) {
        ignoreFilesInStackTraces(self::getFilesForDirectory($path));
    }

    public static function ignoreFileInStackTraces($path) {
        ignoreFilesInStackTraces(array($path));
    }

    public static function ignoreFilesInStackTraces($files) {
        static $reflector = null;
        if (!$reflector) {
            PHPUnit_Util_GlobalState::phpunitFiles();
            $reflector = new ReflectionProperty('PHPUnit_Util_GlobalState', 'phpunitFiles');
            $reflector->setAccessible(true);
        }
        $map = $reflector->getValue();
        foreach ($files as $file) {
            $map[$file] = $file;
        }
        $reflector->setValue($map);
    }
Saturday, May 29, 2021
 
aslum
 
4

Here is the phpunit.xml for one of my projects, this works fine. As you can see, the logging section is outside the filter section, so that is probably your issue as commented by Mark Baker. I chose this one as it is from a small project and is very simple.

<phpunit bootstrap="./bootstrap.php" colors="false">
    <testsuite name="HSSTests">
        <directory>./</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">d:/wamp/app_hss/</directory>
            <exclude>
                <directory suffix=".phtml">d:/wamp/app_hss/</directory>
                <directory suffix=".php">d:/wamp/app_hss/tests/</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
            yui="true" highlight="true"
            lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html" />
    </logging>
</phpunit>

All the information you could ever need on this is in the PHPunit manual.

Friday, August 13, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :