Doctrine2.2 (sqlite using memory) using OrmTestCase with phpUnit TroubleShoot

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. 
+4
source share
2 answers

I had the same problem and the solution was to share an entity manager between tests:

 protected static $em = null; public static function setUpBeforeClass() { $isDevMode = true; $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/../src"), $isDevMode, null, null, false); $connectionOptions = array('driver' => 'pdo_sqlite', 'memory' => true); // obtaining the entity manager self::$em = EntityManager::create($connectionOptions, $config); $schemaTool = new SchemaTool(self::$em); $cmf = self::$em->getMetadataFactory(); $classes = $cmf->getAllMetadata(); $schemaTool->dropDatabase(); $schemaTool->createSchema($classes); } public static function tearDownAfterClass() { self::$em = NULL; } protected function createEntityManager() { return self::$em; } 
+4
source

In addition to what @Zedenek Machek said: in terms of a weak combination of your test, dividing lights should be avoided:

It cannot be emphasized that sharing fixtures between tests reduces the value of the tests. The main problem is that the objects are not connected to each other. You will achieve better results by solving the main design problem and then writing tests using stubs (see Chapter 9) than by creating dependencies between tests at runtime and ignoring the opportunity to improve your design.

see https://phpunit.de/manual/current/en/fixtures.html#fixtures.sharing-fixture.examples.DatabaseTest.php

0
source

Source: https://habr.com/ru/post/1402049/


All Articles