1
0
mirror of synced 2025-03-06 12:56:10 +03:00

Expect an exception instead of failing the test

Since that's the assertion we're actually doing.
This commit is contained in:
Luís Cobucci 2017-05-31 08:14:53 +02:00
parent 19fc91482e
commit 8796e2d938
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
4 changed files with 48 additions and 41 deletions

View File

@ -740,11 +740,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
$address->user = $user; $address->user = $user;
$this->_em->persist($address); $this->_em->persist($address);
// pretend we forgot to persist $user
try { // flushing without persisting $user should raise an exception
$this->_em->flush(); // should raise an exception $this->expectException(\InvalidArgumentException::class);
$this->fail(); $this->_em->flush();
} catch (\InvalidArgumentException $expected) {}
} }
/** /**
@ -774,11 +773,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
$u2->name = "Benjamin E."; $u2->name = "Benjamin E.";
$u2->status = 'inactive'; $u2->status = 'inactive';
$address->user = $u2; $address->user = $u2;
// pretend we forgot to persist $u2
try { // flushing without persisting $u2 should raise an exception
$this->_em->flush(); // should raise an exception $this->expectException(\InvalidArgumentException::class);
$this->fail(); $this->_em->flush();
} catch (\InvalidArgumentException $expected) {}
} }
/** /**
@ -797,11 +795,10 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
$art->addComment($com); $art->addComment($com);
$this->_em->persist($art); $this->_em->persist($art);
// pretend we forgot to persist $com
try { // flushing without persisting $com should raise an exception
$this->_em->flush(); // should raise an exception $this->expectException(\InvalidArgumentException::class);
$this->fail(); $this->_em->flush();
} catch (\InvalidArgumentException $expected) {}
} }
public function testOneToOneOrphanRemoval() public function testOneToOneOrphanRemoval()
@ -933,10 +930,9 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
$user->name = "Benjamin E."; $user->name = "Benjamin E.";
$user->status = 'active'; $user->status = 'active';
$user->id = 42; $user->id = 42;
try {
$this->expectException(EntityNotFoundException::class);
$this->_em->merge($user); $this->_em->merge($user);
$this->fail();
} catch (EntityNotFoundException $enfe) {}
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Proxy\Proxy; use Doctrine\ORM\Proxy\Proxy;
use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsUser;
@ -113,14 +114,16 @@ class DetachedEntityTest extends OrmFunctionalTestCase
{ {
$ph = new CmsPhonenumber(); $ph = new CmsPhonenumber();
$ph->phonenumber = '12345'; $ph->phonenumber = '12345';
$this->_em->persist($ph); $this->_em->persist($ph);
$this->_em->flush(); $this->_em->flush();
$this->_em->clear(); $this->_em->clear();
$this->_em->persist($ph); $this->_em->persist($ph);
try {
// since it tries to insert the object twice (with the same PK)
$this->expectException(UniqueConstraintViolationException::class);
$this->_em->flush(); $this->_em->flush();
$this->fail();
} catch (\Exception $expected) {}
} }
public function testUninitializedLazyAssociationsAreIgnoredOnMerge() public function testUninitializedLazyAssociationsAreIgnoredOnMerge()

View File

@ -72,17 +72,22 @@ class SingleScalarHydratorTest extends HydrationTestCase
$stmt = new HydratorMockStatement($resultSet); $stmt = new HydratorMockStatement($resultSet);
$hydrator = new SingleScalarHydrator($this->_em); $hydrator = new SingleScalarHydrator($this->_em);
if ($name == 'result1') { if ($name === 'result1') {
$result = $hydrator->hydrateAll($stmt, $rsm); $result = $hydrator->hydrateAll($stmt, $rsm);
$this->assertEquals('romanb', $result); $this->assertEquals('romanb', $result);
} else if ($name == 'result2') { return;
}
if ($name === 'result2') {
$result = $hydrator->hydrateAll($stmt, $rsm); $result = $hydrator->hydrateAll($stmt, $rsm);
$this->assertEquals(1, $result); $this->assertEquals(1, $result);
} else if ($name == 'result3' || $name == 'result4') {
try { return;
$result = $hydrator->hydrateAll($stmt, $rsm); }
$this->fail();
} catch (\Doctrine\ORM\NonUniqueResultException $e) {} if (in_array($name, ['result3', 'result4'], true)) {
$this->expectException(NonUniqueResultException::class);
$hydrator->hydrateAll($stmt, $rsm);
} }
} }
} }

View File

@ -22,19 +22,22 @@ class AssignedGeneratorTest extends OrmTestCase
$this->_assignedGen = new AssignedGenerator; $this->_assignedGen = new AssignedGenerator;
} }
public function testThrowsExceptionIfIdNotAssigned() /**
* @dataProvider entitiesWithoutId
*/
public function testThrowsExceptionIfIdNotAssigned($entity)
{ {
try { $this->expectException(ORMException::class);
$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) {}
try {
$entity = new AssignedCompositeIdEntity;
$this->_assignedGen->generate($this->_em, $entity); $this->_assignedGen->generate($this->_em, $entity);
$this->fail('Assigned generator did not throw exception even though ID was missing.'); }
} catch (ORMException $expected) {}
public function entitiesWithoutId(): array
{
return [
'single' => [new AssignedSingleIdEntity()],
'composite' => [new AssignedCompositeIdEntity()],
];
} }
public function testCorrectIdGeneration() public function testCorrectIdGeneration()