PHPUnit: continue after die, expect "die" or somehow handle die()? [duplicate]
Asked Answered
A

1

2

Possible Duplicate:
How do you use PHPUnit to test a function if that function is supposed to kill PHP?

I'm writing some unit tests. The system I'm currently testing is a web-app in an MVC framework.

If we want to render pages without the site-skin system we've traditionally run our code as usual, but printed a "die();" statement at the end of the function to exit before the rest of the website renders.

Well now that we're adding unit testing, this seems to be a problem. You see, when you DIE(); in the MVC, that seems to send the same message to PHPUnit.

Grrrr... the code has now been rendered "Untestable"

Or has it?

How does one plan for a die(); In PHPUnit?

I know about adding this before a test:

/**
 * @expectedException PHPUnit_Framework_Error
 */

but it doesn't allow me to test the variables as they were 'at time of death' (at least I don't think so)

Anyone able to enlighten me?

Thanks! :D

Alcot answered 17/8, 2010 at 15:42 Comment(0)
C
3

In this case you just need to use return instead of die. It should be easy to replace all the deaths to returns in your IDE.

The real problem is, when you really need to test application exit code returned by exit or die (see edorian's answer).

Cheeseburger answered 17/8, 2010 at 18:48 Comment(2)
+1, in most (nearly all) cases it's better to use return instead of die in a application.Arrivederci
a good example where you don't have a choice is where you're code is being executed by some other framework which requires your code to die to achieve what you need it to.For example, several WordPress hooks have this feature if you want to prevent WordPress doing any further processing after your code :(Acolyte

© 2022 - 2024 — McMap. All rights reserved.