diff --git a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php index 016a7949f..7bdb84653 100644 --- a/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php @@ -740,11 +740,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase $address->user = $user; $this->_em->persist($address); - // pretend we forgot to persist $user - try { - $this->_em->flush(); // should raise an exception - $this->fail(); - } catch (\InvalidArgumentException $expected) {} + + // flushing without persisting $user should raise an exception + $this->expectException(\InvalidArgumentException::class); + $this->_em->flush(); } /** @@ -774,11 +773,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase $u2->name = "Benjamin E."; $u2->status = 'inactive'; $address->user = $u2; - // pretend we forgot to persist $u2 - try { - $this->_em->flush(); // should raise an exception - $this->fail(); - } catch (\InvalidArgumentException $expected) {} + + // flushing without persisting $u2 should raise an exception + $this->expectException(\InvalidArgumentException::class); + $this->_em->flush(); } /** @@ -797,11 +795,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase $art->addComment($com); $this->_em->persist($art); - // pretend we forgot to persist $com - try { - $this->_em->flush(); // should raise an exception - $this->fail(); - } catch (\InvalidArgumentException $expected) {} + + // flushing without persisting $com should raise an exception + $this->expectException(\InvalidArgumentException::class); + $this->_em->flush(); } public function testOneToOneOrphanRemoval() @@ -933,10 +930,9 @@ class BasicFunctionalTest extends OrmFunctionalTestCase $user->name = "Benjamin E."; $user->status = 'active'; $user->id = 42; - try { - $this->_em->merge($user); - $this->fail(); - } catch (EntityNotFoundException $enfe) {} + + $this->expectException(EntityNotFoundException::class); + $this->_em->merge($user); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/DetachedEntityTest.php b/tests/Doctrine/Tests/ORM/Functional/DetachedEntityTest.php index 5060f07a1..523c98c09 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DetachedEntityTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DetachedEntityTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM\Functional; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\Proxy\Proxy; use Doctrine\Tests\Models\CMS\CmsUser; @@ -113,14 +114,16 @@ class DetachedEntityTest extends OrmFunctionalTestCase { $ph = new CmsPhonenumber(); $ph->phonenumber = '12345'; + $this->_em->persist($ph); $this->_em->flush(); $this->_em->clear(); + $this->_em->persist($ph); - try { - $this->_em->flush(); - $this->fail(); - } catch (\Exception $expected) {} + + // since it tries to insert the object twice (with the same PK) + $this->expectException(UniqueConstraintViolationException::class); + $this->_em->flush(); } public function testUninitializedLazyAssociationsAreIgnoredOnMerge() diff --git a/tests/Doctrine/Tests/ORM/Hydration/SingleScalarHydratorTest.php b/tests/Doctrine/Tests/ORM/Hydration/SingleScalarHydratorTest.php index 694259b29..f9d91be63 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/SingleScalarHydratorTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/SingleScalarHydratorTest.php @@ -72,17 +72,22 @@ class SingleScalarHydratorTest extends HydrationTestCase $stmt = new HydratorMockStatement($resultSet); $hydrator = new SingleScalarHydrator($this->_em); - if ($name == 'result1') { + if ($name === 'result1') { $result = $hydrator->hydrateAll($stmt, $rsm); $this->assertEquals('romanb', $result); - } else if ($name == 'result2') { + return; + } + + if ($name === 'result2') { $result = $hydrator->hydrateAll($stmt, $rsm); $this->assertEquals(1, $result); - } else if ($name == 'result3' || $name == 'result4') { - try { - $result = $hydrator->hydrateAll($stmt, $rsm); - $this->fail(); - } catch (\Doctrine\ORM\NonUniqueResultException $e) {} + + return; + } + + if (in_array($name, ['result3', 'result4'], true)) { + $this->expectException(NonUniqueResultException::class); + $hydrator->hydrateAll($stmt, $rsm); } } } diff --git a/tests/Doctrine/Tests/ORM/Id/AssignedGeneratorTest.php b/tests/Doctrine/Tests/ORM/Id/AssignedGeneratorTest.php index acc951c16..aa0240ebc 100644 --- a/tests/Doctrine/Tests/ORM/Id/AssignedGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Id/AssignedGeneratorTest.php @@ -22,19 +22,22 @@ class AssignedGeneratorTest extends OrmTestCase $this->_assignedGen = new AssignedGenerator; } - public function testThrowsExceptionIfIdNotAssigned() + /** + * @dataProvider entitiesWithoutId + */ + public function testThrowsExceptionIfIdNotAssigned($entity) { - try { - $entity = new AssignedSingleIdEntity; - $this->_assignedGen->generate($this->_em, $entity); - $this->fail('Assigned generator did not throw exception even though ID was missing.'); - } catch (ORMException $expected) {} + $this->expectException(ORMException::class); - try { - $entity = new AssignedCompositeIdEntity; - $this->_assignedGen->generate($this->_em, $entity); - $this->fail('Assigned generator did not throw exception even though ID was missing.'); - } catch (ORMException $expected) {} + $this->_assignedGen->generate($this->_em, $entity); + } + + public function entitiesWithoutId(): array + { + return [ + 'single' => [new AssignedSingleIdEntity()], + 'composite' => [new AssignedCompositeIdEntity()], + ]; } public function testCorrectIdGeneration()