Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Dave Marshall:
Using Closures as PHPUnit After Hooks
Dec 26, 2016 @ 20:21:32

Dave Marshall has written up a post showing how you can use closures with PHPUnit to provide "after" hook functionality.

Not sure why I didn't start doing this sooner. We have a basic Feature toggle system that is maintained in the global scope to make it easily accessible to any part of the code. [...] I needed to force a particular feature on in a PHPUnit integration test, but in order to tidy up after myself, I would need to ensure that the test reset the Feature system after it had finished. There are a few ways of doing this.

The first of the three, using PHPUnit's own global state handling comes with its own set of problems. The second was to use a try/catch block in the test to ensure the state is reset and a third was to use the "tearDown" to reset everything accordingly. He ended up finding what he needed in the form of "runAfterHooks" handling in a trait, passing in the closure to be executed.

tagged: phpunit unittest closure hook tutorial trait

Link: http://davedevelopment.co.uk/2016/12/23/using-closures-as-phpunit-after-hooks.html


Trending Topics: