live627/phpunit-garbage-collector

Reset properties to save memory after each test

1.0.0 2022-11-15 23:04 UTC

This package is auto-updated.

Last update: 2024-05-16 07:49:48 UTC


README

Tests Scrutinizer Code Quality Code Coverage

PHPUnit seems to waste a lot of memory because it instantiates a lot of objects before actually starting any test, meaning that the initial memory usage varies depending on the number of tests available.

Say, for example, that you have UserTest with 1k test methods. This will unfortunately result in 1k UserTest class instances before any of the tests are actually run. This is actually by design. In order to determine the number of tests, PHPUnit runs @dataProvider methods before actually running the tests (and the setUp method).

The problem comes once all of the tests in the test case have finished. Objects in memory are closer than they appear because they aren't dereferenced until after the entire test suite has finished. Fortunately, this is on the radar.

Inspired by Kris Wallsmith faster PHPUnit article. The claim that this test listener that speeds up PHPUnit tests about 20% by freeing memory is dubious and probably is outdated now that PHP 7 has rewritten zvals to be a bit more compact.

Installation

To install this library, run the command below and you will get the latest version

composer require live627/phpunit-garbage-collector --dev

Usage

Just add to your phpunit.xml configuration

<phpunit>
    <listeners>
        <listener class="\live627\PHPUnitGarbageCollector\MemoryGuard"/>
    </listeners>
</phpunit>

Ignoring Tests

Sometimes it is necessary to ignore specific tests, where freeing their properties is undesired. For this use case, you have the ability to extend the behaviour of the listener by implementing the IgnoreTestPolicy interface.

As an example, if we hypothetically wanted to ignore all tests which include "Legacy" in their test filename, we could create a custom ignore policy as follows

<?php

use live627\PHPUnitGarbageCollector\IgnoreTestPolicy;

class IgnoreLegacyTestPolicy implements IgnoreTestPolicy {
    public function shouldIgnore(\ReflectionObject $testReflection): bool {
        return strpos($testReflection->getFileName(), 'Legacy') !== false;
    }
}

And pass it to the constructor of our test listener in phpunit.xml configuration

<phpunit>
    <listeners>
        <listener class="\live627\PHPUnitGarbageCollector\MemoryGuard">
            <arguments>
                <object class="\IgnoreLegacyTestPolicy"/>
            </arguments>
        </listener>
    </listeners>
</phpunit>