Good day everyone,
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.
My project depends on the Symfony2.1.6 framework and Doctrine so I have some Database-tables that needs filling.
Book (1,n) - (0,n) Genre
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?)
The problem now is that I dont know how to access my Database as it is managed by the framework.
The only thing I could find was with the LiipFunctionalTestBundle https://github.com/liip/LiipFunctionalTestBundle which creates and restores a temporary database everytime I run a test. I have setup everything according to the Instructions.
Edit: My app/config/config_test.yml looks like this now:
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
So now I have a GenreTest class:
Liip doesn't have documentation so I just tried an approach like this.
use Liip\FunctionalTestBundle\Test\WebTestCase;
class GenreTest extends WebTestCase {
public function testInsert() {
$container = $this->getContainer();
$registry = $container->get('doctrine');
$em = $registry->getEntityManager(null);
$genre = new Genre();
$genre->setName("testGenre");
$em->persist($genre);
$em->flush();
$result = $em->createQuery("SELECT g FROM QkprodMangressBundle:Genre g ".
"WHERE g.name = :name")
->setParameter("name", $genre->getName())
->getResult();
$this->assertEqual($result[0]->getName(), $genre->getName());
}
}
phpunit -c web/
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.
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.
Sorry.. turned out to be a little novel here.