title: How to rollback transactions when doing functional testing with Symfony2
body: <p>I'm trying to write a functional test for my project in Symfony2. I'd like to test if a user can access a page, fill up a form and submit it.
  I'm trying to find a way to rollback the database to the state it was before the test.
  I found a helper class I slightly modified at <a href="https://gist.github.com/Vp3n/5472509" rel="noreferrer">https://gist.github.com/Vp3n/5472509</a> which extends WebTestCase and overload setUp and tearDown methods.
  Here are the mods I did in order to try to make it works:</p>
  <pre><code> /**
   * Before each test we start a new transaction
   * everything done in the test will be canceled ensuring isolation et speed
   */
  protected function setUp()
  {
      parent::setUp();
      $this-&gt;client = $this-&gt;createClient();
      $this-&gt;em = static::$kernel-&gt;getContainer()
          -&gt;get('doctrine')
          -&gt;getManager();
      $this-&gt;em-&gt;getConnection()-&gt;beginTransaction();
      $this-&gt;em-&gt;getConnection()-&gt;setAutoCommit(false);
  }
  /**
   * After each test, a rollback reset the state of 
   * the database
   */
  protected function tearDown()
  {
      parent::tearDown();
      if($this-&gt;em-&gt;getConnection()-&gt;isTransactionActive()){
          echo 'existing transactions';
          $this-&gt;em-&gt;getConnection()-&gt;rollback();
          $this-&gt;em-&gt;close();
      }
  }
  </code></pre>
  <p>When I run the tests, it acknowledges for existing transactions but the rollback fails and modifications are persisted.</p>
  <p>Test Logs:</p>
  <pre><code>Runtime:       PHP 5.6.15
  
  .existing transactions.      2/2 (100%)existing transactions                                                                                      
  
  Time: 5.47 seconds, Memory: 24.50MB
  
  OK (2 tests, 5 assertions)
  </code></pre>
  <p>What am I doing wrong? Is that even the best practice?</p>
  <p><strong>EDIT</strong></p>
  <p>This worked for me:</p>
  <pre><code>abstract class DatabaseWebTest extends WebTestCase {
      /**
       * helper to acccess EntityManager
       */
      protected $em;
      /**
       * Helper to access test Client
       */
      protected $client;
      /**
       * Before each test we start a new transaction
       * everything done in the test will be canceled ensuring isolation et speed
       */
      protected function setUp()
      {
          parent::setUp();
          
          $this-&gt;client = $this-&gt;createClient(['environment' =&gt; 'test'], array(
              'PHP_AUTH_USER' =&gt; 'user',
              'PHP_AUTH_PW'   =&gt; 'password',
              ));
          $this-&gt;client-&gt;disableReboot();
          $this-&gt;em = $this-&gt;client-&gt;getContainer()-&gt;get('doctrine.orm.entity_manager');        
          $this-&gt;em-&gt;beginTransaction();
          $this-&gt;em-&gt;getConnection()-&gt;setAutoCommit(false);
      }
      /**
       * After each test, a rollback reset the state of 
       * the database
       */
      protected function tearDown()
      {
          parent::tearDown();
  
          if($this-&gt;em-&gt;getConnection()-&gt;isTransactionActive()) {
              $this-&gt;em-&gt;rollback();
          }        
      }
  }
  </code></pre>