0 0 0 0


I am quite new to the world of testing and I want to make sure I am on the right track.

I am trying to setup unit tests in a symfony2 project using phpunit.

PHPUnit is working and the simple default controller tests work fine. (Yet this is not about functional testing but unit testing my application.)

My project relies heavily on database interactions though, and as far as I understand from phpunit's documentation, I should set up a class based on PHPUnit_Extensions_Database_TestCase, then create fixtures for my db and work from there.

Yet, symfony2 only offers a WebTestCase class which only extends from PHPUnit_Framework_TestCase out of the box.

So am I right to assume that I should create my own DataBaseTestCase which mostly copies WebTestCase, only difference being that it extends from PHPUnit_Extensions_Database_TestCase and implements all its abstract methods?

Or is there another "built-in" recommended workflow for symfony2 concerning database-centric tests?

As I want to make sure that my models store and retrieve the right data, I do not want to end up testing the specifics of doctrine by accident.

Best Answer:


  • If and only if you want to go the whole functional test route, then I recommend looking up Sgoettschkes's answer.
  • If you want to unit test your application and have to test code that interacts with the database, either read on or jump directly to symfony2 docs

There were certain aspects in my original question that make it clear that my understanding of the differences between unit testing and functional tests was lacking. (As I have written I want to unit test the application, yet was also talking about Controller test at the same time; and those are functional test by defintion).

Unit testing only makes sense for services and not for repositories. And those services can use mocks of the entity manager.

My actual use case for my application was in fact pretty well reflected on the symfony2 docs on how to test code that interacts with the databse. They provide this example for a service test:

Service class:

use DoctrineCommonPersistenceObjectManager;
class SalaryCalculator
    private $entityManager;
    public function __construct(ObjectManager $entityManager)
        $this->entityManager = $entityManager;
    public function calculateTotalSalary($id)
        $employeeRepository = $this->entityManager
        $employee = $employeeRepository->find($id);
        return $employee->getSalary() + $employee->getBonus();

Service test class:

namespace TestsAppBundleSalary;
use AppBundleSalarySalaryCalculator;
use AppBundleEntityEmployee;
use DoctrineORMEntityRepository;
use DoctrineCommonPersistenceObjectManager;
class SalaryCalculatorTest extends PHPUnit_Framework_TestCase
    public function testCalculateTotalSalary()
        // First, mock the object to be used in the test
        $employee = $this->getMock(Employee::class);
        // Now, mock the repository so it returns the mock of the employee
        $employeeRepository = $this
        // Last, mock the EntityManager to return the mock of the repository
        $entityManager = $this
        $salaryCalculator = new SalaryCalculator($entityManager);
        $this->assertEquals(2100, $salaryCalculator->calculateTotalSalary(1));

No test database required for those kind of test, only (painful) mocking.

As it is important to test the business logic, not the persistence layer.

Only for functional test it makes sense to have its own test database that one should build and tear down afterwards, and the big question should be:

When do functional test make sense?

I used to think that test all the things is the right answer; yet after working with lots of legacy software that in itself was barely test-driven developped I have become a bit more lazypragmatic and consider certain functionality as working until proven otherwise by a bug.

Assume I have an application that parses an XML, creates an object out of it, and stores those objects into a database. If the logic that stores the objects to the database is known to work (as in: the company requires the data and is, as of yet, not broke), and even if that logic is a big ugly pile of crap, there is no imminent need to test that. As all I need to make sure that my XML parser extracts the right data. I can infer from experience that the right data will be stored.

There are scenarios where functional test are quite important, i.e. if one were to write an online shop. There it would be business critical that bought items get stored into the database and here functional test with the whole test databse makes absolute sense.

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs