drci47425 2014-01-22 10:36 采纳率: 100%
浏览 33
已采纳

PHPUnit - 如何模拟/存根重定向方法?

I am working on a PHP Unit test where I need to test (while 100% code coverage is required) a class that implements it's own redirect($url, $code) method. This method calls exit; at the end to make sure no other code is executed after the redirect (and without this exit; the redirection is not done in the end...).

Now this redirect method is called in 7 different conditions which all have their own unit test case. If I run the unit tests while the exit; is present in the tested class, the unit tests are exited as well. Each of the test cases is testing something like:

$this->assertRedirect();
$this->assertResponseStatusCode(401);

Therefore I need to stub the redirect method so that it set's the Location header as well as the status code.

Here is what I am trying to do (using the redirect method's content except that exit; part - but it is not working obviously):

    $this->class = $this->getMockBuilder('Some\Name\Space\Class')
            ->disableOriginalConstructor()
            ->getMock();
    $this->class->expects($this->any())
            ->method('redirect')
            ->will($this->returnCallback(function () {
                $this->response->getHeaders()->addHeaderLine('Location: /');
                $this->response->setStatusCode(401);
                $this->response->sendHeaders();
            }));

and what I have tried before (and didn't work as well):

    $this->class = $this->getMockBuilder('Some\Name\Space\Class')
            ->disableOriginalConstructor()
            ->getMock();
    $this->class->expects($this->any())
            ->method('redirect')
            ->will($this->returnCallback(function () {
                header('Location: /');
            }));

Now the purpose of the class is to redirect somewhere in 90% of code coverage (only in one case - 2 lines out of 90 - it is not redirecting) so without the possibility to stub this redirect (and without possibility to test redirects at all) the test cases would not have almost any sense for this class (which is unwilling).

Any help is highly appreciated.

EDIT: Looks like phpunit/test_helpers could do the trick, but unfortunately, I am not able to install this extension to our preview and production environments where the tests are automatically run per each application deploy.

  • 写回答

1条回答 默认 最新

  • duangua5308 2014-01-22 13:44
    关注

    OK, I figured the problem out: because the application is an MVC architecture, the exit; called in during the dispatch was a bad approach.

    Instead of it, after the response headers are set correctly (in order to redirect) I am returning false all the way up until it is returned back to the dispatch process. In this case I do not need to call exit; anymore.

    Unit test for this case is running smoothly without any changes needed (it was implemented before the exit; call in the tested class).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)