fmalk/codeigniter-phpunit

Write fixtures

Closed this issue · 2 comments

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

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:

  1. create another "fixture" schema in your database, so you can insert fake data there without compromising your database.
  2. make a CREATE DATABASE script file, deleting existing tables and creating them, exactly as your production schema
  3. make as many INSERT script files as you need to populate your database with fake data, covering all possible data variation inside a table
  4. Make sure your app has a way to change to a secondary database (the fixture one). This connection will be used for fixture testing
  5. Using PHPUnit's setUpBeforeClass(), which is run once for each test class, connect to the database and reset your fixture schema
  6. 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
  7. Write your tests, this time with your app connected to the fixture schema.
  8. 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

Yes it can be help, I think that if you create some example you repository could be used from more developers :)