Skip to content

Commit

Permalink
Migrate more usages of SchemaTool::createSchema()
Browse files Browse the repository at this point in the history
When I introduced OrmFunctionalTestCase::createSchemaForModels(), I made
several wrong assumptions:
- the call is always wrapped in a try / catch;
- that try / catch is always typed with "Exception".

Because of that, I missed, many, many occurrences of
SchemaTool::createSchema().

I recently noticed that contributors kept using the
SchemaTool::createSchema() and figured not everything had been
migrated.

Migrating some of them did not result in something far better until I
also introduced similar methods for
SchemaTool::getUpdateSchemaSql() and SchemaTool::getSchemaFromMetadata().
  • Loading branch information
greg0ire committed Jul 1, 2022
1 parent 0ef08c5 commit 8a94d0c
Show file tree
Hide file tree
Showing 121 changed files with 411 additions and 825 deletions.
11 changes: 1 addition & 10 deletions tests/Doctrine/Tests/ORM/Functional/InsertableUpdatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Tests\Models\Upsertable\Insertable;
use Doctrine\Tests\Models\Upsertable\Updatable;
use Doctrine\Tests\OrmFunctionalTestCase;
Expand All @@ -15,15 +14,7 @@ protected function setUp(): void
{
parent::setUp();

try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(Updatable::class),
$this->_em->getClassMetadata(Insertable::class),
]
);
} catch (ToolsException $e) {
}
$this->createSchemaForModels(Updatable::class, Insertable::class);
}

public function testNotInsertableIsFetchedFromDatabase(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ protected function setUp(): void
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(Country::class),
$this->_em->getClassMetadata(CompositeToOneKeyState::class),
]
$this->createSchemaForModels(
Country::class,
CompositeToOneKeyState::class
);
}

Expand Down
11 changes: 1 addition & 10 deletions tests/Doctrine/Tests/ORM/Functional/MergeSharedEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Tests\OrmFunctionalTestCase;

use function serialize;
Expand All @@ -22,15 +21,7 @@ protected function setUp(): void
{
parent::setUp();

try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(MSEFile::class),
$this->_em->getClassMetadata(MSEPicture::class),
]
);
} catch (ToolsException $ignored) {
}
$this->createSchemaForModels(MSEFile::class, MSEPicture::class);
}

public function testMergeSharedNewEntities(): void
Expand Down
5 changes: 1 addition & 4 deletions tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ protected function setUp(): void

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8383');

$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(NotifyUser::class),
$this->_em->getClassMetadata(NotifyGroup::class),
]);
$this->createSchemaForModels(NotifyUser::class, NotifyGroup::class);
}

public function testChangeTracking(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Tests\Models\OneToOneInverseSideLoad\InverseSide;
use Doctrine\Tests\Models\OneToOneInverseSideLoad\OwningSide;
use Doctrine\Tests\OrmFunctionalTestCase;
Expand All @@ -17,14 +16,7 @@ protected function setUp(): void
{
parent::setUp();

try {
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(OwningSide::class),
$this->_em->getClassMetadata(InverseSide::class),
]);
} catch (ToolsException $e) {
// ignored
}
$this->createSchemaForModels(OwningSide::class, InverseSide::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public function testLazyLoadsAssociation(): void

public function testMultiSelfReference(): void
{
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(MultiSelfReference::class),
]);
$this->createSchemaForModels(MultiSelfReference::class);

$entity1 = new MultiSelfReference();
$this->_em->persist($entity1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ protected function setUp(): void
{
parent::setUp();

$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(Pet::class),
$this->_em->getClassMetadata(Cat::class),
$this->_em->getClassMetadata(LitterBox::class),
]);
$this->createSchemaForModels(
Pet::class,
Cat::class,
LitterBox::class
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class OrderedJoinedTableInheritanceCollectionTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(OJTICPet::class),
$this->_em->getClassMetadata(OJTICCat::class),
$this->_em->getClassMetadata(OJTICDog::class),
]);
$this->createSchemaForModels(
OJTICPet::class,
OJTICCat::class,
OJTICDog::class
);

$dog = new OJTICDog();
$dog->name = 'Poofy';
Expand Down
19 changes: 3 additions & 16 deletions tests/Doctrine/Tests/ORM/Functional/SchemaTool/DBAL483Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,23 @@
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Tools;
use Doctrine\Tests\OrmFunctionalTestCase;

use function array_filter;
use function str_contains;

class DBAL483Test extends OrmFunctionalTestCase
{
/** @var Tools\SchemaTool */
private $schemaTool;

protected function setUp(): void
{
parent::setUp();

$this->_em->getConnection();

$this->schemaTool = new Tools\SchemaTool($this->_em);
}

/**
* @group DBAL-483
*/
public function testDefaultValueIsComparedCorrectly(): void
{
$class = $this->_em->getClassMetadata(DBAL483Default::class);
$class = DBAL483Default::class;

$this->schemaTool->createSchema([$class]);
$this->createSchemaForModels($class);

$updateSql = $this->schemaTool->getUpdateSchemaSql([$class]);
$updateSql = $this->getUpdateSchemaSqlForModels($class);

$updateSql = array_filter($updateSql, static function ($sql) {
return str_contains($sql, 'DBAL483');
Expand Down
44 changes: 12 additions & 32 deletions tests/Doctrine/Tests/ORM/Functional/SchemaTool/DDC214Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\ORM\Tools;
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

use function array_filter;
use function implode;
Expand All @@ -23,12 +21,6 @@
*/
class DDC214Test extends OrmFunctionalTestCase
{
/** @psalm-var list<class-string> */
private $classes = [];

/** @var Tools\SchemaTool */
private $schemaTool = null;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -38,63 +30,51 @@ protected function setUp(): void
if ($conn->getDriver()->getDatabasePlatform() instanceof SqlitePlatform) {
self::markTestSkipped('SQLite does not support ALTER TABLE statements.');
}

$this->schemaTool = new Tools\SchemaTool($this->_em);
}

/**
* @group DDC-214
*/
public function testCmsAddressModel(): void
{
$this->classes = [
$this->assertCreatedSchemaNeedsNoUpdates(
Models\CMS\CmsUser::class,
Models\CMS\CmsPhonenumber::class,
Models\CMS\CmsAddress::class,
Models\CMS\CmsGroup::class,
Models\CMS\CmsArticle::class,
Models\CMS\CmsEmail::class,
];

$this->assertCreatedSchemaNeedsNoUpdates($this->classes);
Models\CMS\CmsEmail::class
);
}

/**
* @group DDC-214
*/
public function testCompanyModel(): void
{
$this->classes = [
$this->assertCreatedSchemaNeedsNoUpdates(
Models\Company\CompanyPerson::class,
Models\Company\CompanyEmployee::class,
Models\Company\CompanyManager::class,
Models\Company\CompanyOrganization::class,
Models\Company\CompanyEvent::class,
Models\Company\CompanyAuction::class,
Models\Company\CompanyRaffle::class,
Models\Company\CompanyCar::class,
];

$this->assertCreatedSchemaNeedsNoUpdates($this->classes);
Models\Company\CompanyCar::class
);
}

public function assertCreatedSchemaNeedsNoUpdates($classes): void
/**
* @param class-string ...$classes
*/
public function assertCreatedSchemaNeedsNoUpdates(string ...$classes): void
{
$classMetadata = [];
foreach ($classes as $class) {
$classMetadata[] = $this->_em->getClassMetadata($class);
}

try {
$this->schemaTool->createSchema($classMetadata);
} catch (Exception $e) {
// was already created
}
$this->createSchemaForModels(...$classes);

$sm = $this->createSchemaManager();

$fromSchema = $sm->createSchema();
$toSchema = $this->schemaTool->getSchemaFromMetadata($classMetadata);
$toSchema = $this->getSchemaForModels(...$classes);

if (method_exists($sm, 'createComparator')) {
$comparator = $sm->createComparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;

Expand Down Expand Up @@ -44,15 +43,10 @@ public function testPostgresMetadataSequenceIncrementedBy10(): void
*/
public function testUpdateSchemaWithPostgreSQLSchema(): void
{
$classes = [
$this->_em->getClassMetadata(DDC1657Screen::class),
$this->_em->getClassMetadata(DDC1657Avatar::class),
];

$tool = new SchemaTool($this->_em);
$tool->createSchema($classes);

$sql = $tool->getUpdateSchemaSql($classes);
$sql = $this->getUpdateSchemaSqlForModels(
DDC1657Screen::class,
DDC1657Avatar::class
);
$sql = array_filter($sql, static function ($sql) {
return str_starts_with($sql, 'DROP SEQUENCE stonewood.');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ protected function setUp(): void
'This test is special to platforms emulating IDENTITY key generation strategy through sequences.'
);
} else {
$this->_schemaTool->createSchema(
[$this->_em->getClassMetadata(SequenceEmulatedIdentityEntity::class)]
);
$this->createSchemaForModels(SequenceEmulatedIdentityEntity::class);
}
}

Expand Down
10 changes: 4 additions & 6 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1080Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ class DDC1080Test extends OrmFunctionalTestCase
{
public function testHydration(): void
{
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC1080Foo::class),
$this->_em->getClassMetadata(DDC1080Bar::class),
$this->_em->getClassMetadata(DDC1080FooBar::class),
]
$this->createSchemaForModels(
DDC1080Foo::class,
DDC1080Bar::class,
DDC1080FooBar::class
);

$foo1 = new DDC1080Foo();
Expand Down
12 changes: 5 additions & 7 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1163Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ protected function setUp(): void
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC1163Product::class),
$this->_em->getClassMetadata(DDC1163SpecialProduct::class),
$this->_em->getClassMetadata(DDC1163ProxyHolder::class),
$this->_em->getClassMetadata(DDC1163Tag::class),
]
$this->createSchemaForModels(
DDC1163Product::class,
DDC1163SpecialProduct::class,
DDC1163ProxyHolder::class,
DDC1163Tag::class
);
}

Expand Down
10 changes: 4 additions & 6 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1181Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ class DDC1181Test extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC1181Hotel::class),
$this->_em->getClassMetadata(DDC1181Booking::class),
$this->_em->getClassMetadata(DDC1181Room::class),
]
$this->createSchemaForModels(
DDC1181Hotel::class,
DDC1181Booking::class,
DDC1181Room::class
);
}

Expand Down
10 changes: 4 additions & 6 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1193Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ protected function setUp(): void
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC1193Company::class),
$this->_em->getClassMetadata(DDC1193Person::class),
$this->_em->getClassMetadata(DDC1193Account::class),
]
$this->createSchemaForModels(
DDC1193Company::class,
DDC1193Person::class,
DDC1193Account::class
);
}

Expand Down
Loading

0 comments on commit 8a94d0c

Please sign in to comment.