Files
core/tests/ModelTestCase.php
T
Vassyli 5c3fd4714d Adds fixes and tests for cascade=persist, remove for scene entities.
It still looks like doctrine doesn't "know" about the column names in a cascade=remove relationship and assumes the property name to be also the column name - which is usually not true (by default, it's propertyname_id).

This update changes the column name so that doctrine's assumptions are correct again and adds tests so any changes which invalidates this relationship can be gecocnized easily.
2017-03-11 12:51:25 +01:00

92 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace LotGD\Core\Tests;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\AnsiQuoteStrategy;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\Tools\SchemaTool;
use LotGD\Core\Configuration;
use LotGD\Core\ComposerManager;
use LotGD\Core\LibraryConfigurationManager;
use LotGD\Core\Exceptions\InvalidConfigurationException;
/**
* Description of ModelTestCase
*/
abstract class ModelTestCase extends \PHPUnit_Extensions_Database_TestCase
{
/** @var \PDO */
static private $pdo = null;
/** @var EntityManager */
static private $em = null;
/** @var \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection */
private $connection = null;
/**
* Returns a connection to test models
* @return \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
*/
final public function getConnection(): \PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
{
if ($this->connection === null) {
$configFilePath = getenv('LOTGD_TESTS_CONFIG_PATH');
if ($configFilePath === false || strlen($configFilePath) == 0 || is_file($configFilePath) === false) {
throw new InvalidConfigurationException("Invalid or missing configuration file: '{$configFilePath}'.");
}
$config = new Configuration($configFilePath);
if (self::$pdo === null) {
self::$pdo = new \PDO($config->getDatabaseDSN(), $config->getDatabaseUser(), $config->getDatabasePassword());
// Read db annotations from model files
$composerManager = new ComposerManager(getcwd());
$libraryConfigurationManager = new LibraryConfigurationManager($composerManager, getcwd());
$directories = $libraryConfigurationManager->getEntityDirectories();
$directories[] = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'src', 'Models']);
$directories[] = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Resources', 'TestModels']);
// Read db annotations from model files
$configuration = Setup::createAnnotationMetadataConfiguration($directories, true);
$configuration->setQuoteStrategy(new AnsiQuoteStrategy());
self::$em = EntityManager::create(["pdo" => self::$pdo], $configuration);
// Create Schema
$metaData = self::$em->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool(self::$em);
$schemaTool->updateSchema($metaData);
}
$this->connection = $this->createDefaultDBConnection(self::$pdo, $config->getDatabaseName());
}
return $this->connection;
}
/**
* Returns the current entity manager
* @return EntityManagerInterface
*/
protected function getEntityManager(): EntityManagerInterface
{
return self::$em;
}
protected function tearDown() {
parent::tearDown();
// Clear out the cache so tests don't get confused.
$this->getEntityManager()->clear();
}
protected function flushAndClear()
{
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
}
}