From 96b4c763e43604f87a879631569e7abf082be551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 28 Aug 2021 16:37:36 +0200 Subject: [PATCH] Stop swallowing exceptions This was probably done in order to get rid of exceptions about tables already existing, but may and does swallow other exceptions as well, for instance exceptions about sequences failing to be created on Oracle because the identifier is too long. This makes it unnecessarily hard to understand what is going on. --- .../ORM/Functional/DefaultValuesTest.php | 23 ++++---- .../ORM/Functional/LifecycleCallbackTest.php | 38 +++++++------ .../ORM/Functional/Locking/OptimisticTest.php | 55 ++++++++++++++----- .../Tests/ORM/Functional/NotifyPolicyTest.php | 14 ++--- ...OneToOneSelfReferentialAssociationTest.php | 12 +--- ...edJoinedTableInheritanceCollectionTest.php | 16 ++---- .../SequenceEmulatedIdentityStrategyTest.php | 10 +--- .../ORM/Functional/Ticket/DDC1690Test.php | 14 ++--- .../ORM/Functional/Ticket/DDC440Test.php | 14 ++--- .../ORM/Functional/Ticket/GH8499Test.php | 47 ++++++++++++---- .../ORM/Functional/Ticket/Ticket2481Test.php | 12 +--- 11 files changed, 138 insertions(+), 117 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php b/tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php index d2fd2794427..29889d90c27 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php @@ -24,16 +24,19 @@ class DefaultValuesTest extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(DefaultValueUser::class), - $this->_em->getClassMetadata(DefaultValueAddress::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $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(); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php index 2eecdff0076..404012295fe 100644 --- a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php @@ -34,6 +34,7 @@ use function count; use function current; use function get_class; +use function iterator_to_array; use function sprintf; class LifecycleCallbackTest extends OrmFunctionalTestCase @@ -41,18 +42,23 @@ class LifecycleCallbackTest extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(LifecycleCallbackEventArgEntity::class), - $this->_em->getClassMetadata(LifecycleCallbackTestEntity::class), - $this->_em->getClassMetadata(LifecycleCallbackTestUser::class), - $this->_em->getClassMetadata(LifecycleCallbackCascader::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $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(); } public function testPreSavePostSaveCallbacksAreInvoked(): void @@ -261,7 +267,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIteration(): void $query = $this->_em->createQuery(sprintf($dql, $e1->getId(), $e2->getId())); - $result = $query->iterate(); + $result = iterator_to_array($query->iterate()); foreach ($result as $entity) { self::assertTrue($entity[0]->postLoadCallbackInvoked); @@ -270,7 +276,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIteration(): void break; } - $iterableResult = $query->toIterable(); + $iterableResult = iterator_to_array($query->toIterable()); foreach ($iterableResult as $entity) { self::assertTrue($entity->postLoadCallbackInvoked); @@ -296,7 +302,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIterationWithSimple 'SELECT e FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity AS e' ); - $result = $query->iterate(null, Query::HYDRATE_SIMPLEOBJECT); + $result = iterator_to_array($query->iterate(null, Query::HYDRATE_SIMPLEOBJECT)); foreach ($result as $entity) { self::assertTrue($entity[0]->postLoadCallbackInvoked); @@ -305,7 +311,7 @@ public function testCascadedEntitiesNotLoadedInPostLoadDuringIterationWithSimple break; } - $result = $query->toIterable([], Query::HYDRATE_SIMPLEOBJECT); + $result = iterator_to_array($query->toIterable([], Query::HYDRATE_SIMPLEOBJECT)); foreach ($result as $entity) { self::assertTrue($entity->postLoadCallbackInvoked); diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php index 428389254d3..4ee1304a97a 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php @@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM\Functional\Locking; use DateTime; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\LockMode; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\DiscriminatorColumn; @@ -24,28 +25,38 @@ class OptimisticTest extends OrmFunctionalTestCase { + /** @var Connection */ + private $_conn; + protected function setUp(): void { parent::setUp(); + $this->_conn = $this->_em->getConnection(); + } - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(OptimisticJoinedParent::class), - $this->_em->getClassMetadata(OptimisticJoinedChild::class), - $this->_em->getClassMetadata(OptimisticStandard::class), - $this->_em->getClassMetadata(OptimisticTimestamp::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + 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), + ]); + } - $this->_conn = $this->_em->getConnection(); + 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), + ]); } public function testJoinedChildInsertSetsInitialVersionValue(): OptimisticJoinedChild { + $this->createSchema(); $test = new OptimisticJoinedChild(); $test->name = 'child'; @@ -83,10 +94,13 @@ public function testJoinedChildFailureThrowsException(OptimisticJoinedChild $chi } catch (OptimisticLockException $e) { self::assertSame($test, $e->getEntity()); } + + $this->dropSchema(); } public function testJoinedParentInsertSetsInitialVersionValue(): OptimisticJoinedParent { + $this->createSchema(); $test = new OptimisticJoinedParent(); $test->name = 'parent'; @@ -123,10 +137,14 @@ public function testJoinedParentFailureThrowsException(OptimisticJoinedParent $p } catch (OptimisticLockException $e) { self::assertSame($test, $e->getEntity()); } + + $this->dropSchema(); } public function testMultipleFlushesDoIncrementalUpdates(): void { + $this->createSchema(); + $test = new OptimisticStandard(); for ($i = 0; $i < 5; $i++) { @@ -138,10 +156,14 @@ public function testMultipleFlushesDoIncrementalUpdates(): void self::assertIsInt($test->getVersion()); self::assertEquals($i + 1, $test->getVersion()); } + + $this->dropSchema(); } public function testStandardInsertSetsInitialVersionValue(): OptimisticStandard { + $this->createSchema(); + $test = new OptimisticStandard(); $test->name = 'test'; @@ -179,10 +201,13 @@ public function testStandardFailureThrowsException(OptimisticStandard $entity): } catch (OptimisticLockException $e) { self::assertSame($test, $e->getEntity()); } + + $this->dropSchema(); } public function testLockWorksWithProxy(): void { + $this->createSchema(); $test = new OptimisticStandard(); $test->name = 'test'; @@ -195,10 +220,12 @@ public function testLockWorksWithProxy(): void $this->_em->lock($proxy, LockMode::OPTIMISTIC, 1); $this->addToAssertionCount(1); + $this->dropSchema(); } public function testOptimisticTimestampSetsDefaultValue(): OptimisticTimestamp { + $this->createSchema(); $test = new OptimisticTimestamp(); $test->name = 'Testing'; @@ -274,6 +301,8 @@ public function testOptimisticTimestampLockFailureThrowsException(OptimisticTime self::assertNotNull($caughtException, 'No OptimisticLockingException was thrown'); self::assertSame($test, $caughtException->getEntity()); + + $this->dropSchema(); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php b/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php index cf18cc83c93..cdf9f995c5d 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NotifyPolicyTest.php @@ -33,16 +33,10 @@ protected function setUp(): void $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8383'); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(NotifyUser::class), - $this->_em->getClassMetadata(NotifyGroup::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(NotifyUser::class), + $this->_em->getClassMetadata(NotifyGroup::class), + ]); } public function testChangeTracking(): void diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToOneSelfReferentialAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToOneSelfReferentialAssociationTest.php index b286abf438c..8abe8a78553 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToOneSelfReferentialAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToOneSelfReferentialAssociationTest.php @@ -99,15 +99,9 @@ public function testLazyLoadsAssociation(): void public function testMultiSelfReference(): void { - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(MultiSelfReference::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(MultiSelfReference::class), + ]); $entity1 = new MultiSelfReference(); $this->_em->persist($entity1); diff --git a/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php index 56a5c1ab823..b9e14e0ff6b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OrderedJoinedTableInheritanceCollectionTest.php @@ -30,17 +30,11 @@ class OrderedJoinedTableInheritanceCollectionTest extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(OJTICPet::class), - $this->_em->getClassMetadata(OJTICCat::class), - $this->_em->getClassMetadata(OJTICDog::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(OJTICPet::class), + $this->_em->getClassMetadata(OJTICCat::class), + $this->_em->getClassMetadata(OJTICDog::class), + ]); $dog = new OJTICDog(); $dog->name = 'Poofy'; diff --git a/tests/Doctrine/Tests/ORM/Functional/SequenceEmulatedIdentityStrategyTest.php b/tests/Doctrine/Tests/ORM/Functional/SequenceEmulatedIdentityStrategyTest.php index 26d9a057aa0..2edd1eea2e9 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SequenceEmulatedIdentityStrategyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SequenceEmulatedIdentityStrategyTest.php @@ -26,13 +26,9 @@ protected function setUp(): void 'This test is special to platforms emulating IDENTITY key generation strategy through sequences.' ); } else { - try { - $this->_schemaTool->createSchema( - [$this->_em->getClassMetadata(SequenceEmulatedIdentityEntity::class)] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema( + [$this->_em->getClassMetadata(SequenceEmulatedIdentityEntity::class)] + ); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1690Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1690Test.php index f3eaddf8de3..5a38169d7fa 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1690Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1690Test.php @@ -24,16 +24,10 @@ class DDC1690Test extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(DDC1690Parent::class), - $this->_em->getClassMetadata(DDC1690Child::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(DDC1690Parent::class), + $this->_em->getClassMetadata(DDC1690Child::class), + ]); } public function testChangeTracking(): void diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php index 91a3955e0d0..89d1d02ac40 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC440Test.php @@ -24,16 +24,10 @@ class DDC440Test extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(DDC440Phone::class), - $this->_em->getClassMetadata(DDC440Client::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(DDC440Phone::class), + $this->_em->getClassMetadata(DDC440Client::class), + ]); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8499Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8499Test.php index a18cb99890a..cd9e87cd554 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8499Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH8499Test.php @@ -29,16 +29,21 @@ class GH8499Test extends OrmFunctionalTestCase protected function setUp(): void { parent::setUp(); + $this->conn = $this->_em->getConnection(); + } - try { - $this->_schemaTool->createSchema( - [$this->_em->getClassMetadata(GH8499VersionableEntity::class)] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + protected function createSchema(): void + { + $this->_schemaTool->createSchema( + [$this->_em->getClassMetadata(GH8499VersionableEntity::class)] + ); + } - $this->conn = $this->_em->getConnection(); + protected function dropSchema(): void + { + $this->_schemaTool->dropSchema( + [$this->_em->getClassMetadata(GH8499VersionableEntity::class)] + ); } /** @@ -46,6 +51,7 @@ protected function setUp(): void */ public function testOptimisticTimestampSetsDefaultValue(): GH8499VersionableEntity { + $this->createSchema(); $entity = new GH8499VersionableEntity(); $entity->setName('Test Entity'); $entity->setDescription('Entity to test optimistic lock fix with DateTimeInterface objects'); @@ -70,9 +76,15 @@ public function testOptimisticLockWithDateTimeForVersion(GH8499VersionableEntity $test = $q->getSingleResult(); $format = $this->_em->getConnection()->getDatabasePlatform()->getDateTimeFormatString(); - $modifiedDate = new DateTime(date($format, strtotime($test->getRevision()->format($format)) - 3600)); + $modifiedDate = new DateTime(date( + $format, + strtotime($test->getRevision()->format($format)) - 3600 + )); - $this->conn->executeQuery('UPDATE GH8499VersionableEntity SET revision = ? WHERE id = ?', [$modifiedDate->format($format), $test->id]); + $this->conn->executeQuery( + 'UPDATE GH8499VersionableEntity SET revision = ? WHERE id = ?', + [$modifiedDate->format($format), $test->id] + ); $this->_em->refresh($test); $this->_em->lock($test, LockMode::OPTIMISTIC, $modifiedDate); @@ -81,8 +93,17 @@ public function testOptimisticLockWithDateTimeForVersion(GH8499VersionableEntity $this->_em->persist($test); $this->_em->flush(); - self::assertEquals('Test Entity Locked', $test->getName(), 'Entity not modified after persist/flush,'); - self::assertGreaterThan($modifiedDate->getTimestamp(), $test->getRevision()->getTimestamp(), 'Current version timestamp is not greater than previous one.'); + self::assertEquals( + 'Test Entity Locked', + $test->getName(), + 'Entity not modified after persist/flush,' + ); + self::assertGreaterThan( + $modifiedDate->getTimestamp(), + $test->getRevision()->getTimestamp(), + 'Current version timestamp is not greater than previous one.' + ); + $this->dropSchema(); } /** @@ -90,6 +111,7 @@ public function testOptimisticLockWithDateTimeForVersion(GH8499VersionableEntity */ public function testOptimisticLockWithDateTimeForVersionThrowsException(): void { + $this->createSchema(); $entity = new GH8499VersionableEntity(); $entity->setName('Test Entity'); $entity->setDescription('Entity to test optimistic lock fix with DateTimeInterface objects'); @@ -98,6 +120,7 @@ public function testOptimisticLockWithDateTimeForVersionThrowsException(): void $this->expectException(OptimisticLockException::class); $this->_em->lock($entity, LockMode::OPTIMISTIC, new DateTime('2020-07-15 18:04:00')); + $this->dropSchema(); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/Ticket2481Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/Ticket2481Test.php index 2fc08128d69..e99d9bb7985 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/Ticket2481Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/Ticket2481Test.php @@ -18,15 +18,9 @@ protected function setUp(): void { parent::setUp(); - try { - $this->_schemaTool->createSchema( - [ - $this->_em->getClassMetadata(Ticket2481Product::class), - ] - ); - } catch (Exception $e) { - // Swallow all exceptions. We do not test the schema tool here. - } + $this->_schemaTool->createSchema([ + $this->_em->getClassMetadata(Ticket2481Product::class), + ]); $this->_conn = $this->_em->getConnection(); }