Callbacks for cleaning up (3.5) #37

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
3 participants

zenovich commented Oct 5, 2010

Hello!
Please, merge this change. I really need it and I suggest it will be usefull for many people.
I really need it in the 3.5 branch.
Thanks.

zenovich commented Oct 6, 2010

I hope that now all things are ok. Please merge it into the master branch too. Thanks in advance.

I will not pull this as the functionality is not required. I don't see what you want to achieve cannot be done using tearDown().

Sebastian,
I use it for the per-method teardown. In many cases this way is much easier and clearer than implicit testcase-level teardown. See the example:

/**
 * @expectedException ObjectFactory_Exception
 */
public function testCreateObjectWithEmptyFile() {
    $objectType = 'ObjectFactoryTest_EmptyFileClass';
    ConfigHelper::setSection($objectType); //sets the section in the config, don't mind

    $oldLibPath = LIB_PATH;
    runkit_constant_redefine('LIB_PATH', __DIR__ . '/_files/');
    $this->addCleanupCallback(function() use ($oldLibPath) {runkit_constant_redefine('LIB_PATH', $oldLibPath);});

    $this->callMethod('ObjectFactory', 'getObject', $objectType);
}

Won't pull, sorry.

I'm happy this feature exists at least in python unittest http://docs.python.org/2/library/unittest.html#unittest.TestCase.addCleanup

You can add this functionality in a custom base class that you extend instead of PHPUnit's. Extend PHPUnit's TestCase and override runBare(). We added this functionality and more in our base test case.

zenovich commented Nov 1, 2012

Yes, that is what I've done two years ago. I think this feature will be helpful for others, so I suggest to add it to PHPUnit for all people.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment