【问题标题】:PHPUnit test if method is a static methodPHPUnit 测试方法是否是静态方法
【发布时间】:2017-03-15 14:46:42
【问题描述】:

如何使用 phpunit 测试方法是否为静态?

我有一个静态方法\ProcessWire\className::getModuleInfo,我想添加一个测试以确保它是静态的。我该怎么做?

【问题讨论】:

  • 你为什么要测试它? IMO,这样的测试没有任何价值。当然,除非您正在使用代码生成器并想要验证其输出。
  • 用于测试我是否可以调用静态方法而不是在调用之前创建类的新实例?
  • 如果不能静态调用方法,php运行时会抛出异常。您无需进行测试即可执行此操作。这是一个测试php的尝试。你不需要测试php,你只需要测试你的域代码。

标签: unit-testing testing phpunit


【解决方案1】:

你可以使用Reflection来确认一个方法是static

<?php
class Apple {
    public function firstMethod() { }
    final protected function secondMethod() { }
    private static function thirdMethod() { }
}

$class = new ReflectionClass('Apple');
// Only return method data that is a static method
$methods = $class->getMethods(ReflectionMethod::IS_STATIC);
// shows information only for `thirdMethod()`
var_dump($methods);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2021-03-12
    • 2012-12-20
    相关资源
    最近更新 更多