1
0
mirror of synced 2025-01-18 22:41:43 +03:00

first tests for DefaultQuoteStrategy

This commit is contained in:
Fabio B. Silva 2012-05-10 21:18:09 -03:00
parent 41d9f612c7
commit 9f297c3140
4 changed files with 323 additions and 0 deletions

View File

@ -629,4 +629,34 @@ class Configuration extends \Doctrine\DBAL\Configuration
return $this->_attributes['namingStrategy'];
}
/**
* Set quote strategy class.
*
* @since 2.4
* @param string $namingStrategy
*/
public function setQuoteStrategyClassName($className)
{
$quoteStrategy = 'Doctrine\Mapping\AbstractQuoteStrategy';
if ($className !== $quoteStrategy && ! is_subclass_of($className, $quoteStrategy)) {
throw new \InvalidArgumentException("Invalid quote strategy class");
}
$this->_attributes['quoteStrategyClassName'] = $namingStrategy;
}
/**
* Get quote strategy class.
*
* @since 2.4
* @return string
*/
public function getQuoteStrategyClassName()
{
return isset($this->_attributes['quoteStrategyClassName'])
? $this->_attributes['quoteStrategyClassName']
: 'Doctrine\Mapping\DefaultQuoteStrategy';
}
}

View File

@ -0,0 +1,138 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
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
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
class DefaultQuoteStrategy extends QuoteStrategy
{
public function isQuotedIdentifier($identifier)
{
return strlen($identifier) > 0 && $identifier[0] === '`';
}
public function getUnquotedIdentifier($identifier)
{
return trim($identifier, '`');
}
/**
* @param string $fieldName
* @param ClassMetadata $class
* @return string
*/
public function getColumnName($fieldName, ClassMetadata $class)
{
return isset($class->fieldMappings[$fieldName]['quoted'])
? $this->platform->quoteIdentifier($class->fieldMappings[$fieldName]['columnName'])
: $class->fieldMappings[$fieldName]['columnName'];
}
/**
* Gets the (possibly quoted) primary table name of this class for safe use
* in an SQL statement.
*
* @param ClassMetadata $class
* @return string
*/
public function getTableName(ClassMetadata $class)
{
return isset($class->table['quoted'])
? $this->platform->quoteIdentifier($class->table['name'])
: $class->table['name'];
}
/**
* Gets the (possibly quoted) name of the join table.
*
* @param ClassMetadata $class
* @return string
*/
public function getJoinTableName($relation, ClassMetadata $class)
{
$assoc = $class->associationMappings[$relation];
return isset($assoc['joinTable']['quoted'])
? $this->platform->quoteIdentifier($assoc['joinTable']['name'])
: $assoc['joinTable']['name'];
}
/**
* Gets the (possibly quoted) identifier column names for safe use in an SQL statement.
*
* @param ClassMetadata $class
* @return array
*/
public function getIdentifierColumnNames(ClassMetadata $class)
{
$quotedColumnNames = array();
foreach ($class->identifier as $fieldName) {
if (isset($this->fieldMappings[$fieldName])) {
$quotedColumnNames[] = $this->getColumnName($fieldName, $class);
continue;
}
// Association defined as Id field
$platform = $this->platform;
$joinColumns = $class->associationMappings[$fieldName]['joinColumns'];
$assocQuotedColumnNames = array_map(
function ($joinColumn) use ($platform) {
return isset($joinColumn['quoted'])
? $platform->quoteIdentifier($joinColumn['name'])
: $joinColumn['name'];
},
$joinColumns
);
$quotedColumnNames = array_merge($quotedColumnNames, $assocQuotedColumnNames);
}
return $quotedColumnNames;
}
/**
* @param string $columnName
* @param string $counter
* @param ClassMetadata $class
* @return string
*/
public function getColumnAlias($columnName, $counter, ClassMetadata $class = null)
{
// Trim the column alias to the maximum identifier length of the platform.
// If the alias is to long, characters are cut off from the beginning.
// And strip non alphanumeric characters
$columnName = $columnName . $counter;
$columnName = substr($columnName, -$this->platform->getMaxIdentifierLength());
$columnName = preg_replace('/[^A-Za-z0-9_]/', '', $columnName);
return $this->platform->getSQLResultCasing($columnName);
}
}

View File

@ -0,0 +1,49 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
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.4
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
abstract class QuoteStrategy
{
/**
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
*/
protected $platform;
/**
* @param AbstractPlatform $platform
*/
public function __construct(AbstractPlatform $platform)
{
$this->platform = $platform;
}
}

View File

@ -0,0 +1,106 @@
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Mapping\ClassMetadata;
require_once __DIR__ . '/../../TestInit.php';
/**
* @group DDC-1719
*/
class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase
{
/**
* @var \Doctrine\ORM\Mapping\DefaultQuoteStrategy
*/
private $strategy;
protected function setUp()
{
parent::setUp();
$em = $this->_getTestEntityManager();
$this->strategy = new DefaultQuoteStrategy($em->getConnection()->getDatabasePlatform());
}
/**
* @param string $className
* @return \Doctrine\ORM\Mapping\ClassMetadata
*/
private function createClassMetadata($className)
{
$cm = new ClassMetadata($className);
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
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');
$cm->mapField(array('fieldName' => 'name', 'columnName' => '`name`'));
$cm->mapField(array('fieldName' => 'id', 'columnName' => 'id'));
$this->assertEquals('id' ,$this->strategy->getColumnName('id', $cm));
$this->assertEquals('"name"' ,$this->strategy->getColumnName('name', $cm));
}
public function testGetTableName()
{
$cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$cm->setPrimaryTable(array('name'=>'`cms_user`'));
$this->assertEquals('"cms_user"' ,$this->strategy->getTableName($cm));
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
$cm->setPrimaryTable(array('name'=>'cms_user'));
$this->assertEquals('cms_user' ,$this->strategy->getTableName($cm));
}
public function testJoinTableName()
{
$cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
$cm->mapManyToMany(array(
'fieldName' => 'user',
'targetEntity' => 'CmsUser',
'inversedBy' => 'users',
'joinTable' => array(
'name' => '`cmsaddress_cmsuser`'
)
)
);
$this->assertEquals('"cmsaddress_cmsuser"', $this->strategy->getJoinTableName('user', $cm));
$cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress');
$cm->mapManyToMany(array(
'fieldName' => 'user',
'targetEntity' => 'CmsUser',
'inversedBy' => 'users',
'joinTable' => array(
'name' => 'cmsaddress_cmsuser'
)
)
);
$this->assertEquals('cmsaddress_cmsuser', $this->strategy->getJoinTableName('user', $cm));
}
}