我正在测试我的Zend应用程序,并希望测试在注册表中没有设置特定键时发生的事情。这是我正在测试的功能:
protected function getDomainFromConfig() {
$config = Zend_Registry::get('config');
if (!isset($config->domain)) {
throw new Exception('Please make sure you have "domain" set in your config file and your config file is being set in the Zend_Registry.');
}
return $config->domain;
}
如何取消注册表中的项?我试过了,但没用:
$config = Zend_Registry::get('config');
$config->__unset('domain');
更新:我真正想知道的是,当没有设置配置文件中的“域”键时,我应该如何测试我的方法抛出异常。
发布于 2009-03-06 12:56:18
更改config对象值的唯一真正方法是将其转储到变量中,取消所述项,要求注册表删除配置项,然后重置它。
<?php
$registry = Zend_Registry::getInstance();
$config = $registry->get('config');
unset($config->domain);
$registry->offsetUnset('config');
$registry->set('config', $config);
?>
但是,要使其工作,您必须先将Zend_Config对象设置为可编辑,然后才能第一次将其设置到注册表中。
您应该考虑到以这种方式编辑注册表不是最佳做法。特别是,一旦最初实例化了Zend_Config对象,它就被设计为静态对象。
我希望我对你的问题有足够的了解!
发布于 2009-03-05 17:56:18
如果您的“配置”实际上是Zend_Config
,那么默认情况下它是只读的。Zend_Config
构造函数的可选第二个参数是布尔$allowModifications
,默认设置为false
。
您可能会在Zend_Config_Ini
中创建bootstrap.php
new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini',
APPLICATION_ENVIRONMENT)
附加$allowModifications
param:
new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini',
APPLICATION_ENVIRONMENT,
true)
发布于 2009-03-05 18:21:16
尝试:
unset($config->domain);
然后用修改后的$registry->config
类重新注册Zend_Config
。请注意,正如vartec所说,您必须将Zend_Config
实例实例化为可编辑的:
$config = new Zend_Config('filename', true);
您试图调用的__unset
方法是在实例上使用unset
时调用的魔术法。
https://stackoverflow.com/questions/615837
复制相似问题