Question

I'd like to unit test my Magento blocks using PHPUnit, how can I properly instantiate my models using the Magento framework to be used as a System Under Test? I include the path to my class, but it will need a link to the rest of the framework to run.. What do I do to avoid a long line on include statements?

include C:\repo\magento\cwdl\app\code\core\Mage\Adminhtml\Block\Sales\Order\Shipment\Create\Tracking.php';

class TrackingPageTests extends PHPUnit_Framework_TestCase{

    public function testGetCarriers(){
        $this->SUT = new Mage_Adminhtml_Block_Sales_Order_Shipment_Create_Tracking();
        $this->SUT.getCarriers();
        $this->assertEquals(5,5);
    }
}

error I get: PHP Fatal error: Class 'Mage_Adminhtml_Block_Template' not found in C:\repo\magento\cwdl\app\code\core\Mage\Adminhtml\Block\Sales\Order\Shipment

Was it helpful?

Solution

You have to include at least the Magento autoloader (Varien_Autoload) and register it. I'd recommend doing this in your test bootstrap file instead of in each test case.

But if you are not testing your classes completely isolated, which is rare for blocks, you will also need to instantiate the Magento application (Mage::app()), basically turning your tests into integration tests.

There is lots of global state in Magento, so you should call Mage::reset() in the setUp() methods to reset all registry values and singletons.

Here's a working example: https://github.com/digitalpianism/testframework

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top