Skip to content

Commit

Permalink
Merge pull request #985 from goetas/merge-2.3.x-into-master
Browse files Browse the repository at this point in the history
Merge 2.3.x into master
  • Loading branch information
goetas committed Jun 13, 2020
2 parents 4febe79 + b802bbe commit da238fb
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php
Expand Up @@ -7,8 +7,10 @@
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Provider\Exception\NoMappingFound;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use function count;
use function usort;

/**
* The OrmSchemaProvider class is responsible for creating a Doctrine\DBAL\Schema\Schema instance from the mapping
Expand Down Expand Up @@ -38,6 +40,10 @@ public function createSchema() : Schema
throw NoMappingFound::new();
}

usort($metadata, static function (ClassMetadata $a, ClassMetadata $b) : int {
return $a->getTableName() <=> $b->getTableName();
});

$tool = new SchemaTool($this->entityManager);

return $tool->getSchemaFromMetadata($metadata);
Expand Down
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Tests\Provider;

class SampleEntity
class A
{
/** @var int|null */
private $id;
Expand Down
16 changes: 16 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/B.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

class B
{
/** @var int|null */
private $id;

public function getId() : ?int
{
return $this->id;
}
}
16 changes: 16 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/C.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

class C
{
/** @var int|null */
private $id;

public function getId() : ?int
{
return $this->id;
}
}
20 changes: 20 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/ClassMetadataFactory.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseMetadataFactoryAlias;
use function array_reverse;

class ClassMetadataFactory extends BaseMetadataFactoryAlias
{
/**
* @return ClassMetadata[]
*/
public function getAllMetadata() : array
{
return array_reverse(parent::getAllMetadata());
}
}
21 changes: 17 additions & 4 deletions tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php
Expand Up @@ -34,9 +34,20 @@ class OrmSchemaProviderTest extends MigrationTestCase
public function testCreateSchemaFetchesMetadataFromEntityManager() : void
{
$schema = $this->ormProvider->createSchema();
self::assertTrue($schema->hasTable('sample_entity'));
$table = $schema->getTable('sample_entity');
self::assertTrue($table->hasColumn('id'));

self::assertSame(
[
'public.a',
'public.b',
'public.c',
],
$schema->getTableNames()
);

foreach (['a', 'b', 'c'] as $expectedTable) {
$table = $schema->getTable($expectedTable);
self::assertTrue($table->hasColumn('id'));
}
}

public function testEntityManagerWithoutMetadataCausesError() : void
Expand All @@ -50,8 +61,10 @@ public function testEntityManagerWithoutMetadataCausesError() : void

protected function setUp() : void
{
$this->config = Setup::createXMLMetadataConfiguration([__DIR__ . '/_files'], true);
$this->config->setClassMetadataFactoryName(ClassMetadataFactory::class);

$this->conn = $this->getSqliteConnection();
$this->config = Setup::createXMLMetadataConfiguration([__DIR__ . '/_files'], true);
$this->entityManager = EntityManager::create($this->conn, $this->config);
$this->ormProvider = new OrmSchemaProvider($this->entityManager);
}
Expand Down
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\SampleEntity" table="sample_entity">
<entity name="Doctrine\Migrations\Tests\Provider\A" table="a">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
Expand Down
@@ -0,0 +1,12 @@
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\B" table="b">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
</entity>

</doctrine-mapping>
@@ -0,0 +1,12 @@
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\C" table="c">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
</entity>

</doctrine-mapping>

0 comments on commit da238fb

Please sign in to comment.