Reset all view helpers in Zend Framework
Is there a way to reset the state of all view helpers, for example remove all scripts added with headScript()
? Scripts and meta tags added in one unit test are being seen by the following unit tests even though I am 开发者_如何学JAVAcreating a new controller and view for each test. This is causing false positives and negatives.
Having a look at the the abstract class for those containers (Zend_View_Helper_Placeholder_Container_Abstract
), I see it extends ArrayObject. Any time you set
it replaces that Array's values with an array that wraps the value you set. When you append
, it gets the old values and unshifts the new value onto the array. So, in theory, you'd be able to use the exchangeArray method to clear any values by passing a blank array (completely untested):
$this->view->headScript()->exchangeArray(array());
Marcin's comment led me to the root cause of the problem in our testing framework, which is that the placeholder registry was being added to the Zend registry before the code that takes a snapshot of the registry was run. Thus, when the code that removes all new registry objects run, it was no longer removing the placeholder registry.
To reset the state for all placeholder-based view helpers, use this:
$registry = Zend_Registry::getInstance();
unset($registry['Zend_View_Helper_Placeholder_Registry']);
精彩评论