I am currently trying to test doctrine2.2 objects with phpUnit to use the doctrine extensions that can be found here .
This is a base class that extends my entire phpUnit TestClass.
<?php use DoctrineExtensions\PHPUnit\Event\EntityManagerEventArgs, DoctrineExtensions\PHPUnit\OrmTestCase, Doctrine\ORM\Tools\SchemaTool, Doctrine\Common\EventManager, Doctrine\ORM\Tools\Setup, Doctrine\ORM\EntityManager; class SchemaSetupListener { public function preTestSetUp(EntityManagerEventArgs $eventArgs) { $em = $eventArgs->getEntityManager(); $schemaTool = new SchemaTool($em); $cmf = $em->getMetadataFactory(); $classes = $cmf->getAllMetadata(); $schemaTool->dropDatabase(); $schemaTool->createSchema($classes); } } class EntityFunctionalTest extends OrmTestCase { protected function createEntityManager() { $config = Setup::createXMLMetadataConfiguration(array(DIR_XML_SCHEMA), true); // dev mode true $conn = array('driver' => 'pdo_sqlite', 'path' => DIR_TEST_SUITE_ROOT . 'test.db'); $conn = array('driver' => 'pdo_sqlite', 'memory' => true); $eventManager = new EventManager(); $eventManager->addEventListener(array("preTestSetUp"), new SchemaSetupListener()); return Doctrine\ORM\EntityManager::create( $conn, $config, $eventManager); } protected function getDataSet() { return $this->createFlatXmlDataSet(DIR_XML_TEST_DATA . 'db.boot.strap.test.flat.xml'); } protected function tearDown(){ $entityManager = $this->getEntityManager(); $entityManager->flush(); $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);; $entityManager->close(); } public function getSystemUser(){ return $this->getEntityManager()->createQuery('select u from User u where u.id = 1')->getSingleResult(); } } ?>
When I have the following comment below, so sqlite does not create its database in memory, everything works, but it is slow.
$conn = array('driver' => 'pdo_sqlite', 'path' => DIR_TEST_SUITE_ROOT . 'test.db'); //$conn = array('driver' => 'pdo_sqlite', 'memory' => true);
When I uncomment the line and try to run the tests using the slqlite database in memory, only the first test function gets the data from the getDataSet () method (when I run it using the sqlite database, everything is fine).
Why does the getDataSet () method work only for the first test method?
Here is the result of my test on the command line:
C: \ TestSuite \ phpUnit \ testsuites> phpUnit --configuration al
lTests.xml PHPUnit 3.6.10 by Sebastian Bergmann. Configuration read from C:\TestSuite\phpUnit\testsuites\al lTests.xml .E Time: 1 second, Memory: 16.00Mb There was 1 error: 1) UserTest::testId Doctrine\ORM\NoResultException: No result was found for query although at least one row was expected. C:\Program Files (x86)\PHP\PEAR\Doctrine\ORM\AbstractQuery.php:491 C:\TestSuite\phpUnit\testsuites\classes\entities\EntityFun ctionalTest.php:43 C:\TestSuite\phpUnit\testsuites\classes\entities\UserTest. php:60 C:\Program Files (x86)\PHP\phpunit:46 FAILURES! Tests: 2, Assertions: 8, Errors: 1.