Reputation: 3311
To load my ini file from a path outside the codebase I use apache to define the path and with php I use apache_getenv / getenv
It seems phpunit doesn't understand the apache_getenv command - is this a known issue and are there any solutions besides hardcoding paths?
Upvotes: 2
Views: 880
Reputation: 28928
When you run php from the commandline it does not use any of the Apache configuration. But you can still access environment variables. So, first change to use getenv
everywhere (instead of apache_getenv
). Then, if you're starting php from bash, run it like this:
export MY_CONFIG = "/path/to/my.ini"
phpunit
You only need to do the export once per bash session. You could wrap that up in a 2-line bash script.
This approach has one issue, which is that you have configuration in two places now. Murphy's Law tells us those two files will try really hard to get out of sync. So, an alternative approach is to use a custom entry in php.ini (*). And then in your code use get_cfg_var to fetch those values. Be aware that some people do not like this approach, e.g. create arbitrary values in php.ini Use it wisely: if your machine is dedicated to the application that needs this setting, then it is a fair choice. If it is just one script among many, and on a multi-user machine, then it is dubious.
A third approach is a symlink to your real ini file that is kept with your script. Then your script will always find it, however it is started. I like this approach best, but it is not so dynamic. I'm guessing the point of using an environment variable was because you use a different value for different virtual hosts, or something like that.
*: Make sure you use the php.ini file that is common to apache and cli: some setups also have a configuration file that is just for apache, and just for the cli. On Ubuntu machines I would create a file under /etc/php5/conf.d called myconfig.ini, as that is easier to maintain, and that directory is used by both apache and cli.
Upvotes: 2