Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use this class:</p> <pre><code>&lt;?php namespace Project\Bundle\Tests; require_once dirname(__DIR__).'/../../../app/AppKernel.php'; use Doctrine\ORM\Tools\SchemaTool; abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * @var Symfony\Component\HttpKernel\AppKernel */ protected $kernel; /** * @var Doctrine\ORM\EntityManager */ protected $entityManager; /** * @var Symfony\Component\DependencyInjection\Container */ protected $container; public function setUp() { // Boot the AppKernel in the test environment and with the debug. $this-&gt;kernel = new \AppKernel('test', true); $this-&gt;kernel-&gt;boot(); // Store the container and the entity manager in test case properties $this-&gt;container = $this-&gt;kernel-&gt;getContainer(); $this-&gt;entityManager = $this-&gt;container-&gt;get('doctrine')-&gt;getEntityManager(); // Build the schema for sqlite $this-&gt;generateSchema(); parent::setUp(); } public function tearDown() { // Shutdown the kernel. $this-&gt;kernel-&gt;shutdown(); parent::tearDown(); } protected function generateSchema() { // Get the metadatas of the application to create the schema. $metadatas = $this-&gt;getMetadatas(); if ( ! empty($metadatas)) { // Create SchemaTool $tool = new SchemaTool($this-&gt;entityManager); $tool-&gt;createSchema($metadatas); } else { throw new Doctrine\DBAL\Schema\SchemaException('No Metadata Classes to process.'); } } /** * Overwrite this method to get specific metadatas. * * @return Array */ protected function getMetadatas() { return $this-&gt;entityManager-&gt;getMetadataFactory()-&gt;getAllMetadata(); } } </code></pre> <p>And then you can test your entity. Something like this (assuming you have a entity User)</p> <pre><code>//Entity Test class EntityTest extends TestCase { protected $user; public function setUp() { parent::setUp(); $this-&gt;user = new User(); $this-&gt;user-&gt;setUsername('username'); $this-&gt;user-&gt;setPassword('p4ssw0rd'); $this-&gt;entityManager-&gt;persist($this-&gt;user); $this-&gt;entityManager-&gt;flush(); } public function testUser(){ $this-&gt;assertEquals($this-&gt;user-&gt;getUserName(), "username"); ... } } </code></pre> <p>Hope this help.</p> <p>Source: theodo.fr/blog/2011/09/symfony2-unit-database-tests</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload