Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge release 1.4.1 into master #336

Merged
merged 9 commits into from
Jan 13, 2020
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ by implementing the [`Doctrine\Common\DataFixtures\FixtureInterface`](lib/Doctri
```php
namespace MyDataFixtures;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;

class UserFixtureLoader implements FixtureInterface
{
Expand Down Expand Up @@ -82,7 +82,7 @@ a relation. Here is an example fixtures for **Role** and **User** relation
namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class UserRoleDataLoader extends AbstractFixture
{
Expand Down Expand Up @@ -110,7 +110,7 @@ And the **User** data loading fixture:
namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class UserDataLoader extends AbstractFixture
{
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class MyFixture extends AbstractFixture implements OrderedFixtureInterface
{
Expand All @@ -168,7 +168,7 @@ namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"require": {
"php": "^7.2",
"doctrine/common": "^2.11"
"doctrine/common": "^2.11",
"doctrine/persistence": "^1.3.3"
},
"conflict": {
"doctrine/phpcr-odm": "<1.3.0"
Expand All @@ -25,7 +26,7 @@
"doctrine/coding-standard": "^6.0",
"doctrine/dbal": "^2.5.4",
"doctrine/mongodb-odm": "^1.3.0",
"doctrine/orm": "^2.5.4",
"doctrine/orm": "^2.7.0",
"phpunit/phpunit": "^7.0"
},
"suggest": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DependentFixtureInterface
* This method must return an array of fixtures classes
* on which the implementing class depends on
*
* @return array
* @return class-string[]
*/
public function getDependencies();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\Common\DataFixtures\SharedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use Exception;
use function get_class;
use function interface_exists;
use function sprintf;

/**
Expand Down Expand Up @@ -139,3 +140,5 @@ public function purge()
*/
abstract public function execute(array $fixtures, $append = false);
}

interface_exists(ObjectManager::class);
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/DataFixtures/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Doctrine\Common\DataFixtures;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use function interface_exists;

/**
* Interface contract for fixture classes to implement.
Expand All @@ -16,3 +17,5 @@ interface FixtureInterface
*/
public function load(ObjectManager $manager);
}

interface_exists(ObjectManager::class);
8 changes: 4 additions & 4 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use function file_get_contents;
use function file_put_contents;
use function get_class;
use function json_decode;
use function json_encode;
use function serialize;
use function substr;
use function unserialize;

/**
* Proxy reference repository
Expand Down Expand Up @@ -58,7 +58,7 @@ public function serialize()
$simpleReferences[$name] = [$className, $this->getIdentifier($reference, $unitOfWork)];
}

return json_encode([
return serialize([
'references' => $simpleReferences,
'identities' => $this->getIdentities(),
]);
Expand All @@ -71,7 +71,7 @@ public function serialize()
*/
public function unserialize($serializedData)
{
$repositoryData = json_decode($serializedData, true);
$repositoryData = unserialize($serializedData);
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
Expand Down
12 changes: 9 additions & 3 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Doctrine\Common\DataFixtures\Sorter\TopologicalSorter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
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 @@ -246,11 +248,15 @@ private function getAssociationTables(array $classes, AbstractPlatform $platform
*/
private function getTableName($class, $platform)
{
if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) {
return $class->table['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
if (method_exists($class, 'getSchemaName')) {
$identifier[] = $class->getSchemaName();
} elseif (isset($class->table['schema'])) {
$identifier[] = $class->table['schema'];
}

return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
$identifier[] = $class->getTableName();

return (new Identifier(implode('.', $identifier)))->getQuotedName($platform);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Doctrine\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;
use Doctrine\Persistence\ObjectManager;
use OutOfBoundsException;
use function array_key_exists;
use function array_keys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use InvalidArgumentException;
use RuntimeException;
use function array_search;
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Common/DataFixtures/FixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Doctrine\Tests\Common\DataFixtures;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

/**
* Test Fixture interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;

/**
* Test Fixture ordering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@

use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Proxy\Proxy;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Link;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Role;
use Doctrine\Tests\Common\DataFixtures\TestTypes\UuidType;
use Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid;

/**
* Test ProxyReferenceRepository.
*/
class ProxyReferenceRepositoryTest extends BaseTest
{
public const TEST_ENTITY_ROLE = Role::class;
public const TEST_ENTITY_LINK = Link::class;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

if (Type::hasType('uuid')) {
return;
}

Type::addType('uuid', UuidType::class);
}

public function testReferenceEntry()
{
Expand Down Expand Up @@ -113,6 +129,35 @@ public function testReferenceReconstruction()
$this->assertInstanceOf(Proxy::class, $ref);
}

public function testReconstructionOfCustomTypedId() : void
{
$em = $this->getMockSqliteEntityManager();
$referenceRepository = new ProxyReferenceRepository($em);
$listener = new ORMReferenceListener($referenceRepository);
$em->getEventManager()->addEventSubscriber($listener);

$schemaTool = new SchemaTool($em);
$schemaTool->dropSchema([]);
$schemaTool->createSchema([$em->getClassMetadata(self::TEST_ENTITY_LINK)]);

$link = new TestEntity\Link(new Uuid('5e48c0d7-78c2-44f5-bed0-e7970b2822b8'));
$link->setUrl('http://example.com');

$referenceRepository->addReference('home-link', $link);
$em->persist($link);
$em->flush();
$em->clear();

$serializedData = $referenceRepository->serialize();
$proxyReferenceRepository = new ProxyReferenceRepository($em);
$proxyReferenceRepository->unserialize($serializedData);

$this->assertInstanceOf(
'Doctrine\Tests\Common\DataFixtures\TestValueObjects\Uuid',
$proxyReferenceRepository->getReference('home-link')->getId()
);
}

public function testReferenceMultipleEntries()
{
$em = $this->getMockSqliteEntityManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 function testGetAssociationTables()
{
Expand All @@ -26,7 +27,7 @@ public function testGetAssociationTables()
$method = $class->getMethod('getAssociationTables');
$method->setAccessible(true);
$associationTables = $method->invokeArgs($purger, [[$metadata], $platform]);
$this->assertEquals($associationTables[0], 'readers.author_reader');
$this->assertEquals($associationTables[0], 'readers__author_reader');
}

public function testGetAssociationTablesQuoted()
Expand Down Expand Up @@ -54,4 +55,18 @@ public function testTableNameWithSchema()
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('test_schema', $tableName);
}

public function testGetTableNameQuoted() : void
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_GROUP);
$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]);
$this->assertStringStartsWith('"', $tableName);
$this->assertStringEndsWith('"', $tableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use BadMethodCallException;
use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
use Doctrine\Common\DataFixtures\ReferenceRepository;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Proxy;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Tests\Common\DataFixtures\TestEntity\Role;
use OutOfBoundsException;
use Prophecy\Prophecy\ProphecyInterface;
Expand Down
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class Group
{
/**
* @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;
}
}
Loading