Last active
February 5, 2016 19:52
-
-
Save ajbonner/42b59d3d85470007ffdb to your computer and use it in GitHub Desktop.
Quick and dirty way to test if an observer responds to a given event in Magento 1 using MageTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeTestTest extends MageTest_PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @param string $observerName e.g. mymodulens_mymodule_does_something_on_order_place | |
* @param string $eventName e.g. a dispatched event e.g. sales_order_place_after | |
* @param string $area e.g. global/frontend/admin/adminhtml | |
*/ | |
public function assertObserverReceivesEvent($observerName, $eventName, $area) | |
{ | |
Mage::app()->getConfig()->whitelistObserver($observerName); | |
$config = Mage::app()->getConfig(); | |
$eventConfig = $config->getEventConfig($area, $eventName); | |
if (isset($eventConfig->observers->$observerName)) { | |
$observer = $eventConfig->observers->$observerName; | |
$class = (string)$observer->class; | |
$mock = $this->getModelMock($class); | |
$mock->expects($this->once()) | |
->method((string)$observer->method); | |
$config->setModelInstanceMock($class, $mock); | |
Mage::dispatchEvent($eventName, array()); | |
} else { | |
$this->fail('No observer called ' . $observerName . ' is listening for event ' . $eventName . ' in area ' . $area); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment