this is my first post so don't hesitate to tell me if something is wrongly said.
I'm working on a projet in Symfony 7.*.
I try to implement unit test in my project but I'm facing some issues.
I use liip/functional-test-bundle (version 4.13.0) and liip/test-fixtures-bundle (version 3.0.0). PHPUnit (version 9.6.22).
Here after, some code to show :
doctrine.yaml
when@test:
doctrine:
dbal:
driver: pdo_sqlite
user: "root"
dbname: "name_test"
password: "*********"
path: "%kernel.cache_dir%/name_test.db"
charset: UTF8
url: null
SceneTrait
...
use Liip\FunctionalTestBundle\Test\WebTestCase;
...
/**
* @extends WebTestCase<SceneTrait>
*/
trait SceneTrait
{
use FixtureTrait {
setUp as fixtureSetUp;
tearDown as fixtureTearDown;
}
public function setUp(): void
{
$this->getRestClient();
$this->fixtureSetUp();
}
...
FixtureTrait
/**
* @extends KernelTestCase<FixtureTrait>
*/
trait FixtureTrait
{
...
public function setUp(): void
{
foreach ($_ENV as $key => $value) {
putenv($key.'='.$value);
}
parent::setUp();
/** @var ContainerInterface $container */
$container = static::getContainer();
/** @var DatabaseToolCollection $databaseToolCollection */
$databaseToolCollection = $container->get(DatabaseToolCollection::class);
$this->databaseTool = $databaseToolCollection->get();
/** @var Registry $doctrine */
$doctrine = $container->get('doctrine');
/** @var EntityManager $entityManager */
$entityManager = $doctrine->getManager();
$this->entityManager = $entityManager;
$this->getRpFixture = $this->databaseTool->loadFixtures($this->_getDependencies())->getReferenceRepository();
}
...
}
And then an example of a Test :
use Liip\FunctionalTestBundle\Test\WebTestCase;
final class AffaireControllerTest extends WebTestCase
{
use SceneTrait;
...
}
Using xdebug, I see that I go through the loadFixtures() function in Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool.php
public function loadFixtures(array $classNames = [], bool $append = false): AbstractExecutor
{
$referenceRepository = new ProxyReferenceRepository($this->om);
/** @var Configuration $config */
$config = $this->om->getConfiguration();
$cacheDriver = $config->getMetadataCache();
if ($cacheDriver) {
$cacheDriver->clear();
}
if (false === $append && false === $this->getKeepDatabaseAndSchemaParameter()) {
// TODO: handle case when using persistent connections. Fail loudly?
$schemaTool = new SchemaTool($this->om);
$schemaTool->dropDatabase();
if (!empty($this->getMetadatas())) {
$schemaTool->createSchema($this->getMetadatas());
}
}
$event = new FixtureEvent();
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_SETUP);
$executor = $this->getExecutor($this->getPurger());
$executor->setReferenceRepository($referenceRepository);
if (false === $append) {
$executor->purge();
}
$loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
$executor->execute($loader->getFixtures(), true);
return $executor;
}
I see the right db parameters from $schemaTool->schemaManager->_conn->params. And also the right schema parameters from my entities in $this->getMetadatas(). And also my fixtures in $classnames.
In the end, when I run my test, my database in created but the schema is not created and so the Fixtures.
For the schema I added in /test/bootstrap.php
$appKernel = new Kernel('test', false);
$appKernel->boot();
$application = new Application($appKernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);
$application->run(new ArrayInput([
'command' => 'doctrine:database:drop',
'--force' => '1',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:database:create',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:schema:create',
]));
$appKernel->shutdown();
So the schema is now created but still no fixtures. And I think that I shouldn't have to do that as there is already $schemaTool->createSchema($this->getMetadatas()) in the loadFixtures() function...
Any idea on what I'm doing wrong ?
Thanks !
EDIT : Seems like the problem came form dama/doctrine-test-bundle (.html#resetting-the-database-automatically-before-each-test). I removed the package and now it's working ...
But now, if I want to refresh/reset the database before each test (using this Dama bundle or RefreshDatabaseTrait from Hautelook Alice Bundle, I come back to the problem with empty database without schema)
this is my first post so don't hesitate to tell me if something is wrongly said.
I'm working on a projet in Symfony 7.*.
I try to implement unit test in my project but I'm facing some issues.
I use liip/functional-test-bundle (version 4.13.0) and liip/test-fixtures-bundle (version 3.0.0). PHPUnit (version 9.6.22).
Here after, some code to show :
doctrine.yaml
when@test:
doctrine:
dbal:
driver: pdo_sqlite
user: "root"
dbname: "name_test"
password: "*********"
path: "%kernel.cache_dir%/name_test.db"
charset: UTF8
url: null
SceneTrait
...
use Liip\FunctionalTestBundle\Test\WebTestCase;
...
/**
* @extends WebTestCase<SceneTrait>
*/
trait SceneTrait
{
use FixtureTrait {
setUp as fixtureSetUp;
tearDown as fixtureTearDown;
}
public function setUp(): void
{
$this->getRestClient();
$this->fixtureSetUp();
}
...
FixtureTrait
/**
* @extends KernelTestCase<FixtureTrait>
*/
trait FixtureTrait
{
...
public function setUp(): void
{
foreach ($_ENV as $key => $value) {
putenv($key.'='.$value);
}
parent::setUp();
/** @var ContainerInterface $container */
$container = static::getContainer();
/** @var DatabaseToolCollection $databaseToolCollection */
$databaseToolCollection = $container->get(DatabaseToolCollection::class);
$this->databaseTool = $databaseToolCollection->get();
/** @var Registry $doctrine */
$doctrine = $container->get('doctrine');
/** @var EntityManager $entityManager */
$entityManager = $doctrine->getManager();
$this->entityManager = $entityManager;
$this->getRpFixture = $this->databaseTool->loadFixtures($this->_getDependencies())->getReferenceRepository();
}
...
}
And then an example of a Test :
use Liip\FunctionalTestBundle\Test\WebTestCase;
final class AffaireControllerTest extends WebTestCase
{
use SceneTrait;
...
}
Using xdebug, I see that I go through the loadFixtures() function in Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool.php
public function loadFixtures(array $classNames = [], bool $append = false): AbstractExecutor
{
$referenceRepository = new ProxyReferenceRepository($this->om);
/** @var Configuration $config */
$config = $this->om->getConfiguration();
$cacheDriver = $config->getMetadataCache();
if ($cacheDriver) {
$cacheDriver->clear();
}
if (false === $append && false === $this->getKeepDatabaseAndSchemaParameter()) {
// TODO: handle case when using persistent connections. Fail loudly?
$schemaTool = new SchemaTool($this->om);
$schemaTool->dropDatabase();
if (!empty($this->getMetadatas())) {
$schemaTool->createSchema($this->getMetadatas());
}
}
$event = new FixtureEvent();
$this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_SETUP);
$executor = $this->getExecutor($this->getPurger());
$executor->setReferenceRepository($referenceRepository);
if (false === $append) {
$executor->purge();
}
$loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
$executor->execute($loader->getFixtures(), true);
return $executor;
}
I see the right db parameters from $schemaTool->schemaManager->_conn->params. And also the right schema parameters from my entities in $this->getMetadatas(). And also my fixtures in $classnames.
In the end, when I run my test, my database in created but the schema is not created and so the Fixtures.
For the schema I added in /test/bootstrap.php
$appKernel = new Kernel('test', false);
$appKernel->boot();
$application = new Application($appKernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);
$application->run(new ArrayInput([
'command' => 'doctrine:database:drop',
'--force' => '1',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:database:create',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:schema:create',
]));
$appKernel->shutdown();
So the schema is now created but still no fixtures. And I think that I shouldn't have to do that as there is already $schemaTool->createSchema($this->getMetadatas()) in the loadFixtures() function...
Any idea on what I'm doing wrong ?
Thanks !
EDIT : Seems like the problem came form dama/doctrine-test-bundle (https://symfony/doc/current/testing.html#resetting-the-database-automatically-before-each-test). I removed the package and now it's working ...
But now, if I want to refresh/reset the database before each test (using this Dama bundle or RefreshDatabaseTrait from Hautelook Alice Bundle, I come back to the problem with empty database without schema)
Share Improve this question edited Apr 1 at 9:31 Dev Th asked Mar 31 at 12:37 Dev ThDev Th 113 bronze badges New contributor Dev Th is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0So, after some more time/issues, I figured that my phpunit/phpunit bundle version was to old (9.*) and so I updated it to works with dama/doctrine-test-bundle (need phpunit version > 10).
But in the end, I removed dama/doctrine-test-bundle and used hautelook/alice-bundle.
I had to add this code in my /test/bootstrap.php to create the db and the schema.
$appKernel = new Kernel('test', false);
$appKernel->boot();
$application = new Application($appKernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);
$application->run(new ArrayInput([
'command' => 'doctrine:database:drop',
'--force' => '1',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:database:create',
]));
$application->run(new ArrayInput([
'command' => 'doctrine:schema:create',
]));
$appKernel->shutdown();
And added use ReloadDatabaseTrait;
at the beginning of my TestClass.