Symfony Unit Test Do not download global applications app_

I am trying to create a unit test for my application in Symfony.

My / config / app.yml looks like this:

all: tmp_dir: "tmp" # usps usps_username: xxxxx usps_password: xxxxx usps_dir: usps 

in unit test, when I run something like:

 $t->comment(sfConfig::get('app_usps_username')); 

It just prints an empty string. What's happening? How can I access these values ​​from app.yml from unit test? If I try to access the values ​​from another place, it will work as expected.

+4
source share
1 answer

Since this is a unit test, the configuration is not initialized. This would not really be a unit test if the entire infrastructure were loaded by default.

If you rely on configuration parameters, than explicitly set them in your test:

 sfConfig::set('usps_username', 'my username'); 

With functional tests, this is a little different, as the whole application loads, and you can easily access your configuration.

Edit:

If you look at your cache directory, you will see that all configurations are created in the application directory (cache / frontend / test / config / config_app.yml.php). If you put it in main config / app.yml, just make sure all applications inherit these values. However, you need to run the script in the application context to use them.

In other words: this should be possible, but you will have to initialize the appropriate application configuration instead of the project configuration (therefore frontendConfiguration instead of ProjectConfiguration in your boot file).

However, I believe it is good practice to keep unit tests as independent of the application as possible.

+5
source

All Articles