1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] Some fixes for ClassExporterTest.

This commit is contained in:
romanb 2009-02-17 12:25:03 +00:00
parent 32d43c365f
commit 87fd08e427
13 changed files with 104 additions and 60 deletions

View File

@ -181,6 +181,16 @@ class Connection
$this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
}
/**
* Gets the DBAL driver instance.
*
* @return Doctrine\DBAL\Driver
*/
public function getDriver()
{
return $this->_driver;
}
/**
* Gets the Configuration used by the Connection.
*

View File

@ -16,7 +16,7 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL;

View File

@ -1013,13 +1013,13 @@ abstract class AbstractPlatform
}
}
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields;
$query = 'CREATE TABLE ' . $this->quoteIdentifier($table, true) . ' (' . $queryFields;
$check = $this->getCheckDeclaration($columns);
/*$check = $this->getCheckDeclaration($columns);
if ( ! empty($check)) {
$query .= ', ' . $check;
}
}*/
$query .= ')';

View File

@ -114,7 +114,6 @@ class MySqlPlatform extends AbstractPlatform
);*/
/**
* Constructor.
* Creates a new MySqlPlatform instance.
*/
public function __construct()
@ -1071,26 +1070,13 @@ class MySqlPlatform extends AbstractPlatform
/** @override */
protected function _getCommonIntegerTypeDeclarationSql(array $columnDef)
{
$default = $autoinc = '';
$autoinc = '';
if ( ! empty($columnDef['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT';
} elseif (array_key_exists('default', $columnDef)) {
if ($columnDef['default'] === '') {
$columnDef['default'] = empty($columnDef['notnull']) ? null : 0;
}
if (is_null($columnDef['default'])) {
$default = ' DEFAULT NULL';
} else {
$default = ' DEFAULT '.$this->quote($columnDef['default']);
}
} elseif (empty($columnDef['notnull'])) {
$default = ' DEFAULT NULL';
}
$notnull = (isset($columnDef['notnull']) && $columnDef['notnull']) ? ' NOT NULL' : '';
$unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : '';
return $unsigned . $default . $notnull . $autoinc;
return $unsigned . $autoinc;
}
/**
@ -1104,33 +1090,31 @@ class MySqlPlatform extends AbstractPlatform
*/
public function getDefaultFieldDeclarationSql($field)
{
$default = empty($field['notnull']) && !in_array($field['type'], array('clob', 'blob'))
? ' DEFAULT NULL' : '';
$default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) {
if ($field['default'] === '') {
$field['default'] = null;
if (! empty($field['notnull']) && array_key_exists($field['type'], $this->valid_default_values)) {
/*if ( ! empty($field['notnull']) && array_key_exists($field['type'], $this->valid_default_values)) {
$field['default'] = $this->valid_default_values[$field['type']];
}
if ($field['default'] === ''
&& ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
) {
$field['default'] = ' ';
}
}*/
}
if ($field['type'] == 'enum' && $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
/*if ($field['type'] == 'enum' && $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
$fieldType = 'varchar';
} else {
if ($field['type'] === 'boolean') {
$fields['default'] = $this->convertBooleans($field['default']);
}
$fieldType = $field['type'];
}
}*/
$default = ' DEFAULT ' . $this->quote($field['default'], $fieldType);
$default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
}
return $default;
}

View File

@ -453,7 +453,7 @@ class SqlitePlatform extends AbstractPlatform
$autoinc = ! empty($columnDef['autoincrement']) ? ' AUTOINCREMENT' : '';
$pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? ' PRIMARY KEY' : '';
return "INTEGER $pk $autoinc";
return "INTEGER" . $pk . $autoinc;
}
/**
@ -513,13 +513,13 @@ class SqlitePlatform extends AbstractPlatform
$name = $this->quoteIdentifier($name, true);
$sql = 'CREATE TABLE ' . $name . ' (' . $queryFields;
if ($check = $this->getCheckDeclarationSql($fields)) {
/*if ($check = $this->getCheckDeclarationSql($fields)) {
$sql .= ', ' . $check;
}
if (isset($options['checks']) && $check = $this->getCheckDeclarationSql($options['checks'])) {
$sql .= ', ' . $check;
}
}*/
$sql .= ')';
@ -527,7 +527,7 @@ class SqlitePlatform extends AbstractPlatform
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index => $definition) {
$query[] = $this->createIndexSql($name, $index, $definition);
$query[] = $this->getCreateIndexSql($name, $index, $definition);
}
}
return $query;

View File

@ -94,6 +94,7 @@ class ClassExporter
$column['notnull'] = ! $mapping['nullable'];
if ($class->isIdentifier($fieldName)) {
$column['primary'] = true;
$options['primary'][] = $mapping['columnName'];
if ($class->isIdGeneratorIdentity()) {
$column['autoincrement'] = true;
}
@ -124,6 +125,7 @@ class ClassExporter
} else if ($mapping->isManyToMany() && $mapping->isOwningSide()) {
//... create join table
$joinTableColumns = array();
$joinTableOptions = array();
$joinTable = $mapping->getJoinTable();
$constraint1 = array();
$constraint1['tableName'] = $joinTable['name'];
@ -133,6 +135,7 @@ class ClassExporter
foreach ($joinTable['joinColumns'] as $joinColumn) {
$column = array();
$column['primary'] = true;
$joinTableOptions['primary'][] = $joinColumn['name'];
$column['name'] = $joinColumn['name'];
$column['type'] = $class->getTypeOfColumn($joinColumn['referencedColumnName']);
$joinTableColumns[$joinColumn['name']] = $column;
@ -149,6 +152,7 @@ class ClassExporter
foreach ($joinTable['inverseJoinColumns'] as $inverseJoinColumn) {
$column = array();
$column['primary'] = true;
$joinTableOptions['primary'][] = $inverseJoinColumn['name'];
$column['name'] = $inverseJoinColumn['name'];
$column['type'] = $this->_em->getClassMetadata($mapping->getTargetEntityName())
->getTypeOfColumn($inverseJoinColumn['referencedColumnName']);
@ -158,7 +162,8 @@ class ClassExporter
}
$foreignKeyConstraints[] = $constraint2;
$sql = array_merge($sql, $this->_platform->getCreateTableSql($joinTable['name'], $joinTableColumns, array()));
$sql = array_merge($sql, $this->_platform->getCreateTableSql(
$joinTable['name'], $joinTableColumns, $joinTableOptions));
}
}
@ -168,7 +173,7 @@ class ClassExporter
// Now create the foreign key constraints
if ($this->_platform->supportsForeignKeyConstraints()) {
foreach ($foreignKeyConstraints as $fkConstraint) {
$sql = array_merge($sql, $this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint));
$sql = array_merge($sql, (array)$this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint));
}
}

View File

@ -8,7 +8,8 @@ class ConnectionMock extends \Doctrine\DBAL\Connection
private $_lastInsertId = 0;
private $_inserts = array();
public function __construct() {
public function __construct(array $params, $driver, $config = null, $eventManager = null) {
parent::__construct($params, $driver, $config, $eventManager);
$this->_platformMock = new DatabasePlatformMock();
$this->_platform = $this->_platformMock;
}

View File

@ -3,11 +3,10 @@
namespace Doctrine\Tests\Mocks;
// THIS FILE DOES NOT EXIST YET!!!!
//require_once 'lib/mocks/Doctrine_SchemaManagerMock.php';
class DriverMock implements \Doctrine\DBAL\Driver
{
private $_platformMock;
public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{
return new DriverConnectionMock();
@ -24,14 +23,30 @@ class DriverMock implements \Doctrine\DBAL\Driver
return "";
}
/**
* @override
*/
public function getDatabasePlatform()
{
return new DatabasePlatformMock();
if ( ! $this->_platformMock) {
$this->_platformMock = new DatabasePlatformMock;
}
return $this->_platformMock;
}
/**
* @override
*/
public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
{
return new SchemaManagerMock($conn);
}
/* MOCK API */
public function setDatabasePlatform(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
$this->_platformMock = $platform;
}
}

View File

@ -0,0 +1,20 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
namespace Doctrine\Tests\Mocks;
/**
* Description of SchemaManagerMock
*
* @author robo
*/
class SchemaManagerMock extends \Doctrine\DBAL\Schema\AbstractSchemaManager
{
public function __construct(\Doctrine\DBAL\Connection $conn) {
parent::__construct($conn);
}
}

View File

@ -24,7 +24,7 @@ class EntityPersisterTest extends \Doctrine\Tests\OrmTestCase
protected function setUp() {
parent::setUp();
$this->_connMock = new ConnectionMock(array());
$this->_connMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
$this->_emMock = EntityManagerMock::create($this->_connMock);
$this->_uowMock = new UnitOfWorkMock($this->_emMock);
$this->_emMock->setUnitOfWork($this->_uowMock);

View File

@ -16,7 +16,7 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Tests\ORM\Export;
@ -40,15 +40,22 @@ class ClassExporterTest extends \Doctrine\Tests\OrmTestCase
{
public function testTest()
{
/*
$em = $this->_getTestEntityManager();
// DDL is platform dependant. We can inject the platform to test into the driver mock.
$driver = new \Doctrine\Tests\Mocks\DriverMock;
$conn = new \Doctrine\Tests\Mocks\ConnectionMock(array(), $driver);
//$conn->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SqlitePlatform());
$conn->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform());
$classes = array($em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'));
$em = $this->_getTestEntityManager($conn);
$classes = array(
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber')
);
$exporter = new ClassExporter($em);
$sql = $exporter->getExportClassesSql($classes);
print_r($sql);
exit('test');
*/
}
}

View File

@ -27,7 +27,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
protected function setUp() {
parent::setUp();
$this->_connectionMock = new ConnectionMock(array());
$this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
// SUT
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);

View File

@ -15,18 +15,20 @@ class OrmTestCase extends DoctrineTestCase
*
* @return Doctrine\ORM\EntityManager
*/
protected function _getTestEntityManager($conf = null, $eventManager = null)
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null)
{
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl());
$eventManager = new \Doctrine\Common\EventManager();
$connectionOptions = array(
if (is_null($conn)) {
$conn = array(
'driverClass' => 'Doctrine\Tests\Mocks\DriverMock',
'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock',
'user' => 'john',
'password' => 'wayne'
);
return \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager);
}
return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager);
}
private static function getSharedMetadataCacheImpl()