Write fixtures
Closed this issue · 2 comments
AlessandroMinoccheri commented
Hi,
It will be very useful if in this repository developers can get some example.
Because, for example, I can't write fixtures because I don't know how to do it.
Can you make an example of fixtures and how to use it?
Thanks
fmalk commented
I don't know when I'm gonna have time to write proper fixtures for a test example, but I can give you a fast guide:
If you don't mind testing directly in your database, do these steps:
- create another "fixture" schema in your database, so you can insert fake data there without compromising your database.
- make a
CREATE DATABASE
script file, deleting existing tables and creating them, exactly as your production schema - make as many
INSERT
script files as you need to populate your database with fake data, covering all possible data variation inside a table - Make sure your app has a way to change to a secondary database (the fixture one). This connection will be used for fixture testing
- Using PHPUnit's
setUpBeforeClass()
, which is run once for each test class, connect to the database and reset your fixture schema - Using PHPUnit's
setUp()
, which is run once for each test, delete all data in your fixture tables and insert them again. This is standard fixture testing: at each test, you'll want to start with a known, pre-established scenario - Write your tests, this time with your app connected to the fixture schema.
- Using PHPUnit's
tearDownAfterClass()
, close your connection to the fixture database (tests open a lot of connections, it could fill your connection pool), and optionally delete your fixture schema entirely
Hope this helps for now
AlessandroMinoccheri commented
Yes it can be help, I think that if you create some example you repository could be used from more developers :)