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.

It looks like I based my grep on the comment inside. 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 4db0a4f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
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
10 changes: 1 addition & 9 deletions tests/Doctrine/Tests/ORM/Functional/SequenceGeneratorTest.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\SequenceGenerator;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

/**
* Description of SequenceGeneratorTest
Expand All @@ -25,14 +24,7 @@ protected function setUp(): void
self::markTestSkipped('Only working for Databases that support sequences.');
}

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

public function testHighAllocationSizeSequence(): void
Expand Down
22 changes: 8 additions & 14 deletions tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Doctrine\ORM\Query\QueryException;
use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
use Doctrine\Tests\OrmFunctionalTestCase;
use Exception;

use function class_exists;
use function sprintf;
Expand All @@ -35,19 +34,14 @@ protected function setUp(): void
{
parent::setUp();

try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC93Person::class),
$this->_em->getClassMetadata(DDC93Address::class),
$this->_em->getClassMetadata(DDC93Vehicle::class),
$this->_em->getClassMetadata(DDC93Car::class),
$this->_em->getClassMetadata(DDC3027Animal::class),
$this->_em->getClassMetadata(DDC3027Dog::class),
]
);
} catch (Exception $e) {
}
$this->createSchemaForModels(
DDC93Person::class,
DDC93Address::class,
DDC93Vehicle::class,
DDC93Car::class,
DDC3027Animal::class,
DDC3027Dog::class
);
}

public function testMetadataHasReflectionEmbeddablesAccessible(): void
Expand Down
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\DebugUnitOfWorkListener;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
Expand Down Expand Up @@ -337,6 +338,38 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
],
];

/** @var list<class-string> */
private $modelsToTearDown;

/**
* @param list<class-string> $models
*/
final protected function createSchemaForModels(string ...$models): void
{
$this->modelsToTearDown = $models;
$this->_schemaTool->createSchema(array_map(
function (string $className): ClassMetadata {
return $this->_em->getClassMetadata($className);
},
$models
));
}

/**
* @after
*/
public function tearDownModels(): void
{
if ($this->modelsToTearDown !== null) {
$this->_schemaTool->dropSchema(array_map(
function (string $className): ClassMetadata {
return $this->_em->getClassMetadata($className);
},
$this->modelsToTearDown
));
}
}

protected function useModelSet(string $setName): void
{
$this->_usedModelSets[$setName] = true;
Expand Down

0 comments on commit 4db0a4f

Please sign in to comment.