简体   繁体   English

PHP - 用于拦截方法调用的 runkit 的替代品

[英]PHP - Alternatives to runkit for intercepting method calls

I have some very test-unfriendly code (to say the least) that I need to test.我有一些非常不利于测试的代码(至少可以这么说)需要测试。 Refactoring unfortunately is not an option.不幸的是,重构不是一种选择。 I have to test the code as it is, without the possibility of changing it.我必须按原样测试代码,而不能更改它。

To do that, I was thinking of intercepting function calls and dynamically change what they do so I can run my tests, as I need some functions and methods to return known values, and I need others that make requests, connect to the database, etc, to stop doing that and return what I need them to return.为此,我正在考虑拦截 function 调用并动态更改它们的功能,以便我可以运行测试,因为我需要一些函数和方法来返回已知值,并且我需要其他发出请求、连接到数据库等的函数和方法,停止这样做并返回我需要他们返回的东西。 Is there any way to do this without runkit_method_redefine() , which is preferably not "EXPERIMENTAL" and still maintained?没有runkit_method_redefine()有没有办法做到这一点,最好不是“实验”并且仍然保持? Maybe an alternative to runkit?也许是runkit的替代品? Maybe a better way?也许更好的方法?

Edit: will use PHPUnit's test doubles and PHP 5.3.2's features for making private methods accessible, if I need that functionality.编辑:如果我需要该功能,将使用 PHPUnit 的测试替身和 PHP 5.3.2 的功能来使私有方法可访问。

Note: the Test-Helper extension is superseded by https://github.com/krakjoe/uopz注意: Test-Helper 扩展被https://github.com/krakjoe/uopz取代

PHPUnit 's Test Helper extension (PECL) allows redefiniton/intercepting/stubbing/mocking of hardcoded dependencies with your own implementations: PHPUnit的测试助手扩展 (PECL) 允许使用您自己的实现重新定义/拦截/存根/模拟硬编码的依赖项

protected function setUp()
{
    $this->getMock(
      'Bar',                    /* name of class to mock     */
      array('doSomethingElse'), /* list of methods to mock   */
      array(),                  /* constructor arguments     */
      'BarMock'                 /* name for mocked class     */
    );

    set_new_overload(array($this, 'newCallback'));
}

It also allows intercepting the exit statement and instance creation:它还允许拦截退出语句和实例创建:

For stubbing and mocking methods you simply use PHPUnit's regular mocking framework.对于存根和 mocking 方法,您只需使用 PHPUnit 的常规 mocking 框架。 See

You can also use Mockery with PHPUnit:您还可以将 Mockery 与 PHPUnit 一起使用:

Another option would be to use http://antecedent.github.io/patchwork另一种选择是使用http://antecedent.github.io/patchwork

Patchwork is a PHP library that makes it possible to redefine user-defined functions and methods at runtime, loosely replicating the functionality runkit_function_redefine in pure PHP 5.3 code, which, among other things, enables you to replace static and private methods with test doubles. Patchwork is a PHP library that makes it possible to redefine user-defined functions and methods at runtime, loosely replicating the functionality runkit_function_redefine in pure PHP 5.3 code, which, among other things, enables you to replace static and private methods with test doubles.

The runkit extension is a perfect solution for your needs. runkit 扩展是满足您需求的完美解决方案。 It is proven by years of my personal experience and described in many presentations and articles authored by different authors in the internet.我多年的个人经验证明了这一点,并在互联网上不同作者撰写的许多演示文稿和文章中进行了描述。

I can assure you that the runkit_method_redefine function as well as the whole runkit extension is not experimental anymore (documentation hosted on the php.net is obsolete).我可以向您保证,runkit_method_redefine function 以及整个 runkit 扩展不再是实验性的(托管在 php.net 上的文档已过时)。 The up-to-date runkit extension can be found on http://github.com/zenovich/runkit可以在http://github.com/zenovich/runkit上找到最新的 runkit 扩展

Sincerely, Dmitry Zenovich此致 德米特里·泽诺维奇

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM