开发者

Code is executed before PHPUnit's initialization

开发者 https://www.devze.com 2023-04-12 19:17 出处:网络
I was reading my project\'s code coverage report, and I noticed something strange: a line was uncovered, but I was sure that line got executed during the tests. So, I added a var_dump() before it and

I was reading my project's code coverage report, and I noticed something strange: a line was uncovered, but I was sure that line got executed during the tests. So, I added a var_dump() before it and this is what I got when executing the tests:

bool(true)
PHPUnit 3.5.5 by Sebastian Bergmann.

...

This is weird. How is it possible that a line is executed before PHPUnit's initialization? I believe this is the reason why code coverage says that line is uncovered.

Any hints?

EDIT: Here's some code. It's an IRC framework that makes use of the Doctrine Common library to read annotations and also uses the ClassLoader and EventDispatcher Symfony components. This is the incriminated method:

/**
 * Returns this module's reflection.
 * 
 * @return \ReflectionClass
 * @access public
 */
static public function getReflection()
{
    // The var_dump() displaying bool(false) is executed before PHPUnit, while the other
    // ones are correctly executed.
    var_dump(is_null(self::$refle开发者_开发知识库ction));

    if (null === self::$reflection) {
        // This line is reported as uncovered, but it must be executed since I'm
        // accessing the reflection!
        self::$reflection = new \ReflectionClass(get_called_class());
    }

    return self::$reflection;
}

What do you think?


Then, why are all the others var_dump() (that method gets executed many times in the application) shown after PHPUnit's output? And why isn't that line reported in code coverage even if it's executed?

I assume (but that's just a guess as it's hard to say since you have not shown the code), that it's related to code that gets executed on file-inclusion, rather after actual test functions are executed or testcases get instantiated.


The accessor must be getting called outside of a unit test which initializes self::$reflection. After that, all further calls to getReflection() skip the if block so it'll never be counted as covered. PHPUnit instantiates all of the test case classes--one per test method, data provider method, and data provider argument array--before running any of the tests or tracing code coverage. Look for a test case that calls getReflection() from its constructor or outside the class itself where the code is executed upon loading.

I forget if the test cases are instantiated before PHPUnit outputs its version and cannot check now, but I believe this is the case. The other possibility is that you're calling getReflection() from bootstrap.php, but you probably already checked for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号