PHP Mocking Final Class(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)
这篇关于PHP 模拟最终类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 模拟最终类


基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01