Skip to content

Commit

Permalink
Make creating test models more straightforward
Browse files Browse the repository at this point in the history
In doctrine#8962, I established that
swallowing exceptions while creating model was bad because it could hide
other helpful exceptions.
As it turns out however, swallowing exceptions is really the way to go
here, because of the performance implication of calling dropSchema(). It
is possible to catch a more precise exception as well, which should
preserve the benefits of not swallowing them.

It looks like I based my grep on the comment inside the catch and today,
I found many other occurrences of this pattern, without the easy to grep
comment.

I decided to fix them as well, but in a lazier way: one no longer has to
remember to call dropSchema in tearDown() now, it is automated.
  • Loading branch information
greg0ire committed Feb 19, 2022
1 parent 152c04c commit 15b685e
Show file tree
Hide file tree
Showing 47 changed files with 166 additions and 572 deletions.
26 changes: 7 additions & 19 deletions tests/Doctrine/Tests/ORM/Functional/AdvancedAssociationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,13 @@ class AdvancedAssociationTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(Phrase::class),
$this->_em->getClassMetadata(PhraseType::class),
$this->_em->getClassMetadata(Definition::class),
$this->_em->getClassMetadata(Lemma::class),
$this->_em->getClassMetadata(Type::class),
]);
}

protected function tearDown(): void
{
parent::tearDown();
$this->_schemaTool->dropSchema([
$this->_em->getClassMetadata(Phrase::class),
$this->_em->getClassMetadata(PhraseType::class),
$this->_em->getClassMetadata(Definition::class),
$this->_em->getClassMetadata(Lemma::class),
$this->_em->getClassMetadata(Type::class),
]);
$this->createSchemaForModels(
Phrase::class,
PhraseType::class,
Definition::class,
Lemma::class,
Type::class
);
}

public function testIssue(): void
Expand Down
20 changes: 3 additions & 17 deletions tests/Doctrine/Tests/ORM/Functional/CascadeRemoveOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,9 @@ protected function setUp(): void
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(CascadeRemoveOrderEntityO::class),
$this->_em->getClassMetadata(CascadeRemoveOrderEntityG::class),
]
);
}

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

$this->_schemaTool->dropSchema(
[
$this->_em->getClassMetadata(CascadeRemoveOrderEntityO::class),
$this->_em->getClassMetadata(CascadeRemoveOrderEntityG::class),
]
$this->createSchemaForModels(
CascadeRemoveOrderEntityO::class,
CascadeRemoveOrderEntityG::class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,12 @@ class ClassTableInheritanceSecondTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(CTIParent::class),
$this->_em->getClassMetadata(CTIChild::class),
$this->_em->getClassMetadata(CTIRelated::class),
$this->_em->getClassMetadata(CTIRelated2::class),
]);
}

protected function tearDown(): void
{
parent::tearDown();
$this->_schemaTool->dropSchema([
$this->_em->getClassMetadata(CTIParent::class),
$this->_em->getClassMetadata(CTIChild::class),
$this->_em->getClassMetadata(CTIRelated::class),
$this->_em->getClassMetadata(CTIRelated2::class),
]);
$this->createSchemaForModels(
CTIParent::class,
CTIChild::class,
CTIRelated::class,
CTIRelated2::class
);
}

public function testOneToOneAssocToBaseTypeBidirectional(): void
Expand Down
17 changes: 4 additions & 13 deletions tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@ class DefaultValuesTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(DefaultValueUser::class),
$this->_em->getClassMetadata(DefaultValueAddress::class),
]);
}

protected function tearDown(): void
{
$this->_schemaTool->dropSchema([
$this->_em->getClassMetadata(DefaultValueUser::class),
$this->_em->getClassMetadata(DefaultValueAddress::class),
]);
parent::tearDown();
$this->createSchemaForModels(
DefaultValueUser::class,
DefaultValueAddress::class
);
}

/**
Expand Down
23 changes: 6 additions & 17 deletions tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,12 @@ class LifecycleCallbackTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(LifecycleCallbackEventArgEntity::class),
$this->_em->getClassMetadata(LifecycleCallbackTestEntity::class),
$this->_em->getClassMetadata(LifecycleCallbackTestUser::class),
$this->_em->getClassMetadata(LifecycleCallbackCascader::class),
]);
}

protected function tearDown(): void
{
$this->_schemaTool->dropSchema([
$this->_em->getClassMetadata(LifecycleCallbackEventArgEntity::class),
$this->_em->getClassMetadata(LifecycleCallbackTestEntity::class),
$this->_em->getClassMetadata(LifecycleCallbackTestUser::class),
$this->_em->getClassMetadata(LifecycleCallbackCascader::class),
]);
parent::tearDown();
$this->createSchemaForModels(
LifecycleCallbackEventArgEntity::class,
LifecycleCallbackTestEntity::class,
LifecycleCallbackTestUser::class,
LifecycleCallbackCascader::class
);
}

public function testPreSavePostSaveCallbacksAreInvoked(): void
Expand Down
33 changes: 6 additions & 27 deletions tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,12 @@ protected function setUp(): void

private function createSchema(): void
{
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(OptimisticJoinedParent::class),
$this->_em->getClassMetadata(OptimisticJoinedChild::class),
$this->_em->getClassMetadata(OptimisticStandard::class),
$this->_em->getClassMetadata(OptimisticTimestamp::class),
]);
}

private function dropSchema(): void
{
$this->_schemaTool->dropSchema([
$this->_em->getClassMetadata(OptimisticJoinedParent::class),
$this->_em->getClassMetadata(OptimisticJoinedChild::class),
$this->_em->getClassMetadata(OptimisticStandard::class),
$this->_em->getClassMetadata(OptimisticTimestamp::class),
]);
$this->createSchemaForModels(
OptimisticJoinedParent::class,
OptimisticJoinedChild::class,
OptimisticStandard::class,
OptimisticTimestamp::class
);
}

public function testJoinedChildInsertSetsInitialVersionValue(): OptimisticJoinedChild
Expand Down Expand Up @@ -93,8 +83,6 @@ public function testJoinedChildFailureThrowsException(OptimisticJoinedChild $chi
} catch (OptimisticLockException $e) {
self::assertSame($test, $e->getEntity());
}

$this->dropSchema();
}

public function testJoinedParentInsertSetsInitialVersionValue(): OptimisticJoinedParent
Expand Down Expand Up @@ -136,8 +124,6 @@ public function testJoinedParentFailureThrowsException(OptimisticJoinedParent $p
} catch (OptimisticLockException $e) {
self::assertSame($test, $e->getEntity());
}

$this->dropSchema();
}

public function testMultipleFlushesDoIncrementalUpdates(): void
Expand All @@ -155,8 +141,6 @@ public function testMultipleFlushesDoIncrementalUpdates(): void
self::assertIsInt($test->getVersion());
self::assertEquals($i + 1, $test->getVersion());
}

$this->dropSchema();
}

public function testStandardInsertSetsInitialVersionValue(): OptimisticStandard
Expand Down Expand Up @@ -200,8 +184,6 @@ public function testStandardFailureThrowsException(OptimisticStandard $entity):
} catch (OptimisticLockException $e) {
self::assertSame($test, $e->getEntity());
}

$this->dropSchema();
}

public function testLockWorksWithProxy(): void
Expand All @@ -219,7 +201,6 @@ public function testLockWorksWithProxy(): void
$this->_em->lock($proxy, LockMode::OPTIMISTIC, 1);

$this->addToAssertionCount(1);
$this->dropSchema();
}

public function testOptimisticTimestampSetsDefaultValue(): OptimisticTimestamp
Expand Down Expand Up @@ -300,8 +281,6 @@ public function testOptimisticTimestampLockFailureThrowsException(OptimisticTime

self::assertNotNull($caughtException, 'No OptimisticLockingException was thrown');
self::assertSame($test, $caughtException->getEntity());

$this->dropSchema();
}
}

Expand Down
22 changes: 7 additions & 15 deletions tests/Doctrine/Tests/ORM/Functional/OneToOneEagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Proxy\Proxy;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

use function count;
use function get_class;
Expand All @@ -30,19 +28,13 @@ class OneToOneEagerLoadingTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
$schemaTool = new SchemaTool($this->_em);
try {
$schemaTool->createSchema(
[
$this->_em->getClassMetadata(Train::class),
$this->_em->getClassMetadata(TrainDriver::class),
$this->_em->getClassMetadata(TrainOwner::class),
$this->_em->getClassMetadata(Waggon::class),
$this->_em->getClassMetadata(TrainOrder::class),
]
);
} catch (Exception $e) {
}
$this->createSchemaForModels(
Train::class,
TrainDriver::class,
TrainOwner::class,
Waggon::class,
TrainOrder::class
);
}

/**
Expand Down
14 changes: 4 additions & 10 deletions tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

class PersistentCollectionTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(PersistentCollectionHolder::class),
$this->_em->getClassMetadata(PersistentCollectionContent::class),
]
);
} catch (Exception $e) {
}
$this->createSchemaForModels(
PersistentCollectionHolder::class,
PersistentCollectionContent::class
);

PersistentObject::setObjectManager($this->_em);
}
Expand Down
10 changes: 1 addition & 9 deletions tests/Doctrine/Tests/ORM/Functional/PersistentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

/**
* Test that Doctrine ORM correctly works with the ObjectManagerAware and PersistentObject
Expand All @@ -25,14 +24,7 @@ protected function setUp(): void
{
parent::setUp();

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

PersistentObject::setObjectManager($this->_em);
}
Expand Down
24 changes: 9 additions & 15 deletions tests/Doctrine/Tests/ORM/Functional/ProxiesLikeEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Proxies\__CG__\Doctrine\Tests\Models\CMS\CmsUser as CmsUserProxy;
use Exception;

use function assert;

Expand All @@ -34,20 +33,15 @@ class ProxiesLikeEntitiesTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(CmsUser::class),
$this->_em->getClassMetadata(CmsTag::class),
$this->_em->getClassMetadata(CmsPhonenumber::class),
$this->_em->getClassMetadata(CmsArticle::class),
$this->_em->getClassMetadata(CmsAddress::class),
$this->_em->getClassMetadata(CmsEmail::class),
$this->_em->getClassMetadata(CmsGroup::class),
]
);
} catch (Exception $e) {
}
$this->createSchemaForModels(
CmsUser::class,
CmsTag::class,
CmsPhonenumber::class,
CmsArticle::class,
CmsAddress::class,
CmsEmail::class,
CmsGroup::class
);

$this->user = new CmsUser();
$this->user->username = 'ocramius';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,7 @@ protected function setUp(): void
{
parent::setUp();
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(QueryBuilderParenthesisEntity::class),
]
);
}

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

$this->_schemaTool->dropSchema(
[
$this->_em->getClassMetadata(QueryBuilderParenthesisEntity::class),
]
);
$this->createSchemaForModels(QueryBuilderParenthesisEntity::class);
}

public function testParenthesisOnSingleLine(): void
Expand Down
10 changes: 1 addition & 9 deletions tests/Doctrine/Tests/ORM/Functional/ReadOnlyTest.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\Query;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

use function get_class;

Expand All @@ -25,14 +24,7 @@ protected function setUp(): void
{
parent::setUp();

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

public function testReadOnlyEntityNeverChangeTracked(): void
Expand Down
Loading

0 comments on commit 15b685e

Please sign in to comment.