2013-12-17 21:56:13 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
|
|
|
|
2015-01-14 20:31:11 +03:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Tools\ToolsException;
|
2013-12-17 21:56:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class makes tests on the correct use of a database schema when entities are stored
|
|
|
|
*
|
|
|
|
* @group DDC-2825
|
|
|
|
*/
|
|
|
|
class DDC2825Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function setup()
|
|
|
|
{
|
|
|
|
parent::setup();
|
|
|
|
|
|
|
|
$platform = $this->_em->getConnection()->getDatabasePlatform();
|
|
|
|
|
|
|
|
if ( ! $platform->supportsSchemas() && ! $platform->canEmulateSchemas()) {
|
|
|
|
$this->markTestSkipped("This test is only useful for databases that support schemas or can emulate them.");
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:31:11 +03:00
|
|
|
try {
|
|
|
|
$this->_schemaTool->createSchema(array(
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2825MySchemaMyTable'),
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2825MySchemaMyTable2'),
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2825MySchemaOrder'),
|
|
|
|
));
|
|
|
|
} catch (ToolsException $e) {
|
|
|
|
// tables already exist
|
|
|
|
}
|
2013-12-17 21:56:13 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:47:17 +03:00
|
|
|
public function testMappingsContainCorrectlyDefinedOrEmulatedSchema()
|
2013-12-17 21:56:13 +04:00
|
|
|
{
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->checkClassMetadata(DDC2825ClassWithExplicitlyDefinedSchema::CLASSNAME, 'myschema', 'mytable');
|
|
|
|
$this->checkClassMetadata(DDC2825ClassWithImplicitlyDefinedSchema::CLASSNAME, 'myschema', 'mytable2');
|
|
|
|
$this->checkClassMetadata(DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName::CLASSNAME, 'myschema', 'order');
|
2015-01-14 20:47:17 +03:00
|
|
|
}
|
2013-12-17 21:56:13 +04:00
|
|
|
|
2015-01-14 20:47:17 +03:00
|
|
|
/**
|
|
|
|
* Test with a table with a schema
|
|
|
|
*/
|
|
|
|
public function testFetchingFromEntityWithExplicitlyDefinedSchemaInMappings()
|
|
|
|
{
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->persist(new DDC2825ClassWithExplicitlyDefinedSchema());
|
2013-12-17 21:56:13 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2015-01-14 20:31:11 +03:00
|
|
|
$this->assertCount(
|
|
|
|
1,
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->getRepository(DDC2825ClassWithExplicitlyDefinedSchema::CLASSNAME)->findAll()
|
2015-01-14 20:31:11 +03:00
|
|
|
);
|
|
|
|
}
|
2013-12-17 21:56:13 +04:00
|
|
|
|
2015-01-14 20:47:17 +03:00
|
|
|
/**
|
|
|
|
* Test with schema defined directly as a table annotation property
|
|
|
|
*/
|
2015-01-14 20:31:11 +03:00
|
|
|
public function testFetchingFromEntityWithImplicitlyDefinedSchemaInMappings()
|
|
|
|
{
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->persist(new DDC2825ClassWithImplicitlyDefinedSchema());
|
2013-12-17 21:56:13 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2015-01-14 20:34:58 +03:00
|
|
|
$this->assertCount(
|
|
|
|
1,
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->getRepository(DDC2825ClassWithImplicitlyDefinedSchema::CLASSNAME)->findAll()
|
2015-01-14 20:34:58 +03:00
|
|
|
);
|
|
|
|
}
|
2013-12-17 21:56:13 +04:00
|
|
|
|
2015-01-14 20:47:17 +03:00
|
|
|
/**
|
|
|
|
* Test with a table named "order" (which is a reserved keyword) to make sure the table name is not
|
|
|
|
* incorrectly escaped when a schema is used and that the platform doesn't support schemas
|
|
|
|
*/
|
2015-01-14 20:34:58 +03:00
|
|
|
public function testFetchingFromEntityWithImplicitlyDefinedSchemaAndQuotedTableNameInMappings()
|
|
|
|
{
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->persist(new DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName());
|
2013-12-17 21:56:13 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2015-01-14 20:47:17 +03:00
|
|
|
$this->assertCount(
|
|
|
|
1,
|
2015-01-14 20:56:37 +03:00
|
|
|
$this->_em->getRepository(DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName::CLASSNAME)->findAll()
|
2015-01-14 20:47:17 +03:00
|
|
|
);
|
2013-12-17 21:56:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that class metadata is correctly stored when a database schema is used and
|
|
|
|
* checks that the table name is correctly converted whether the platform supports database
|
|
|
|
* schemas or not
|
|
|
|
*
|
2015-01-14 20:47:17 +03:00
|
|
|
* @param string $className Class metadata
|
2013-12-17 21:56:13 +04:00
|
|
|
* @param string $expectedSchemaName Expected schema name
|
|
|
|
* @param string $expectedTableName Expected table name
|
|
|
|
*/
|
2015-01-14 20:47:17 +03:00
|
|
|
protected function checkClassMetadata($className, $expectedSchemaName, $expectedTableName)
|
2013-12-17 21:56:13 +04:00
|
|
|
{
|
2015-01-14 20:47:17 +03:00
|
|
|
$classMetadata = $this->_em->getClassMetadata($className);
|
2013-12-17 21:56:13 +04:00
|
|
|
$platform = $this->_em->getConnection()->getDatabasePlatform();
|
2015-01-14 20:50:17 +03:00
|
|
|
$quotedTableName = $this->_em->getConfiguration()->getQuoteStrategy()->getTableName($classMetadata, $platform);
|
2013-12-17 21:56:13 +04:00
|
|
|
|
|
|
|
// Check if table name and schema properties are defined in the class metadata
|
2015-01-14 20:50:17 +03:00
|
|
|
$this->assertEquals($expectedTableName, $classMetadata->table['name']);
|
|
|
|
$this->assertEquals($expectedSchemaName, $classMetadata->table['schema']);
|
2013-12-17 21:56:13 +04:00
|
|
|
|
|
|
|
if ($this->_em->getConnection()->getDatabasePlatform()->supportsSchemas()) {
|
|
|
|
$fullTableName = sprintf('%s.%s', $expectedSchemaName, $expectedTableName);
|
|
|
|
} else {
|
|
|
|
$fullTableName = sprintf('%s__%s', $expectedSchemaName, $expectedTableName);
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:50:17 +03:00
|
|
|
$this->assertEquals($fullTableName, $quotedTableName);
|
2013-12-17 21:56:13 +04:00
|
|
|
|
|
|
|
// Checks sequence name validity
|
2015-01-14 20:50:17 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
$fullTableName . '_' . $classMetadata->getSingleIdentifierColumnName() . '_seq',
|
|
|
|
$classMetadata->getSequenceName($platform)
|
|
|
|
);
|
2013-12-17 21:56:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @Table(name="myschema.mytable")
|
|
|
|
*/
|
2015-01-14 20:56:37 +03:00
|
|
|
class DDC2825ClassWithExplicitlyDefinedSchema
|
2013-12-17 21:56:13 +04:00
|
|
|
{
|
2015-01-14 20:31:11 +03:00
|
|
|
const CLASSNAME = __CLASS__;
|
|
|
|
|
2013-12-17 21:56:13 +04:00
|
|
|
/**
|
|
|
|
* Test with a quoted column name to check that sequence names are
|
|
|
|
* correctly handled
|
|
|
|
*
|
|
|
|
* @Id @GeneratedValue
|
|
|
|
* @Column(name="`number`", type="integer")
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @Table(name="mytable2",schema="myschema")
|
|
|
|
*/
|
2015-01-14 20:56:37 +03:00
|
|
|
class DDC2825ClassWithImplicitlyDefinedSchema
|
2013-12-17 21:56:13 +04:00
|
|
|
{
|
2015-01-14 20:31:11 +03:00
|
|
|
const CLASSNAME = __CLASS__;
|
|
|
|
|
2013-12-17 21:56:13 +04:00
|
|
|
/**
|
|
|
|
* @Id @GeneratedValue
|
|
|
|
* @Column(type="integer")
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
* @Table(name="myschema.order")
|
|
|
|
*/
|
2015-01-14 20:56:37 +03:00
|
|
|
class DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName
|
2013-12-17 21:56:13 +04:00
|
|
|
{
|
2015-01-14 20:31:11 +03:00
|
|
|
const CLASSNAME = __CLASS__;
|
|
|
|
|
2013-12-17 21:56:13 +04:00
|
|
|
/**
|
|
|
|
* @Id @GeneratedValue
|
|
|
|
* @Column(type="integer")
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $id;
|
|
|
|
}
|