Testing database insert using Symfony

不羁的心 提交于 2019-12-03 09:13:08
Michael Kargl

I have, almost completely, solved my problem by:

  1. Install the drivers needed for db-communication ;)
    PDO_SQLITE driver not present.. what to do?

    sudo apt-get install php5-sqlite
    
  2. Recreate schema on every Test-run
    https://stackoverflow.com/a/10463614/1177024


This is my Class testing against a database
namespace Qkprod\MangressBundle\Entity;

use Qkprod\MangressBundle\Entity\Genre;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

    private $em;

    /**
     * Sets up environment for testing
     * Regenerates Database schema before every test-run
     */
    public function setUp() {
        static::$kernel = static::createKernel();
        static::$kernel -> boot();
        $this -> em = static::$kernel->getContainer()
                                     ->get('doctrine')
                                     ->getEntityManager();
        $this->regenerateSchema();
    }

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

    /**
     * Drops current schema and creates a brand new one
     */
    protected function regenerateSchema() {
        $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
        if (!empty($metadatas)) {
            $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
            $tool -> dropSchema($metadatas);
            $tool -> createSchema($metadatas);
        }
    }


    public function testInsert() {
        $genre = new Genre();
        $genre -> setName("testGenre");

        $this -> em -> persist($genre);
        $this -> em -> flush();

        $result = $this -> em 
                    -> createQuery("SELECT g "
                                 . " FROM QkprodMangressBundle:Genre g " 
                                 . " WHERE g.name = :name") 
                    -> setParameter("name", $genre -> getName())
                    -> getResult();

        $this -> assertEquals($result[0] -> getName(), $genre -> getName());
    }
}


Could not get the LiipBundle to work but you can increase speed incredibly by setting up symfony to save the sqlite db in memory instead of filesystem. https://stackoverflow.com/a/10460139/1177024

# app/config/config_test
doctrine:
    dbal:
        driver: pdo_sqlite
        path: :memory:
        memory: true


Or maybe only create the schema once and instead of recreating it, just override the database with a fresh backup.

I hope this shortens the search for some people having the same problem! Thanks to everyone helping me along the way :) You are great!

Try it again with the symfony docs.

http://symfony.com/doc/2.0/cookbook/testing/doctrine.html

Or you can modify your TestCase constructor with:

public function __construct()
{
    $kernelNameClass = $this->getKernelClass();
    $kernel = new $kernelNameClass('test', true);
    $kernel->boot();
    $this->em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
}

Then you will use

$this->em->createQuery($dql);

etc.

You can connect inside of the setUp() method:

/**
 * @var \Doctrine\ORM\EntityManager
 */
private $em;

/**
 * {@inheritDoc}
 */
public function setUp()
{
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->em = static::$kernel->getContainer()
        ->get('doctrine')
        ->getManager()
    ;
}

Here an exmple from the Symfony 2.1 book

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!