如果该功能应该杀死PHP,如何使用PHPUnit测试该功能? [英] How do you use PHPUnit to test a function if that function is supposed to kill PHP?

查看:65
本文介绍了如果该功能应该杀死PHP,如何使用PHPUnit测试该功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个名为killProgram的类的方法,该方法旨在发送hTTP重定向,然后杀死PHP.

Essentially I have a method of a class called killProgram, which is intended to send a hTTP redirect and then kill PHP.

我应该如何测试呢?当我运行phpunit时,该测试不返回任何内容,并完全关闭.

How am I supposed to test this? When I run phpunit it doesn't return anything for that test, and closes completely.

现在,我正在考虑让killProgram函数抛出一个不应处理的异常,这将使我断言已引发异常.

Right now I'm considering having the killProgram function throw an exception which shouldn't get handled, which would allow me to assert that an exception was thrown.

有更好的方法吗?

推荐答案

由于每个测试均由同一PHPUnit进程运行,因此,如果在PHP代码中使用exit/die,您将杀死所有内容-正如您所注意到的^^

As every tests are run by the same PHPUnit process, if you use exit/die in your PHP code, you will kill everything -- as you noticed ^^

因此,您必须找到另一种解决方案,是的-就像返回而不是死亡;或引发异常(您可以测试某些经过测试的代码是否引发了预期的异常).

So, you have to find another solution, yes -- like returning instead of dying ; or throwing an exception (you can test if some tested code has thrown an expected exception).

也许是PHPUnit 3.4及其--process-isolation开关(请参见可以选择使用单独的PHP进程执行每个测试)可能(不会使所有生命垂死)来帮助 ,但是如果PHPUnit无法获得测试结果,您仍将无法获得测试结果控制回来.

Maybe PHPUnit 3.4 and it's --process-isolation switch (see Optionally execute each test using a separate PHP process) might help (by not having everything dying), but you still wouldn't be able to get the result of the test, if PHPUnit doesn't get the control back.

我有过几次这个问题;通过返回而不是死亡来解决它-甚至返回几次(如果需要)以返回调用栈中的足够高" ^^
最后,我想我的应用程序中不再有任何死"了.考虑到MVC,顺便说一句,它可能会更好.

I've had this problem a couple of times ; solved it by returning instead of dying -- even returning several times, if needed, to go back "high enough" in the call stack ^^
In the end, I suppose I don't have any "die" anymore in my application... It's probably better, when thinking about MVC, btw.

这篇关于如果该功能应该杀死PHP,如何使用PHPUnit测试该功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆