Sqlite is always empty in memory, despite the configuration

I followed the points of the site Testing Symfony applications using a one-time database . I added Fixtures to my test folder and no errors appear during SetUp. If I add an error to Fixtures (for example, leaving the nullable = false field blank), an error message will appear, so this code will definitely be executed.

My configuration:

doctrine: dbal: default_connection: memory connections: memory: driver: pdo_sqlite memory: true charset: UTF8 

My SetUp in my WebTestCase:

 protected function setUp() { parent::setUp(); self::bootKernel(); DatabasePrimer::prime(self::$kernel); $this->loadFixtures([ 'AppBundle\DataFixtures\ORM\UserData', 'AppBundle\DataFixtures\ORM\ArtistData' ]); } 

However, in my WebTestCase it seems that there are no tables. The result throws a Doctrine exception because my table does not exist.

 SQLSTATE[HY000]: General error: 1 no such table: my_user_table 

If I switch to sql_lite in the file, everything will work fine without any changes:

 dbal: default_connection: file connections: file: driver: pdo_sqlite path: %kernel.cache_dir%/test.db charset: UTF8 

Has anyone been successful in this tutorial or used db sqlite memory for unit tests and had any hints or ideas?

Update: I changed my installation to this to ensure that the kernel will not be closed between them. It did not help:

 parent::setUp(); $this->client = $this->getClient(); MemoryDbPrimer::prime(self::$kernel); $this->loadFixtures([ 'AppBundle\DataFixtures\ORM\UserData', 'AppBundle\DataFixtures\ORM\ArtistData' ]); 
+7
sqlite symfony phpunit doctrine functional-testing
source share
2 answers

I assume that you call createClient() in your test functions. The very first thing that createClient() does is call static::bootKernel() . This basically means that the kernel loaded into your setUp() closes and the new kernel loads with a fresh instance of the SQLite memory database.

You can move the createClient() call to your setUp() by replacing bootKernel() to avoid this:

 class MyTest extends WebTestCase { private $client = null; public function setUp() { $this->client = static::createClient(); // prime database } public function testSomething() { $crawler = $this->client->request('GET', '/'); // ... } } 
+1
source share

When you

 $client->request(<METHOD>, <URL>); 

which causes

 Symfony\Bundle\FrameworkBundleClient::doRequest($request) 

After the request, the kernel is disabled by default, and your database in memory is reset.

If you call

 client->disableReboot(); 

in the setup () function of your test, this behavior will be disabled and you can run the entire package.

0
source share

All Articles