【问题标题】:global variables are null when using PHPUnit使用 PHPUnit 时全局变量为空
【发布时间】:2012-02-22 19:04:02
【问题描述】:

我正在将 PHPUnit 测试放入现有项目中。全局常量变量被广泛使用。在我的单元测试中,函数失败了,因为全局变量为空。这是一个失败的测试示例

static $secret_key = "a secret Key";
class secret_key_Test extends PHPUnit_Framework_TestCase
{
    function test_secret_key()
    {
        global $secret_key; 
        $this->assertEquals($secret_key, "a secret Key");   
    }
}

>> Failed asserting that 'a secret Key' matches expected null

任何帮助将不胜感激

更新: 我试过删除静态并添加

protected $backupGlobals = FALSE;

到类声明没有成功。

【问题讨论】:

  • 呵呵,全局变量是你开始单元测试时应该摆脱的,因为隔离和状态
  • 顺便说一句,你可能想说global $secret_key = "a secret Key";而不是static,不是吗?
  • 应该删除 static 关键字。 global 仅在函数/方法中才有意义。
  • @zerkms 是的,永远不应该使用全局变量,但不幸的是 phpunit 也会删除本地启动的变量(在特定的单元测试类中受到保护)。这是错误的,应该由开发人员决定哪些变量将通过 setUp() 和 tearDown() 清除。例如,当我测试 Web 服务时,我只需要一个登录名,我想在多次测试中重复使用它 - 但是 phpunit 总是删除启动的实例并强制我进行多次身份验证。错了。
  • @lubosdz:那是隔离

标签: php phpunit


【解决方案1】:

你应该要求 phpunit 不要备份全局变量

protected $backupGlobals = FALSE;

就像 S. Bergmann 的原始文章中所说的那样:https://web.archive.org/web/20130407024122/http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html

【讨论】:

  • 或者确保它们在引导过程中由bootstrap.php 或它在 PHPUnit 开始执行实际测试方法之前加载的某个模块设置。
  • @rp90:上面的代码包含奇怪的static,你还没有解释static是关于什么的
【解决方案2】:

这个答案不起作用。我问了一个几乎相同的问题here,最后得到了一个更有意义的答案;您不能覆盖 PHPUnit 将看到的测试类中的受保护属性 $backupGlobals。如果您在命令行上运行,似乎可以通过创建 xml 配置文件并将 backupGlobals 设置为 false 来让 Globals 工作。

编辑:使用 PHPUnit 时,您需要在全局空间中声明 $secret_key 并在全局空间中为其赋值。 PHP 默认将全局初始化的变量放入全局命名空间,但 PHPUnit 在备份全局变量时会更改此默认值!

需要进行以下更改:

global $secret_key; // Declaring variable global in global namespace
$secret_key = "a secret Key"; // Assigning value to global variable

您的代码现在应该可以工作了。

【讨论】:

  • 这解决了我的问题。我将 PHP 版本 7.3.0 与 PHPUnit 7.3.5 一起使用。非常感谢!
【解决方案3】:

您必须在引导测试时设置全局变量。这是我如何编写测试的示例代码

    /**
     * Class to allow us set product on the fly
     */
    class Product
    {
        public function __call($method, $args)
        {
            if (isset($this->$method)) {
                $func = $this->$method;
                return call_user_func_array($func, $args);
            }
        }
    }

    /**
     * UssdShortcode Tester
     */
    class ShortCodeTester extends WP_UnitTestCase {

        protected  $product;

        public function setUp()
        {   
            $this->product            = new Product;
            $this->product->get_id    = function(){ return 50; };

            $GLOBALS['product']       = $this->product;
        }

        /**
         * A single example test.
         */
        function test_it_can_display_ussd_shortcode() {

            $displayer = new UssdShortCodeDisplayer;
            $expected  = view('show-product-short-code',['product_id' => $this->product->get_id() ]);
            $results   = $displayer->display($this->product->get_id());

            // $this->assertRegexp('/'.$expected.'/', $results);
            $this->assertEquals($expected,$results);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2020-11-14
    相关资源
    最近更新 更多