I would like to write end to end tests in symfony. Before running the test, I would like to load data into the database using fixtures. The code executes well however, the data is not persisted in the database. We notice all the same that the indexes of the tables affected by the fixtures have changed.
How do I keep data loaded by fixtures in my test database? Or m’I missing something on how it should work ?
Here is my code :
- An abstract class for all tests :
abstract class AbstractWebTestCase extends WebTestCase
{
use TraitJsonSerializer, PantherTestCaseTrait;
protected AbstractDatabaseTool $databaseTool;
protected Client $client;
protected EntityManager $entityManager;
protected function setUp(): void
{
parent::setUp();
self::stopWebServer();
$this->client = self::createPantherClient();
$this->databaseTool = static::getContainer()->get(DatabaseToolCollection::class)->get();
$this->entityManager = static::getContainer()->get('doctrine')->getManager();
}
- The test that should persist datas :
public function testPantherConnexion() : void
{
$references = $this->getDatabaseTool()->loadFixtures([
Fixture::class
], true)->getReferenceRepository();
$this->client->request('GET', '/xxxxx');
$crawler = $this->client->waitForVisibility('#ktAppBody');
$this->client->submit(ConnexionForm::CREATE_FORM($crawler));
$lastInsertedId = $this->entityManager->getRepository(CompteUtilisateur::class)
->createQueryBuilder('a')
->select("MAX(a.id) as maxId")
->getQuery()
->getSingleResult()['maxId'];
$compte = $this->entityManager->getRepository(CompteUtilisateur::class)->findOneById($lastInsertedId);
// Trigger an error to stop the panther process and debug the view
// pause() doesn't exist
$this->client->pause();
}
Here, the $compte inserted by the fixture is present, but when i look into my database using phpMyAdmin, there is none.
- My .env.test file :
# define your env variables for the test env here
KERNEL_CLASS='AppKernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=test
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
DATABASE_URL="pdo_mysql://root:[email protected]:3306/xxxxxxxx?serverVersion=15&charset=utf8mb4"
PANTHER_CHROME_DRIVER_BINARY=%kernel.project_dir%/root/drivers
PANTHER_NO_HEADLESS=1
PANTHER_NO_SANDBOX=1
PANTHER_ERROR_SCREENSHOT_DIR=%kernel.project_dir%/root/var/error-screenshots
- dama configuration :
#config/packages/test/dama_doctrine_test_bundle.yaml
dama_doctrine_test:
enable_static_connection: true
enable_static_meta_data_cache: true
enable_static_query_cache: true
- liip configuration :
#config/packages/test/liip_fixtures.yaml
liip_test_fixtures:
keep_database_and_schema: true
cache_metadata: true
cache_db: ~
- framework configuration :
#config/packages/framework.yaml
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
csrf_protection: true
http_method_override: false
profiler:
only_exceptions: false
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
#esi: true
#fragments: true
php_errors:
log: true
when@test:
framework:
test: true
session:
storage_factory_id: session.storage.factory.mock_file
- doctrine configuration
doctrine:
dbal:
#url: '%env(resolve:DATABASE_URL)%'
dbname: '%env(resolve:DBNAME)%'
charset: '%env(resolve:CHARSET)%'
user: 'root' #'%env(resolve:LOGIN)%'
password: 'root' #'%env(resolve:PASSWORD)%'
driver: '%env(resolve:DRIVER)%'
host: '%env(resolve:HOST)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '15'
orm:
dql:
string_functions:
YEAR: DoctrineExtensionsQueryMysqlYear
MONTH: DoctrineExtensionsQueryMysqlMonth
DAY: DoctrineExtensionsQueryMysqlDay
NOW: DoctrineExtensionsQueryMysqlNow
auto_generate_proxy_classes: true
enable_lazy_ghost_objects: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
# gedmo_translatable:
# type: annotation
# prefix: GedmoTranslatableEntity
# dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity"
# alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
# is_bundle: false
# gedmo_translator:
# type: annotation
# prefix: GedmoTranslatorEntity
# dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translator/Entity"
# alias: GedmoTranslator # (optional) it will default to the name set for the mapping
# is_bundle: false
gedmo_loggable:
type: attribute
prefix: GedmoLoggableEntity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Loggable/Entity"
alias: GedmoLoggable # (optional) it will default to the name set for the mapping
is_bundle: false
# gedmo_tree:
# type: annotation
# prefix: GedmoTreeEntity
# dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Tree/Entity"
# alias: GedmoTree # (optional) it will default to the name set for the mapping
# is_bundle: false
Entity:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'Entity'
alias: Entity
RelationalEntity:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity/RelationalEntity'
prefix: 'RelationalEntity'
alias: RelationalEntity
when@test:
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
when@prod:
doctrine:
orm:
auto_generate_proxy_classes: false
proxy_dir: '%kernel.build_dir%/doctrine/orm/Proxies'
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
I tried to insert data in my test database in a panther test case. I expect to find my data into the database DURING the test and eventually after the test but i know that liip purge the database after the test. However, i have inserted by hand some data in the database and they aren’t purged at all.
Thanks in advance for your help 🙂