Symfony2: How can I choose the environment to use when running unit tests?

I need to run unit tests for a Symfony2 application for two different database configurations: one using a MySQL database and the other using an SQLite database.

Currently, I am choosing the database configuration used when performing unit tests by editing app/config/config_test.yml . I will either uncomment the db settings related to MySQL and comment out the db options related to SQLite or vice versa.

I would not have to do this and instead have two configuration files - perhaps app/config/config-test-mysql.yml and app/config/config-test-sqlite.yml - and select the test environment from the command line when starting the tests .

By looking at the default phpunit configuration of Symfony2 in app/phpunit.xml.dist and looking at the bootstrap file that uses config ( app/bootstrap.php.cache ), I cannot determine how the default environment is set to test when doing unit tests .

How to choose the environment that will be used when performing unit tests?

+4
source share
1 answer

I have not tried this solution, but I am sure that this is a good advantage.

My unit test class extends Symfony\Bundle\FrameworkBundle\Test\WebTestCase , which allows you to create a Client that Kernel creates itself.

In unit test you can do this:

 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class DatabaseRelatedTest extends WebTestCase { private static $client; public function setUp() { // this is the part that should make things work $options = array( 'environment' => 'test_mysql' ); self::$client = static::createClient($options); self::$client->request('GET', '/foo/bar'); // must be a valid url } } 

You will be able to access EntityManager and by expanding Connection using the client container.

 self::$client->getContainer()->get('doctrine') 

It would be ideal to pass the environment name to the setUp method using the phpunit.xml.dist file. This is probably a semi-answer, but I think this is a good result.

+1
source

All Articles