0

Magento has a static method that does some extra reporting just before throwing an exception.

/**
 * Throw Exception
 *
 * @param string $message
 * @param string $messageStorage
 * @throws Mage_Core_Exception
 */
public static function throwException($message, $messageStorage = null)
{
    if ($messageStorage && ($storage = self::getSingleton($messageStorage))) {
        $storage->addError($message);
    }
    throw new Mage_Core_Exception($message);
}

It's guaranteed to throw an exception, so it's mildly annoying that PHPUnit's code coverage considers the closing brace after a Mage::throwException statement to be uncovered code.

Shows code coverage analysis of code with no coverage following throwException statement.

I looked through the PHPUnit documentation, but I don't see any non-hacky way to have it consider the line covered. (I consider putting a dead-code return statement at the end of the method hacky, or really anything that we would have to do every time we use Mage::throwException.)

Is there some way I can teach PHPUnit that Mage::throwException always throws an exception, so treat it the same (with respect to coverage) as it would throw new WhateverException()?

4

1 回答 1

1

我也有这个问题。

做就是了:

throw Mage::getException(...);

并在 getException 中返回异常对象。否则你会被它困住。

于 2014-05-30T14:11:17.767 回答