How to inject default datas in my application : with Data Fixtures.
Sixth day of our small CMS with Symfony howto. Previous article Sonata Page Installation.
In a previous article on BDD, we’ve installed some bundles that we are not using yet DoctrineFixturesBundle
Doctrine Data Fixtures Extension for Behat (in composer.json : doctrine/data-fixtures, doctrine/doctrine-fixtures-bundle and vipsoft/doctrine-data-fixtures-extension).
Those bundles will help us inject default data in our application before tests or before another project phase (staging, prod, etc).
The only user we’ve created was created with the FOS User commend :
We can automate this task.
Create a file src/Application/Sonata/UserBundle/DataFixtures/ORM/010-LoadUserData.php
This file will load datas from src/Application/Sonata/UserBundle/DataFixtures/ORM/010-LoadUserData.yml
Now if you run a :
This will be useful to populate our database with known datas each time we run a test.
To make that our fixtures are loaded for each Feature, we’ll uncomment the four last lines in our behat.yml file:
To verify this, we just rebuild an empty database :
And run our tests.
All test are green.
We can for example test if a normal user can access our admin.
It is supposed to get an “access denied” response.
If you implement iAmLoggedInAsBob step in your context (see previous article). All the lights a greens.
See you next time for an article on how to use sonata.
Edit oct 22 : Implementation for the iAmLoggedInAsBob step. Note that we refactor ** iAmLoggedInAsAdmin** step.