PHP 模拟最终类

我试图模拟一个 php final class 但由于它被声明为 final 我一直收到这个错误:

I am attempting to mock a php final class but since it is declared final I keep receiving this error:

PHPUnit_Framework_Exception:类DoctrineORMQuery"被声明为final"并且不能被模拟.

有没有办法在不引入任何新框架的情况下,仅针对我的单元测试来解决这种 final 行为?

Is there anyway to get around this final behavior just for my unit tests without introducing any new frameworks?

推荐答案

既然您提到不想使用任何其他框架,那么您只剩下一个选择:uopz

Since you mentioned you don't want to use any other framework, you are only leaving yourself one option: uopz

uopz 是 runkit-and-scary-stuff 类型的黑魔法扩展,旨在帮助 QA 基础设施.

uopz is a black magic extension of the runkit-and-scary-stuff genre, intended to help with QA infrastructure.

uopz_flags 是一个可以修改函数、方法和类的标志的函数.

uopz_flags is a function that can modify the flags of functions, methods and classes.

<?php
final class Test {}

/** ZEND_ACC_CLASS is defined as 0, just looks nicer ... **/

uopz_flags(Test::class, null, ZEND_ACC_CLASS);

$reflector = new ReflectionClass(Test::class);

var_dump($reflector->isFinal());
?>

会屈服

bool(false)

相关文章