PHP 模拟最终类 [英] PHP Mocking Final Class

查看:27
本文介绍了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 模拟最终类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆