views:

1048

answers:

6

I am testing my Zend Framework application and would like to test that something happens when a particular key is not set in the registry. This is the function that I am testing:

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;
}

How can I unset a key in the registry? I tried this, but it did not work:

$config = Zend_Registry::get('config');
$config->__unset('domain');

Update: What I really want to know is how should I test that my method throws an exception when the "domain" key in the config file is not set.

A: 

If your 'config' is actually Zend_Config, then by default it's read-only. Zend_Config constructor's optional second parameter is boolean $allowModifications, by default set to false.

You probably create Zend_Config_Ini in bootstrap.php with

new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', 
                    APPLICATION_ENVIRONMENT)

append $allowModifications param:

new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', 
                    APPLICATION_ENVIRONMENT, 
                    true)
vartec
That's exactly the error message I am getting: Zend_Registry is read-only. I need a way to fake it. Maybe there is a different way to do it, rather than actually "unsetting" it.
Andrew
Zend_Registry or Zend_Config?
vartec
A: 

Try:

unset($config->domain);

And then re-register $registry->config with the modified Zend_Config class. Note that, as vartec said, you'll have to instantiate your Zend_Config instance as editable:

$config = new Zend_Config('filename', true);

The __unset method you're trying to call is a magic method that is called when you use unset on the instance.

Ross
unset($config->domain) calls $config->__unset('domain'), so it doesn't make any difference.
vartec
A: 

I'm seeing you are trying to delete a value in the $config, where $config is stored in Zend_Registry before deletion. so if deleting the value does not affect the stored $config value in Zend_Registry, I assume that by calling Zend_Registry::get() you are having the value of $config and not a reference to the $config in Zend_Registry. so when you change something in the copied $config, it will not affect the stored one. I suggest you first change the $config, and then overwrite the $config in the registry, by setting the $config in Zend_Registry again.

farzad
A: 

I think you might find your answer here:

http://framework.zend.com/issues/browse/ZF-2752

Typeoneerror
A: 
  1. Don't mix up Zend_Registry and the config.ini. The fact that you're storing the values from your config.ini in the Zend_Registry doesn't mean they're the same. It's just one use of the Registry. Getting the config.ini into an array and then storing that array in the Registry is just for convenience.

  2. The registry and especially the config.ini are read-only. You don't manipulate things in there at runtime. You can set new value in the registry with Zend_Registry::set() but you don't alter them.

  3. If you want to test, what happens, if a particular config value is missing, just comment it out in the config.ini.

tharkun
+3  A: 

The only real way to change the value of the config object would be to dump it to a variable, unset the item in question, ask the registry to delete the key for config, and then reset it.

<?php
$registry = Zend_Registry::getInstance();
$config = $registry->get('config');
unset($config->domain);
$registry->offsetUnset('config');
$registry->set('config', $config);
?>

However, for this to work you would have to have set the Zend_Config object to editable before it is set into the registry the first time.

You should consider that it is not best practice to edit the registry in this way. In particular, the Zend_Config object is designed to be static once it is instantiated originally.

I hope I have understood your problem well enough!

tombazza
I'm not working on this project anymore, so I haven't had a chance to verify your answer, so I am accepting it based on number of user votes.
Andrew