Symfony Unit Test Won't load app_ globals
I'm attempting to create a unit test for my app in Symfony.
My /config/app.yml looks like this:
all:
tmp_dir: "tmp"
# usps
usps_username: xxxxx
usp开发者_如何学Pythons_password: xxxxx
usps_dir: usps
in the unit test, when I run something like:
$t->comment(sfConfig::get('app_usps_username'));
It will just output and empty line. What's going on? How can I access these values from app.yml from the unit test? If I attempt to access the values from somewhere else, it works as expected.
Since it's a unit test the configuration is not initialized. It wouldn't be really an unit test if whole framework was loaded by default.
If you rely on configuration options than explicitly set them in your test:
sfConfig::set('usps_username', 'my username');
With functional tests it's a little bit different as whole application is being bootstrapped and you can easily access your configuration.
Edit:
If you take a look at your cache directory you'll see that all the configurations are generated under the application's directory (cache/frontend/test/config/config_app.yml.php). If you put it in main config/app.yml it's just that all applications inherit those values. Still you need to run your script in the context of an application to use them.
In other words: it should be possible but you'd have to initialize appropriate application configuration instead of project configuration (so frontendConfiguration instead of ProjectConfiguration in your bootstrap).
However, I think it's a good practice to keep unit tests as much independent from the application as possible.
精彩评论