Why doesn't my script trigger a notice when accessing a value in an undefined array?
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.
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
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.
Do you have convertNoticesToExceptions configuration property set to "true"?
精彩评论