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 719e627
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
9 changes: 1 addition & 8 deletions tests/Doctrine/Tests/ORM/Functional/PersistentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,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
21 changes: 8 additions & 13 deletions tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,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 719e627

Please sign in to comment.