2

For the sake of simplicity, let's say I have this schema.

A Machine, Site and User is associated with a Task. A Task can have many (say at least twenty) different TaskDetail's.

Task

task_id (int PK)
machine_id (int FK)
user_id (int FK)
site_id (int FK)
task_name (varchar)
task_time (int)

TaskDetail

task_detail_id (int PK)
task_id (int FK)
task_detail_value (varchar)

Machine

machine_id (int PK)
machine_name (varchar)

User

user_id (int PK)
user_name (varchar)
user_register_time (int)

Site

site_id (int PK)
site_name (varchar)

How can I best populate test data with Symfony? I am using PHPUnit as the testing framework.

I have written functional tests in Symfony before, but never to test data from the database. It's always been limited to simpler tests (does the dropdown menu have the following items in it, etc).

So for example, on my list of tasks page - I wanted to test that 5 tasks appear. How would I set up a test such that I'd populate the database with 5 tasks (that I imagine I will need to hard code somewhere?), then test that the output of that page shows those 5 tasks I created?

I've looked at http://symfony.com/doc/current/cookbook/testing/database.html and a few other pages and I've yet to find anything which outlines how to do what I am asking - and it seems like a fairly simple test case to me.

So any pointers are certainly welcome. Thanks.

1
  • take a look at this article Commented Jul 21, 2015 at 8:06

1 Answer 1

2

Have a look at Doctrine Fixtures, they allow you to load controlled data sets into your database for testing or even to make sure you have some initial data.

symfony2 has very good integration with doctrine fixtures through DoctrineFixturesBundle.

Write fixtures for all the entities that are needed to make sure there is enough data for tests . Then you can actually load the fixtures using the console command the bundle offers. Then you can do your functional testing as usual.

Also follow these articles to unit test the Code which interacts with database.

http://symfony.com/doc/current/cookbook/testing/database.html

http://symfony.com/doc/current/cookbook/testing/doctrine.html

Sign up to request clarification or add additional context in comments.

2 Comments

Fixtures really are the way to go. In addition to this, you can use LiipFunctionnalTestBundle which integrates them better in tests and uses a Sqlite3 DB for tests.
Thanks - that bundle looks great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.