Reputation: 19325
A search yields a couple of questions on catching fatal exceptions, but this one is specific to using SimpleTest. (I know that SimpleTest is out of date, but getting PHP-Unit to work on my configuration is another question).
I am trying to get the tearDown() method to work even when there is a fatal exception, as I create some test rows in the database during setup, and remove them during tear-down. But when SimpleTest comes to a fatal exception, teardown() is never ran.
Is there a way to get tearDown() to run despite a fatal exeception?
Upvotes: 2
Views: 452
Reputation: 7214
There is a "register_shutdown_fuction" hook that can be used:
register_shutdown_function(array($this, 'shutdownHandler'));
In "shutdownHandler" you can write:
error_get_last() && $this->tearDown();
Upvotes: 3
Reputation: 401172
When a Fatal Error occurs, the PHP process is terminated -- which means there is no way to have that same PHP process executed any kind of addionnal code, as it's no longer there.
This will also mean that :
You should fix the problem : a Fatal Error in your application is bad ; it's great that you detected it with your automated tests, of course -- but the next step is to make it go away ;-)
As you cannot run anymore PHP code in the same process that has died, the only solution I see would be to launch another process, to run your clean-up code.
The basic idea would be to :
Of course, this means the clean-up will only be done once, after all tests have been run ; but I suppose it's better than nothing.
Now, how to do that in an automated way ?
The simplest solution would probably be to use a shell-script, that runs both commands ; something like this, I'd say :
#!/bin/sh
php /.../launch-tests.php
php /.../cleanup.php
And run your tests by launching that shell-script.
Upvotes: 1