Skip to content

Latest commit

 

History

History
63 lines (39 loc) · 2.36 KB

README.md

File metadata and controls

63 lines (39 loc) · 2.36 KB

Don't Leak

PHP Composer and unittest


ARCHIVED, NO LONGER NEEDED.

This package is no longer needed, since PHPUnit now destructs the TestCase instances during the run.


PHPUnit keeps all Test instance in memory for the whole run. This causes used memory to continuously increase, unless you clean up all your properties during a tearDown().

There's No Doubt you'll eventually forget this tedious task.

Installation

composer require --dev talkinnl/dont-leak

Usage

In your Test classes, add this as a very last line of your tearDown().

Maybe you'd like to always use a common parent class so you'll never forget.

    protected function tearDown(): void
    {
        // Start with some of your cleanups which are still needed.
        // Maybe removing files made during the test etc
        
        // Always call your parents; they might get worried. :)
        parent::tearDown();
        
        // Unset any properties, prevent memory leaks.
        DontLeak::freeOwnProperties($this);
    }

What does it do?

DontLeak::freeOwnProperites($object) unsets all properties reachable in your own scope of given $object, and all private properties of parent objects.

Properties defined by PHPUnit WON'T be touched.

What's next

I'd really like it if this package would be become obsolete thanks to PHPUnit changing the behaviour.

Please read, and maybe vote --> sebastianbergmann/phpunit#4705

Credits

Many suggestions of a tearDown which uses reflection to clean up exist scattered across Stack Overflow, Reddit, Github, etc.

The implementation was heavily inspired by https://gist.github.com/malarzm/e8c6141b510708e52c8535d2a13cd613 , which unsets instead of assigning null, and also clears private properties of safe parents.