Skip to content

Commit

Permalink
Fix doctrine/data-fixtures PR#334 (#340)
Browse files Browse the repository at this point in the history
Fix doctrine/data-fixtures PR#334
  • Loading branch information
greg0ire authored Jan 17, 2020
2 parents 640ab13 + 355199c commit 39e9777
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
28 changes: 12 additions & 16 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function array_reverse;
use function array_search;
use function count;
use function implode;
use function is_callable;
use function method_exists;
use function preg_match;
Expand Down Expand Up @@ -152,7 +151,7 @@ public function purge()
}

if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$connection->executeUpdate('DELETE FROM ' . $tbl);
$connection->executeUpdate($this->getDeleteFromTableSQL($tbl, $platform));
} else {
$connection->executeUpdate($platform->getTruncateTableSQL($tbl, true));
}
Expand Down Expand Up @@ -240,23 +239,13 @@ private function getAssociationTables(array $classes, AbstractPlatform $platform
return $associationTables;
}

/**
* @param ClassMetadata $class
* @param AbstractPlatform $platform
*
* @return string
*/
private function getTableName($class, $platform)
private function getTableName(ClassMetadata $class, AbstractPlatform $platform) : string
{
if (method_exists($class, 'getSchemaName')) {
$identifier[] = $class->getSchemaName();
} elseif (isset($class->table['schema'])) {
$identifier[] = $class->table['schema'];
if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) {
return $class->table['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
}

$identifier[] = $class->getTableName();

return (new Identifier(implode('.', $identifier)))->getQuotedName($platform);
return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
}

/**
Expand All @@ -274,4 +263,11 @@ private function getJoinTableName($assoc, $class, $platform)

return $this->em->getConfiguration()->getQuoteStrategy()->getJoinTableName($assoc, $class, $platform);
}

private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $platform) : string
{
$tableIdentifier = new Identifier($tableName);

return 'DELETE FROM ' . $tableIdentifier->getQuotedName($platform);
}
}
33 changes: 26 additions & 7 deletions tests/Doctrine/Tests/Common/DataFixtures/Purger/ORMPurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
*/
class ORMPurgerTest extends BaseTest
{
public const TEST_ENTITY_USER = TestEntity\User::class;
public const TEST_ENTITY_USER_WITH_SCHEMA = TestEntity\UserWithSchema::class;
public const TEST_ENTITY_QUOTED = TestEntity\Quoted::class;
public const TEST_ENTITY_GROUP = TestEntity\Group::class;
public const TEST_ENTITY_USER = TestEntity\User::class;
public const TEST_ENTITY_USER_WITH_SCHEMA = TestEntity\UserWithSchema::class;
public const TEST_ENTITY_QUOTED = TestEntity\Quoted::class;
public const TEST_ENTITY_GROUP = TestEntity\Group::class;
public const TEST_ENTITY_GROUP_WITH_SCHEMA = TestEntity\GroupWithSchema::class;

public function testGetAssociationTables()
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public function testTableNameWithSchema()
$this->assertStringStartsWith('test_schema', $tableName);
}

public function testGetTableNameQuoted() : void
public function testGetDeleteFromTableSQL() : void
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_GROUP);
Expand All @@ -66,7 +67,25 @@ public function testGetTableNameQuoted() : void
$method = $class->getMethod('getTableName');
$method->setAccessible(true);
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('"', $tableName);
$this->assertStringEndsWith('"', $tableName);
$method = $class->getMethod('getDeleteFromTableSQL');
$method->setAccessible(true);
$sql = $method->invokeArgs($purger, [$tableName, $platform]);
$this->assertEquals('DELETE FROM "Group"', $sql);
}

public function testGetDeleteFromTableSQLWithSchema() : void
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_GROUP_WITH_SCHEMA);
$platform = $em->getConnection()->getDatabasePlatform();
$purger = new ORMPurger($em);
$class = new ReflectionClass(ORMPurger::class);
$method = $class->getMethod('getTableName');
$method->setAccessible(true);
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$method = $class->getMethod('getDeleteFromTableSQL');
$method->setAccessible(true);
$sql = $method->invokeArgs($purger, [$tableName, $platform]);
$this->assertEquals('DELETE FROM test_schema__group', $sql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Table(name="group",schema="test_schema")
*/
class GroupWithSchema
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
*
* @var int|null
*/
private $id;

/**
* @ORM\Column(length=32)
* @ORM\Id
*
* @var string|null
*/
private $code;

public function setId($id) : void
{
$this->id = $id;
}

public function getId() : ?int
{
return $this->id;
}

public function setCode($code) : void
{
$this->code = $code;
}

public function getCode() : ?string
{
return $this->code;
}
}

0 comments on commit 39e9777

Please sign in to comment.