30

I'm trying to get started with Symfony2 and have been trying to set up automated testing for the model layer of my application. The Symfony2 book talks about unit testing for controllers but I can't find many examples of model testing.

I would like to have a clean data set to work with before each test runs and found these articles:

Based on the sznapka.pl article I have a test actually running without errors, but although the test schema is created the fixtures don't load. I can't see why, or even a way to debug this.

Background: I've previously worked with CakePHP where the loading of fixtures is largely handled automatically, maybe I have the wrong approach for Symfony/Doctrine?

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
contrebis
  • 1,287
  • 1
  • 11
  • 20
  • (Starting to feel guilty about abandoning this question.) Have had to move away from Symfony explorations to other projects but hope to return soon. My feeling at the moment is that a lighter approach in Symfony is more appropriate, as suggested by jules below. For example, we don't need to test the persistence layer and we should always try to test around the edges of the object graph. – contrebis Oct 10 '11 at 01:16

3 Answers3

7

Yes DoctrineFixtures are a good choice.

To test model: you don't really need to load fixtures in the database, you should create objects with the data you want (by injecting it with setters).

To test controller: load doctrine fixtures and use doctrine transactions so the state of your database is the same before each testcase, begin transaction in setUp() and rollback in tearDow(). (If your controller use transactions too i haven't found a good solution yet).

For fixtures error, if you don't have any error and your fixtures aren't loaded maybe you have missed a naming convention. Can you show us some code ?

julesbou
  • 5,570
  • 4
  • 31
  • 36
4

Have a look at this solution. I don't think using transactions is the best idea, since chances are that you'll use transactions in your code. This solution suggests to load fixtures manually in each of your test.

Community
  • 1
  • 1
Reza S
  • 9,480
  • 3
  • 54
  • 84
4

There is a very handy LiipFunctionalTestBundle that simplifies working with a fixtures in test. The basic idea is to create a database each time you run the tests and then load fixtures. Now you can save the models, delete, every test they will be the same.

Alexey B.
  • 11,965
  • 2
  • 49
  • 73