Mock Admin Session With EcomDev_PHPUnit In Magento Enterprise

In integration tests with EcomDev_PHPUnit_Test_Case_Controller there is a useful helper method adminSession(), to test requests in the Magento backend. With Magento Enterprise you might get this error message if you use it:

Exception: Warning: in_array() expects parameter 2 to be array, null given in /home/vagrant/mwdental/www/app/code/core/Enterprise/AdminGws/Model/Role.php on line 83

As seen on Magento EE 1.14.1.

Without disclosing details about the Enterprise modules, here a solution where the responsible observer gets mocked:

$adminObserverMock = $this->getModelMock(
    'enterprise_admingws/observer',
    array('adminControllerPredispatch'));
$adminObserverMock->expects($this->any())
    ->method('adminControllerPredispatch')
    ->will($this->returnSelf());
$this->replaceByMock('singleton', 'enterprise_admingws/observer',
    $adminObserverMock);
$this->adminSession();

Magento: Lost Block Content After core_block_to_html_after Event

Be careful if you use Mage_Core_Block_Abstract::toHtml() inside an observer for core_block_to_html_after. I did that to inject HTML from another block into an existing block with the result that everything except this new block was removed from output.

Digging through the core, I found the cause in how the event is dispatched:

        /**
         * Use single transport object instance for all blocks
         */
        if (self::$_transportObject === null) {
            self::$_transportObject = new Varien_Object;
        }
        self::$_transportObject->setHtml($html);
        Mage::dispatchEvent('core_block_abstract_to_html_after',
            array('block' => $this, 'transport' => self::$_transportObject));
        $html = self::$_transportObject->getHtml();

As you see, the transport object that is used to allow modification of the HTML in observers, is a class variable of Mage_Core_Block_Abstract, so basically acts like a singleton. I cannot think of a good reason for this, but it smells ridiculously like premature optimization.

Now what’s happening is, that during the event is processed, the new block sets its own output to the same transport object in its toHtml() method and the original output is lost, if you didn’t save it before.

The solution to my particular problem was to generate and pre-render the new block elsewhere but if you don’t, you need to copy the output at the very beginning:

$html = $observer->getTransport()->getHtml(); // should be the first statement

// now you can do what you want, create other blocks, modifiy $html ...

$observer->getTransport()->setHtml($html); // should be the last statement

Retrospective: The Blog 2014

A personally and businesswise successful year has gone by and the new one is still starting slowly. Time to look back how the blog has been doing since I relaunched my site last February and set some new goals for 2015.

Hot topics

Some topics that sticked out in one way or the other:

Unit Testing

My all-time top article from 2011 [PHP] Mocking Built-in Functions Like time() In Unit Tests was still the second most visited in 2014 and I am happy to say that the concept that it introduced got some new attention:

PHP 5.3 Death March

The most controversional, most discussed and most clicked article of 2014 was my announcement to drop PHP 5.3 support wherever possible to force adaption of current PHP versions (5.3 reached its end of life in August 2014 and does not get any security fixes anymore since then).

I feel in good company because soon after, Anthony Ferrara (@ircmaxell) kicked of the same discussion on larger scale on Twitter and elsewhere with his articles about PHP versions:

  1. On PHP Version Requirements
  2. Being A Responsible Developer
  3. PHP Install Statistics

Vagrant

puphpet-vagrant-puppet-magentoI wrote a series on articles on Vagrant, specifically to introduce it for Magento developers, published on integer-net.de. Until now it is only available in German but I published an English summary in the Webguys advent calendar that covers everything to get you started: Türchen 19: Kickstart your Magento Dev System with Vagrant. Two of the four teasers on this blog made it into the top ten of 2014 (see numbers below).
Continue reading “Retrospective: The Blog 2014”