[英]Mock php functions
I make a symfony 4 reusable bundle for my projects.我为我的项目制作了一个 symfony 4 可重用包。 I want to test it with a coverage nearby 100%.
我想用接近 100% 的覆盖率来测试它。 But i don't know how to test some part of my code who depends of php functions.
但我不知道如何测试依赖于 php 函数的代码的某些部分。
example :例子 :
public function getStatusCode(\Exception $exception)
{
if ($exception instanceof HttpExceptionInterface) {
return $exception->getStatusCode();
}
if (is_a($exception, "Symfony\Component\Security\Core\Exception\AccessDeniedException")) {
return Response::HTTP_UNAUTHORIZED; // I want to test this
}
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
I try to use getMockBuilder but the mock's class is not correct.我尝试使用 getMockBuilder 但模拟的类不正确。
Is there a way to mock or replace is_a or to create a mock whose class is the same as that in condition?有没有办法模拟或替换 is_a 或创建一个类与条件相同的模拟?
If you really want to mock this function, you can use this package: https://packagist.org/packages/hjerichen/prophecy-php如果你真的想模拟这个功能,你可以使用这个包: https : //packagist.org/packages/hjerichen/prophecy-php
This is a simple example for a test method in a PHPUnit TestCase.这是 PHPUnit TestCase 中测试方法的简单示例。
$php = $this->prophesizePHP(__NAMESPACE__);
$php->is_a($exception, "Symfony\Component\Security\Core\Exception\AccessDeniedException")->willReturn(true);
$php->reveal()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.