Skip to content

Commit

Permalink
refactor the datbaseTestCase structure and integrate into the entitie…
Browse files Browse the repository at this point in the history
…s testcase

fixup code coverage for the new DatabaseTestCase
  • Loading branch information
pscheit committed Nov 6, 2013
1 parent d111b8c commit 07a202a
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 111 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
"php": ">=5.3.2",
"doctrine/orm": "2.4.*@stable"
},
"suggests": {
"doctrine/data-fixtures": "to use the database testcase"
},
"require-dev": {
"webforge/webforge": "dev-master",
"webforge/testdata-repository": "*@alpha",
"webforge/testplate": ">=1.2@dev",
"mockery/mockery": "dev-master",
"liip/rmt": "0.9.*",
"satooshi/php-coveralls": "dev-master"
"satooshi/php-coveralls": "dev-master",
"doctrine/data-fixtures": "1.0.*@dev"
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 5 additions & 0 deletions lib/Webforge/Doctrine/Fixtures/FixturesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Common\DataFixtures\FixtureInterface as DCFixture;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\ORM\EntityManager;
use RuntimeException;

class FixturesManager {

Expand All @@ -18,6 +19,10 @@ class FixturesManager {

public function __construct(EntityManager $em) {
$this->em = $em;

if (!class_exists('Doctrine\Common\DataFixtures\Executor\ORMExecutor', $autoload=true)) {
throw new RuntimeException('To use the fixtures manager you need to install doctrine/data-fixtures: '."\ncomposer require --dev doctrine/data-fixtures:1.0.*@dev");
}
}

public function add(DCFixture $fixture) {
Expand Down
26 changes: 26 additions & 0 deletions lib/Webforge/Doctrine/Test/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,36 @@ class Base extends \Webforge\Code\Test\Base {
*/
protected $dcc;

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

/**
* @var array
*/
protected $entitiesPaths;

/**
* @var string
*/
protected $con = 'tests';

public function setUp() {
parent::setUp();

$this->mocker = new Mocker($this);
$this->dcc = new Container();
}

protected function initDoctrineContainer() {
$this->dcc->initDoctrine(
$this->frameworkHelper->getBootContainer()->getProject()->getConfiguration()->get(array('db')),
$this->entitiesPaths = array($this->getTestDirectory('Entities/'))
);
}

protected function setUpEntityManager() {
$this->em = $this->dcc->getEntityManager($this->con, $reset = TRUE, $resetConnection = TRUE);
}
}
148 changes: 54 additions & 94 deletions lib/Webforge/Doctrine/Test/DatabaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

namespace Psc\Doctrine;
namespace Webforge\Doctrine\Test;

use Psc\Code\Code;
use Webforge\Common\ArrayUtil AS A;
use Psc\Doctrine\Helper as DoctrineHelper;
use Doctrine\DBAL\Logging\EchoSQLLogger;
use Webforge\Doctrine\Fixtures\FixturesManager;

/**
* Basisklasse für Datenbanktests
* Use this as a starting point for your database tests
*
* die Klasse ruft bei Default vor dem ersten Test die Mainfixture auf (und das nur einmal)
* soll während des Tests die Datenbank neu geladen werden kann der test einfach self::$setupDatabase = TRUE; setzen
* setUpDatabase() kann überschrieben werden für weitere fixtures
*/
abstract class DatabaseTestCase extends \Psc\Code\Test\HTMLTestCase {
abstract class DatabaseTestCase extends \Webforge\Doctrine\Test\SchemaTestCase {

/**
* Ist dies während der setUp()-Method == true dann wird die gesetzte Fixture geladen
Expand All @@ -27,41 +24,15 @@ abstract class DatabaseTestCase extends \Psc\Code\Test\HTMLTestCase {
*/
protected $backupGlobals = FALSE;

/**
* @var string
*/
protected $con = 'tests';

/**
* @var Psc\Doctrine\DCPackage
*/
protected $dc;

/**
* @var Psc\Doctrine\Module
*/
protected $module;

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

/**
* @var Doctrine\DBAL\Schema\AbstractSchemaManager
*/
protected $sm;

/**
* @var Psc\Doctrine\FixturesManager
*/
protected $dcFixtures;

/**
* Set this to add to dcFixtures at setup
* @var array
* @var Webforge\Doctrine\Test\FixturesManager
*/
protected $fixtures = NULL;
protected $fm;

/**
*
Expand All @@ -75,11 +46,7 @@ public static function setUpBeforeClass() {
public function setUp() {
parent::setUp();

$this->module = $this->getModule('Doctrine');
$this->setUpModule($this->module);
$this->setUpEntityManager();
$this->dc = $this->getDoctrinePackage();

// dcc and em are defined from schematestcase and base
$this->setUpFixturesManager();

if (self::$setupDatabase) {
Expand All @@ -88,90 +55,77 @@ public function setUp() {
}
}

protected function setUpModule($module) {}
protected function setUpFixturesManager() {
$this->fm = new FixturesManager($this->em);

foreach ($this->getFixtures() as $fixture) {
$this->fm->add($fixture);
}
}

protected function setUpEntityManager() {
$this->em = $this->module->getEntityManager($this->con, $reset = TRUE, $resetConnection = TRUE);
protected function getFixtures() {
return array();
}

/**
* Wird nur einmal Pro TestKlasse aufgerufen
*
* code in setup der sachen aus der Datenbank braucht muss nach parent::setUp() stehen
*
* setUpDatabase()
* Setups the fixtures and the datbase
*
* per default this does $this->fm->execute()
* you can override this behaviour for your own test
* but be aware: this function is called only ONCE in a test-suite
* you can trigger further calls to this when you call resetDatabaseOnNextTest
*
* here is a log how this would be called:
*
* setUp()
* setUpDatabase()
* firstTest()
* tearDown()
*
* setUp()
* secondTest()
* $this->resetDatbaseOnNextTest()
* tearDown()
*
* setUp()
* setUpDatabase()
* thirdTest()
* tearDown()
*/
protected function setUpDatabase() {
$this->dcFixtures->execute();
}

protected function setUpFixturesManager() {
$this->dcFixtures = new FixturesManager($this->em);
$this->setUpFixtures();
}

public function setUpFixtures() {
if (Code::isTraversable($this->fixtures)) {
foreach ($this->fixtures as $fixture) {
$this->dcFixtures->add($fixture);
}
}
$this->fm->execute();
}

/**
* Call this to trigger setUpDatabase for the NEXT test
*/
protected function resetDatabaseOnNextTest() {
self::$setupDatabase = TRUE;
}

/**
* @return Psc\Doctrine\DCPackage
*/
public function getDoctrinePackage() {
if (!isset($this->dc)) {
$this->dc = new DCPackage($this->module, $this->em);
}
return $this->dc;
}

/**
* @return ClassMetadata
*/
public function getEntityMetadata($entityName) {
return $this->em->getMetaDataFactory()->getMetadataFor($this->getEntityName($entityName));
}

public function getSchemaManager() {
if (!isset($this->sm)) {
$this->sm = $this->em->getConnection()->getSchemaManager();
}

return $this->sm;
}

/**
* @return Psc\Doctrine\Mocks\EntityManager
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManagerMock() {
return $this->doublesManager->createEntityManagerMock($this->module, $this->con);
return $this->mocker->createEntityManager();
}

/**
* Cleanup open transactions
*/
protected function onNotSuccessfulTest(\Exception $e) {
if (isset($this->em) && $this->em->getConnection()->isTransactionActive()) {
$this->em->getConnection()->rollback();
}

parent::onNotSuccessfulTest($e);
return parent::onNotSuccessfulTest($e);
}


Expand All @@ -184,17 +138,25 @@ public function getRepository($name) {
}

/**
* @return string
* override this for shorter names in your tests
*/
public function getEntityName($shortName) {
return $this->module->getEntityName($shortName);
protected function getEntityName($shortName) {
return $shortName;
}

public function getSchemaManager() {
if (!isset($this->sm)) {
$this->sm = $this->em->getConnection()->getSchemaManager();
}

return $this->sm;
}

/**
* @chainable
*/
public function startDebug() {
$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Psc\Doctrine\FlushSQLLogger);
$this->em->getConnection()->getConfiguration()->setSQLLogger(new EchoSQLLogger());
return $this;
}

Expand All @@ -206,18 +168,14 @@ public function stopDebug() {
return $this;
}

public function dump($var, $depth = 3) {
\Psc\Doctrine\Helper::dump($var, $depth);
}

/**
* Hydrates one entity by criterias or by identifier
*
* @param int|string|array an identifier or an array
* @return object<$entity>
*/
public function hydrate($entity, $data) {
if (is_array($data) && !A::isNumeric($data)) {// numeric bedeutet composite key (z.b. OID)
if (is_array($data) && !A::isNumeric($data)) { // numeric bedeutet composite key (z.b. OID)
return $this->getRepository($entity)->hydrateBy($data);
} else {
return $this->getRepository($entity)->hydrate($data);
Expand All @@ -227,12 +185,14 @@ public function hydrate($entity, $data) {
/* ASSERTIONS */

/**
* Asserted 2 Collections mit einem Feld als vergleicher
* Asserts that 2 collections are really equal
*/
/*
public function assertCollection($expected, $actual, $compareFieldGetter = 'identifier') {
$this->assertEquals(DoctrineHelper::map($expected, $compareFieldGetter),
DoctrineHelper::map($actual, $compareFieldGetter),
'Collections sind nicht gleich'
);
}
*/
}
6 changes: 2 additions & 4 deletions lib/Webforge/Doctrine/Test/SchemaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ class SchemaTestCase extends Base {
public function setUp() {
parent::setUp();

$this->dcc->initDoctrine(
$this->frameworkHelper->getBootContainer()->getProject()->getConfiguration()->get(array('db')),
$this->entitiesPaths = array($this->getTestDirectory('Entities/'))
);
$this->initDoctrineContainer();
$this->setUpEntityManager();

if (!self::$schemaCreated) {
$this->dcc->getUtil()->updateSchema('tests', Util::FORCE, $eol = "\n");
Expand Down

0 comments on commit 07a202a

Please sign in to comment.