From f335f231455dd696a7d7fa5c4f31ed143d5b88aa Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 4 Jun 2012 16:56:46 -0300 Subject: [PATCH] tests for quoted columns metadata --- .../ORM/Mapping/ClassMetadataInfo.php | 34 +++++++++ .../ORM/Mapping/DefaultQuoteStrategy.php | 20 +---- lib/Doctrine/ORM/Mapping/QuoteStrategy.php | 18 ----- tests/Doctrine/Tests/Models/Quote/Address.php | 30 ++++++++ tests/Doctrine/Tests/Models/Quote/Group.php | 29 ++++++++ tests/Doctrine/Tests/Models/Quote/Phone.php | 24 ++++++ .../SimpleEntity.php} | 10 +-- tests/Doctrine/Tests/Models/Quote/User.php | 61 +++++++++++++++ .../ORM/Functional/Ticket/DDC1719Test.php | 8 +- .../ORM/Mapping/ClassMetadataFactoryTest.php | 74 +++++++++++++++++++ .../Tests/ORM/Mapping/QuoteStrategyTest.php | 16 ---- .../BasicEntityPersisterTypeValueSqlTest.php | 4 +- .../ORM/Query/SelectSqlGenerationTest.php | 12 +-- 13 files changed, 270 insertions(+), 70 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/Quote/Address.php create mode 100644 tests/Doctrine/Tests/Models/Quote/Group.php create mode 100644 tests/Doctrine/Tests/Models/Quote/Phone.php rename tests/Doctrine/Tests/Models/{DDC1719/DDC1719Entity.php => Quote/SimpleEntity.php} (56%) create mode 100644 tests/Doctrine/Tests/Models/Quote/User.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 24131d17e..69d8c20cc 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1373,17 +1373,25 @@ class ClassMetadataInfo implements ClassMetadata $uniqueContraintColumns[] = $joinColumn['name']; } } + if (empty($joinColumn['name'])) { $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']); } + if (empty($joinColumn['referencedColumnName'])) { $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); } + if ($joinColumn['name'][0] == '`') { $joinColumn['name'] = trim($joinColumn['name'], '`'); $joinColumn['quoted'] = true; } + if ($joinColumn['referencedColumnName'][0] == '`') { + $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`'); + $joinColumn['quoted'] = true; + } + $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName']; $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName']) ? $joinColumn['fieldName'] : $joinColumn['name']; @@ -1464,12 +1472,25 @@ class ClassMetadataInfo implements ClassMetadata if (empty($joinColumn['name'])) { $joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']); } + if (empty($joinColumn['referencedColumnName'])) { $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); } + + if ($joinColumn['name'][0] == '`') { + $joinColumn['name'] = trim($joinColumn['name'], '`'); + $joinColumn['quoted'] = true; + } + + if ($joinColumn['referencedColumnName'][0] == '`') { + $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`'); + $joinColumn['quoted'] = true; + } + if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') { $mapping['isOnDeleteCascade'] = true; } + $mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName']; $mapping['joinTableColumns'][] = $joinColumn['name']; } @@ -1478,12 +1499,25 @@ class ClassMetadataInfo implements ClassMetadata if (empty($inverseJoinColumn['name'])) { $inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']); } + if (empty($inverseJoinColumn['referencedColumnName'])) { $inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); } + + if ($inverseJoinColumn['name'][0] == '`') { + $inverseJoinColumn['name'] = trim($inverseJoinColumn['name'], '`'); + $inverseJoinColumn['quoted'] = true; + } + + if ($inverseJoinColumn['referencedColumnName'][0] == '`') { + $inverseJoinColumn['referencedColumnName'] = trim($inverseJoinColumn['referencedColumnName'], '`'); + $inverseJoinColumn['quoted'] = true; + } + if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') { $mapping['isOnDeleteCascade'] = true; } + $mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName']; $mapping['joinTableColumns'][] = $inverseJoinColumn['name']; } diff --git a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php index 763e8784d..3e09f1929 100644 --- a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php @@ -23,30 +23,12 @@ namespace Doctrine\ORM\Mapping; /** * A set of rules for determining the physical column, alias and table quotes * - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.doctrine-project.org - * @since 2.4 + * @since 2.3 * @author Fabio B. Silva */ class DefaultQuoteStrategy extends QuoteStrategy { - /** - * {@inheritdoc} - */ - public function isQuotedIdentifier($identifier) - { - return strlen($identifier) > 0 && $identifier[0] === '`'; - } - - /** - * {@inheritdoc} - */ - public function getUnquotedIdentifier($identifier) - { - return trim($identifier, '`'); - } - /** * {@inheritdoc} */ diff --git a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php index a7b7798dd..5882783ad 100644 --- a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php @@ -25,8 +25,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; /** * A set of rules for determining the physical column, alias and table quotes * - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.doctrine-project.org * @since 2.3 * @author Fabio B. Silva */ @@ -45,22 +43,6 @@ abstract class QuoteStrategy $this->platform = $platform; } - /** - * Checks if the given identifier is quoted - * - * @param string $identifier - * @return string - */ - abstract public function isQuotedIdentifier($identifier); - - /** - * Gets the uquoted column name. - * - * @param string $identifier - * @return string - */ - abstract public function getUnquotedIdentifier($identifier); - /** * Gets the (possibly quoted) column name for safe use in an SQL statement. * diff --git a/tests/Doctrine/Tests/Models/Quote/Address.php b/tests/Doctrine/Tests/Models/Quote/Address.php new file mode 100644 index 000000000..3597687d5 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Quote/Address.php @@ -0,0 +1,30 @@ +phones = new ArrayCollection; + $this->groups = new ArrayCollection; + } + +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1719Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1719Test.php index 1d5a2065b..93b3458e6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1719Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1719Test.php @@ -2,7 +2,7 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; -use Doctrine\Tests\Models\DDC1719\DDC1719Entity; +use Doctrine\Tests\Models\Quote\SimpleEntity; require_once __DIR__ . '/../../../TestInit.php'; @@ -12,7 +12,7 @@ require_once __DIR__ . '/../../../TestInit.php'; class DDC1719Test extends \Doctrine\Tests\OrmFunctionalTestCase { - const CLASS_NAME = '\Doctrine\Tests\Models\DDC1719\DDC1719Entity'; + const CLASS_NAME = '\Doctrine\Tests\Models\Quote\SimpleEntity'; protected function setUp() { @@ -27,8 +27,8 @@ class DDC1719Test extends \Doctrine\Tests\OrmFunctionalTestCase public function testCreateRetreaveUpdateDelete() { - $e1 = new DDC1719Entity('Bar 1'); - $e2 = new DDC1719Entity('Foo 1'); + $e1 = new SimpleEntity('Bar 1'); + $e2 = new SimpleEntity('Foo 1'); // Create $this->_em->persist($e1); diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 2a2f46ce3..f72659336 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -243,6 +243,80 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); return $cm1; } + + /** + * @group DDC-1845 + */ + public function testQuoteMetadata() + { + $cmf = new ClassMetadataFactory(); + $driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/Quote/')); + $em = $this->_createEntityManager($driver); + $cmf->setEntityManager($em); + + + $userMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\User'); + $phoneMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Phone'); + $groupMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Group'); + $addressMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Address'); + + + // Phone Class Metadata + $this->assertTrue($phoneMetadata->fieldMappings['number']['quoted']); + $this->assertEquals('phone-number', $phoneMetadata->fieldMappings['number']['columnName']); + + $user = $phoneMetadata->associationMappings['user']; + $this->assertTrue($user['joinColumns'][0]['quoted']); + $this->assertEquals('user-id', $user['joinColumns'][0]['name']); + $this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']); + + + + // User Class Metadata + $this->assertTrue($groupMetadata->fieldMappings['id']['quoted']); + $this->assertTrue($groupMetadata->fieldMappings['name']['quoted']); + + $this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']); + $this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']); + + + + // Address Class Metadata + $this->assertTrue($addressMetadata->fieldMappings['id']['quoted']); + $this->assertTrue($addressMetadata->fieldMappings['zip']['quoted']); + + $this->assertEquals('address-id', $addressMetadata->fieldMappings['id']['columnName']); + $this->assertEquals('address-zip', $addressMetadata->fieldMappings['zip']['columnName']); + + $user = $addressMetadata->associationMappings['user']; + $this->assertTrue($user['joinColumns'][0]['quoted']); + $this->assertEquals('user-id', $user['joinColumns'][0]['name']); + $this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']); + + + + // User Class Metadata + $this->assertTrue($userMetadata->fieldMappings['id']['quoted']); + $this->assertTrue($userMetadata->fieldMappings['name']['quoted']); + + $this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']); + $this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']); + + + $address = $userMetadata->associationMappings['address']; + $this->assertTrue($address['joinColumns'][0]['quoted']); + $this->assertEquals('address-id', $address['joinColumns'][0]['name']); + $this->assertEquals('address-id', $address['joinColumns'][0]['referencedColumnName']); + + $groups = $userMetadata->associationMappings['groups']; + $this->assertTrue($groups['joinTable']['joinColumns'][0]['quoted']); + $this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['name']); + $this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['referencedColumnName']); + + $this->assertTrue($groups['joinTable']['inverseJoinColumns'][0]['quoted']); + $this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['name']); + $this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['referencedColumnName']); + } } /* Test subject class with overriden factory method for mocking purposes */ diff --git a/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php b/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php index 50b87c159..4e832060b 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php @@ -26,7 +26,6 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase $this->strategy = new DefaultQuoteStrategy($em->getConnection()->getDatabasePlatform()); } - /** * @param string $className * @return \Doctrine\ORM\Mapping\ClassMetadata @@ -39,21 +38,6 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase return $cm; } - - public function testIsQuotedIdentifier() - { - $this->assertTrue($this->strategy->isQuotedIdentifier('`table_name`')); - $this->assertFalse($this->strategy->isQuotedIdentifier('table_name')); - $this->assertFalse($this->strategy->isQuotedIdentifier(null)); - $this->assertFalse($this->strategy->isQuotedIdentifier('')); - } - - public function testGetUnquotedIdentifier() - { - $this->assertEquals('table_name' ,$this->strategy->getUnquotedIdentifier('`table_name`')); - $this->assertEquals('table_name' ,$this->strategy->getUnquotedIdentifier('table_name')); - } - public function testGetColumnName() { $cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index 344ca12f9..fa1d7530a 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -82,10 +82,10 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase */ public function testStripNonAlphanumericCharactersFromSelectColumnListSQL() { - $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\DDC1719\DDC1719Entity')); + $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\SimpleEntity')); $method = new \ReflectionMethod($persister, '_getSelectColumnListSQL'); $method->setAccessible(true); - $this->assertEquals('t0."entity-id" AS entityid1, t0."entity-value" AS entityvalue2', $method->invoke($persister)); + $this->assertEquals('t0."simple-entity-id" AS simpleentityid1, t0."simple-entity-value" AS simpleentityvalue2', $method->invoke($persister)); } } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index ae7cd7a52..3a6e349dd 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1647,18 +1647,18 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase public function testStripNonAlphanumericCharactersFromAlias() { $this->assertSqlGeneration( - 'SELECT e FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e', - 'SELECT d0_."entity-id" AS entityid0, d0_."entity-value" AS entityvalue1 FROM "ddc-1719-entity" d0_' + 'SELECT e FROM Doctrine\Tests\Models\Quote\SimpleEntity e', + 'SELECT d0_."simple-entity-id" AS simpleentityid0, d0_."simple-entity-value" AS simpleentityvalue1 FROM "ddc-1719-simple-entity" d0_' ); $this->assertSqlGeneration( - 'SELECT e.value FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', - 'SELECT d0_."entity-value" AS entityvalue0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' + 'SELECT e.value FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value', + 'SELECT d0_."simple-entity-value" AS simpleentityvalue0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC' ); $this->assertSqlGeneration( - 'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', - 'SELECT TRIM(d0_."entity-value") AS sclr0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' + 'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value', + 'SELECT TRIM(d0_."simple-entity-value") AS sclr0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC' ); }