0

My project uses Symfony 2.6. I am trying to isolate my test in order to don't commit any change on my database.

I manage to isolate my test with only one request. But when there are many of them, it doesn't work and I can't manage to find why. Any ideas ?

Here is my testController code :

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class RequestControllerTest extends WebTestCase{

    protected $client;
    protected $entityManager;

    protected function setUp(){
        parent::setUp();
        $this->client = static::createClient();
        $this->entityManager = $this->client->getContainer()->get('doctrine')->getManager();
        $this->entityManager->beginTransaction();
    }

    protected function tearDown(){
        parent::tearDown();
        $this->entityManager->rollback();
        $this->entityManager->close();
    }

    // This test is working just fine : the request isn't deleted from database at the end
    public function testIsolationOK(){
       $this->client->request('DELETE', self::WEB_SERVICES_EXISTING_REQUEST_URL);
       $this->client->request('GET', self::WEB_SERVICES_EXISTING_REQUEST_URL);           
    }

    // But this one isn't working : the request is deleted from database at the end
    public function testIsolationNOK(){
       $this->client->request('GET', self::WEB_SERVICES_EXISTING_REQUEST_URL);
       $this->client->request('DELETE', self::WEB_SERVICES_EXISTING_REQUEST_URL);
    }

}
emottet
  • 457
  • 6
  • 18

2 Answers2

1

To be honest, the easiest and safest way to do this kind of testing is to create a new database for testing purposes and specify its configuration in the config_test.yml.

Using this approach you'll be sure that your real DB isn't in danger of being modified by your tests and will also make your life easier when testing.

I usually use this approach though I don't know if it's what your looking for.

Documentation

Hope it helps.

acontell
  • 6,792
  • 1
  • 19
  • 32
0

I finally manage to make it work that way :

public function testIsolationOK(){
   $this->client->request('GET', self::WEB_SERVICES_EXISTING_REQUEST_URL);
   $this->setUp();
   $this->client->request('DELETE', self::WEB_SERVICES_EXISTING_REQUEST_URL);
}

I don't know if it's a right way to do but it works. As a reminder here is the setUp method :

protected function setUp(){
    parent::setUp();
    $this->client = static::createClient();
    $this->entityManager = $this->client->getContainer()->get('doctrine')->getManager();
    $this->entityManager->beginTransaction();
}
emottet
  • 457
  • 6
  • 18