重置 Zend Framework 中的所有视图助手

发布于 2024-10-18 20:25:17 字数 124 浏览 4 评论 0 原文

有没有办法重置所有视图助手的状态,例如删除使用 headScript() 添加的所有脚本?即使我为每个测试创建一个新的控制器和视图,以下单元测试也会看到在一个单元测试中添加的脚本和元标记。这会导致误报和误报。

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 creating a new controller and view for each test. This is causing false positives and negatives.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-10-25 20:25:17

查看这些容器的抽象类(Zend_View_Helper_Placeholder_Container_Abstract),我发现它扩展了ArrayObject。每当您设置时,它都会用包装您设置的值的数组替换该数组的值。当您append时,它会获取旧值并将新值移至数组中。因此,理论上,您可以使用 exchangeArray 方法通过传递空白数组来清除任何值(完全未经测试):

$this->view->headScript()->exchangeArray(array());

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());
各自安好 2024-10-25 20:25:17

Marcin 的评论让我找到了测试框架中问题的根本原因,即在运行获取注册表快照的代码之前,将占位符注册表添加到 Zend 注册表中。因此,当删除所有新注册表对象的代码运行时,它不再删除占位符注册表。

要重置所有基于占位符的视图助手的状态,请使用以下命令:

$registry = Zend_Registry::getInstance();
unset($registry['Zend_View_Helper_Placeholder_Registry']);

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']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文