I'm adding tests to a Symfony2 project. Previously I used the same database for dev
and test
environments, it used a MySQL database already populated with the same data than on the production server.
The tests were working dependently because some tests depended on previous tests. For example if I had a shop website, I added a product in the cart then removed the product from the cart. So I needed to insert data by using a form, before being able to remove it.
Now I want to work with independent functional tests, because that's the recommended way (by one of Symfony2's developers).
I've configured LiipFunctionalTestBundle correctly to use a SQLite database in the test
environment and I've started to add fixtures with DoctrineFixturesBundle.
But I don't know how much data I have to load for each functional test. What fixture should I load at the beginning of a test? How to deal with CRUD operations when the entity depends on other entities because of relationships between tables?
Let's say I'm developing a shop, I want a few tests:
- The user add some products in its cart
- The user remove one product from its cart
- The user order the remaining products
Should I create a different fixture for every step? It means that my fixtures will need to exist in many different states: empty cart, cart with one product ordered, etc. It seems correct to me but very time consuming, so I'm wondering if my idea is valid.
For each test case is better to load less fixture as possible both for isolation and for performance (the test suite can go very slowly).
When fixture depends each other, you simply manage them with the doctrine reference and link each other, take care of the order also. As Example, suppose the simply user and role relations.
A generic class for manage role fixture:
A Dedicated class for the Simple Role
A Dedicated class for the Admin Role
And the user: A generic class for manage user fixture:
A Dedicated class for the Simple User
A Dedicated class for the Admin User
Now you can use it separately, as example, based on the Liip Functional Test Bundle:
Hope this help.