为什么我的脚本在访问未定义数组中的值时不触发通知?

发布于 2024-10-10 07:44:38 字数 982 浏览 0 评论 0原文

我有一个如下所示的测试方法:

$row = $this->GetRowFromUserTable($id);
$this->assertLessThan(time(), time($row['last_update']));

当 $row 为 null 时,访问 $row['last_update'] 应触发 E_NOTICE 并且此测试应该失败,但事实并非如此。

这段代码在第一次断言时失败,所以我知道 $row 为空(fixture 是相同的):

$row = $this->GetRowFromUserTable($id);
$this->assertNotNull($row);
$this->assertLessThan(time(), time($row['last_update']));

当我写这个时:

$row = $this->GetRowFromUserTable($id);
$this->assertEquals(E_ALL, error_reporting());
$this->assertLessThan(time(), time($row['last_update']));

它成功了,所以我也确信 error_reporting 是正确的,这种情况一定与 PHPUnit 有关。

我使用 PHPUnit 3.5.6

我读过这个问题: 如果代码抛出异常,我能否使 PHPUnit 失败注意? 但答案建议使用较新版本的 PHPUnit,但该答案是 2009 年的,所以不是它。

编辑:我使用 NetBeans IDE 6.9.1 来运行我的测试。

I have a test method which looks like this:

$row = $this->GetRowFromUserTable($id);
$this->assertLessThan(time(), time($row['last_update']));

When $row is null, access to $row['last_update'] should trigger an E_NOTICE and this test should fail, but it doesn't.

This code fails on first assert, so I know $row is null (fixture is the same):

$row = $this->GetRowFromUserTable($id);
$this->assertNotNull($row);
$this->assertLessThan(time(), time($row['last_update']));

When I write this:

$row = $this->GetRowFromUserTable($id);
$this->assertEquals(E_ALL, error_reporting());
$this->assertLessThan(time(), time($row['last_update']));

it succeeds, so I am also sure that error_reporting is right and this situation must have something to do with PHPUnit.

I use PHPUnit 3.5.6

I read this question:
Can I make PHPUnit fail if the code throws a notice?
but the answer suggests to use newer version of PHPUnit, but that answer is from 2009, so it's not it.

EDIT: I use NetBeans IDE 6.9.1 to run my test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-10-17 07:44:38

据说“convertNoticesToExceptions”也是我唯一真正的猜测(+1)。

您可以在测试用例中的某个位置编写一个 $foo = $undefinedVariable; 来看看这是否也不会触发通知 // 不会使测试失败?

要检查您的 phpunit / php 安装是否正常工作(也许可以缩小问题范围),您可以尝试:

<?php

class myTest extends PHPUnit_Framework_TestCase {
    public function testFoo() {
        $x = $foo;
        $this->assertTrue(true);
    }
}

并运行(注意 --no-configuration 部分只是为了确保您不会选择 phpunit.dist.xml 或phpunit.xml 意外)

phpunit --no-configuration foo.php 

它显示输出:

PHPUnit 3.5.6,作者:Sebastian Bergmann。

E

时间:0秒,内存:2.50Mb

有 1 个错误:

1) myTest::testFoo 未定义的变量:
foo

/home/user/foo.php:5

也许这有一点帮助

Saidly "convertNoticesToExceptions" was my only real guess too (+1).

Could you write a $foo = $undefinedVariable; somewhere in the testcase to see if that also will not not trigger a notice // not fail the test ?

To check if your phpunit / php installation is working (and maybe narrow down the problem) you can try:

<?php

class myTest extends PHPUnit_Framework_TestCase {
    public function testFoo() {
        $x = $foo;
        $this->assertTrue(true);
    }
}

and run (note the --no-configuration part just to make sure you don't pick up a phpunit.dist.xml or phpunit.xml by accident)

phpunit --no-configuration foo.php 

It show output:

PHPUnit 3.5.6 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 2.50Mb

There was 1 error:

1) myTest::testFoo Undefined variable:
foo

/home/user/foo.php:5

Maybe that helps a litte

灰色世界里的红玫瑰 2024-10-17 07:44:38

您描述的具体情况没有给出E_NOTICE。访问已定义数组的未定义键会给出 E_NOTICE,但奇怪的是,尝试访问 null 值却不会,而数组则不会。

The specific case you describe doesn't give an E_NOTICE. Accessing undefined keys of a defined array give an E_NOTICE, but strangely trying to access null values as an array doesn't.

紫﹏色ふ单纯 2024-10-17 07:44:38

您是否将 convertNoticesToExceptions 配置属性设置为“true”?

Do you have convertNoticesToExceptions configuration property set to "true"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文