Search code examples
phpmagentophpunitcode-coverage

Can I teach PHPUnit that Mage::throwException *always* throws an exception?


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()?


Solution

  • I have also this problem.

    Just do:

    throw Mage::getException(...);
    

    and in getException you return the exception object. Otherwise you're stuck with it.