title: Testing database insert using Symfony
body: <p>Good day everyone,</p>
  
  <p>I have been looking into Test Driven Development a lot in the past few days and decided that I need to learn it as well. Although I can not figure out how to do it precisely.</p>
  
  <p>My project depends on the Symfony2.1.6 framework and Doctrine so I have some Database-tables that needs filling.</p>
  
  <blockquote>
    <p>Book (1,n) - (0,n) Genre</p>
  </blockquote>
  
  <p>Now if I want to insert a Genre-record I first need to write a test to ensure everything is being inserted as it should (or am I wrong?)</p>
  
  <p>The problem now is that I dont know how to access my Database as it is managed by the framework.</p>
  
  <p>The only thing I could find was with the LiipFunctionalTestBundle 
  <a href="https://github.com/liip/LiipFunctionalTestBundle" rel="nofollow">https://github.com/liip/LiipFunctionalTestBundle</a> which creates and restores a temporary database everytime I run a test. I have setup everything according to the Instructions.</p>
  
  <p><b><em>Edit:</em></b> My app/config/config_test.yml looks like this now:</p>
  
  <pre><code>imports:
      - { resource: config_dev.yml }
  
  framework:
      test: ~
      session:
          storage_id: session.storage.filesystem
  
  liip_functional_test: ~
  
  web_profiler:
      toolbar: false
      intercept_redirects: false
  
  swiftmailer:
      disable_delivery: true
  
  liip_functional_test:
      cache_sqlite_db: true
  
  doctrine:
      dbal:
          default_connection: default
          connections:
              default:
                  driver:   pdo_sqlite
                  path:     %kernel.cache_dir%/test.db
  </code></pre>
  
  <p>So now I have a GenreTest class:<br/>
  Liip doesn't have documentation so I just tried an approach like this.</p>
  
  <pre><code>use Liip\FunctionalTestBundle\Test\WebTestCase;
  class GenreTest extends WebTestCase {
      public function testInsert() {
          $container = $this-&gt;getContainer();
          $registry = $container-&gt;get('doctrine');
          $em = $registry-&gt;getEntityManager(null);       
  
          $genre = new Genre();
          $genre-&gt;setName("testGenre"); 
  
          $em-&gt;persist($genre);
          $em-&gt;flush();
  
          $result = $em-&gt;createQuery("SELECT g FROM QkprodMangressBundle:Genre g ".
                                    "WHERE g.name = :name")
                      -&gt;setParameter("name", $genre-&gt;getName())
                      -&gt;getResult();
  
          $this-&gt;assertEqual($result[0]-&gt;getName(), $genre-&gt;getName());
      }
  }
  </code></pre>
  
  <p>phpunit -c web/</p>
  
  <blockquote>
    <p>PDOException: could not find driver
    /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:36
    /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php
    :60
    /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:350
    /.../Mangress/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:949
    /.../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:306
    /.../Mangress/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:355
    /.../Mangress/app/cache/test/jms_diextra/doctrine/EntityManager_510128d0a5878.ph
    p:362
    /.../Mangress/src/Qkprod/MangressBundle/Tests/Entity/GenreTest.php:27
    FAILURES! Tests: 3, Assertions: 1, Errors: 1.</p>
  </blockquote>
  
  <p>The big question I am having is.. how do I test something like that? 
  Or do I even test database communication? Faking the database 
  communication through a custom implementation doesn't seem like a 
  good idea to me because it will use ORM and Doctrine in the production 
  environment as well.</p>
  
  <p>Sorry.. turned out to be a little novel here.</p>