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 模拟最终类


基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01