From 2b4c29e4f29ef914a929ac35598bc8adb70f94df Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 30 May 2012 15:28:03 -0300 Subject: [PATCH] tests for default strategy --- .../ORM/Mapping/DefaultQuoteStrategy.php | 25 +++++-- .../Tests/ORM/Mapping/QuoteStrategyTest.php | 70 ++++++++++++++++--- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php index 8a8d2bbe9..a2a49f1b4 100644 --- a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php @@ -32,17 +32,31 @@ namespace Doctrine\ORM\Mapping; class DefaultQuoteStrategy extends QuoteStrategy { + /** + * Checks if the given identifier is quoted + * + * @param string $identifier + * @return string + */ public function isQuotedIdentifier($identifier) { return strlen($identifier) > 0 && $identifier[0] === '`'; } + /** + * Gets the uquoted column name. + * + * @param string $identifier + * @return string + */ public function getUnquotedIdentifier($identifier) { return trim($identifier, '`'); } /** + * Gets the (possibly quoted) column name for safe use in an SQL statement. + * * @param string $fieldName * @param ClassMetadata $class * @return string @@ -55,8 +69,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the (possibly quoted) primary table name of this class for safe use - * in an SQL statement. + * Gets the (possibly quoted) primary table name for safe use in an SQL statement. * * @param ClassMetadata $class * @return string @@ -93,7 +106,7 @@ class DefaultQuoteStrategy extends QuoteStrategy $quotedColumnNames = array(); foreach ($class->identifier as $fieldName) { - if (isset($this->fieldMappings[$fieldName])) { + if (isset($class->fieldMappings[$fieldName])) { $quotedColumnNames[] = $this->getColumnName($fieldName, $class); continue; @@ -118,8 +131,10 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * @param string $columnName - * @param string $counter + * Gets the column alias. + * + * @param string $columnName + * @param integer $counter * @param ClassMetadata $class * @return string */ diff --git a/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php b/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php index 38bc4de21..e541f890d 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/QuoteStrategyTest.php @@ -9,7 +9,7 @@ use Doctrine\ORM\Mapping\ClassMetadata; require_once __DIR__ . '/../../TestInit.php'; /** - * @group DDC-1719 + * @group DDC-1845 */ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase { @@ -78,20 +78,19 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase public function testJoinTableName() { - $cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); - $cm->mapManyToMany(array( + $cm1 = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + $cm2 = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $cm1->mapManyToMany(array( 'fieldName' => 'user', 'targetEntity' => 'CmsUser', 'inversedBy' => 'users', 'joinTable' => array( - 'name' => '`cmsaddress_cmsuser`' - ) + 'name' => '`cmsaddress_cmsuser`' ) - ); - $this->assertEquals('"cmsaddress_cmsuser"', $this->strategy->getJoinTableName('user', $cm)); + )); - $cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); - $cm->mapManyToMany(array( + $cm2->mapManyToMany(array( 'fieldName' => 'user', 'targetEntity' => 'CmsUser', 'inversedBy' => 'users', @@ -100,7 +99,58 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase ) ) ); - $this->assertEquals('cmsaddress_cmsuser', $this->strategy->getJoinTableName('user', $cm)); + $this->assertEquals('"cmsaddress_cmsuser"', $this->strategy->getJoinTableName('user', $cm1)); + $this->assertEquals('cmsaddress_cmsuser', $this->strategy->getJoinTableName('user', $cm2)); + + } + + public function testIdentifierColumnNames() + { + $cm1 = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + $cm2 = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $cm1->mapField(array( + 'id' => true, + 'fieldName' => 'id', + 'columnName' => '`id`', + )); + + $cm2->mapField(array( + 'id' => true, + 'fieldName' => 'id', + 'columnName' => 'id', + )); + + $this->assertEquals(array('"id"'), $this->strategy->getIdentifierColumnNames($cm1)); + $this->assertEquals(array('id'), $this->strategy->getIdentifierColumnNames($cm2)); + } + + + public function testColumnAlias() + { + $i = 0; + $this->assertEquals('columnName0', $this->strategy->getColumnAlias('columnName', $i++)); + $this->assertEquals('column_name1', $this->strategy->getColumnAlias('column_name', $i++)); + $this->assertEquals('COLUMN_NAME2', $this->strategy->getColumnAlias('COLUMN_NAME', $i++)); + $this->assertEquals('COLUMNNAME3', $this->strategy->getColumnAlias('COLUMN-NAME-', $i++)); + } + + public function testQuoteJoinColumnNames() + { + $this->markTestIncomplete(); + + $cm = $this->createClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails'); + + $cm->mapOneToOne(array( + 'id' => true, + 'fieldName' => 'article', + 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article', + 'joinColumns' => array(array( + 'name' => '`article`' + )), + )); + + $this->assertEquals(array('"article"'), $this->strategy->getIdentifierColumnNames($cm)); } } \ No newline at end of file