Skip to content

Commit

Permalink
Fix: Do not create entity manager in setUp()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 9, 2020
1 parent cd1eb31 commit 671f0cd
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 65 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -785,11 +785,6 @@ parameters:
count: 1
path: test/Doctrine/Fixtures/TestCase.php

-
message: "#^Method FactoryGirl\\\\Tests\\\\Provider\\\\Doctrine\\\\Fixtures\\\\TestCase\\:\\:setUp\\(\\) is not final, but since the containing class is abstract, it should be\\.$#"
count: 1
path: test/Doctrine/Fixtures/TestCase.php

-
message: "#^Parameter \\#1 \\$paths of method Doctrine\\\\ORM\\\\Configuration\\:\\:newDefaultAnnotationDriver\\(\\) expects array, string given\\.$#"
count: 1
Expand Down
24 changes: 12 additions & 12 deletions test/Doctrine/Fixtures/BasicUsageTest.php
Expand Up @@ -13,7 +13,7 @@ class BasicUsageTest extends TestCase
*/
public function acceptsConstantValuesInEntityDefinitions()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$ss = $fixtureFactory
->defineEntity(Entity\SpaceShip::class, [
Expand All @@ -31,7 +31,7 @@ public function acceptsGeneratorFunctionsInEntityDefinitions()
{
$name = "Star";

$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => function () use (&$name) {
Expand All @@ -49,7 +49,7 @@ public function acceptsGeneratorFunctionsInEntityDefinitions()
*/
public function valuesCanBeOverriddenAtCreationTime()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$ss = $fixtureFactory
->defineEntity(Entity\SpaceShip::class, [
Expand All @@ -64,7 +64,7 @@ public function valuesCanBeOverriddenAtCreationTime()
*/
public function preservesDefaultValuesOfEntity()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$ss = $fixtureFactory
->defineEntity(Entity\SpaceStation::class)
Expand All @@ -77,7 +77,7 @@ public function preservesDefaultValuesOfEntity()
*/
public function doesNotCallTheConstructorOfTheEntity()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$ss = $fixtureFactory
->defineEntity(Entity\SpaceShip::class, [])
Expand All @@ -90,7 +90,7 @@ public function doesNotCallTheConstructorOfTheEntity()
*/
public function instantiatesCollectionAssociationsToBeEmptyCollectionsWhenUnspecified()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$ss = $fixtureFactory
->defineEntity(Entity\SpaceShip::class, [
Expand All @@ -107,7 +107,7 @@ public function instantiatesCollectionAssociationsToBeEmptyCollectionsWhenUnspec
*/
public function arrayElementsAreMappedToCollectionAsscociationFields()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person::class, [
Expand All @@ -132,7 +132,7 @@ public function arrayElementsAreMappedToCollectionAsscociationFields()
*/
public function unspecifiedFieldsAreLeftNull()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$this->assertNull($fixtureFactory->get(Entity\SpaceShip::class)->getName());
Expand All @@ -143,7 +143,7 @@ public function unspecifiedFieldsAreLeftNull()
*/
public function entityIsDefinedToDefaultNamespace()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person\User::class);
Expand All @@ -164,7 +164,7 @@ public function entityIsDefinedToDefaultNamespace()
*/
public function entityCanBeDefinedToAnotherNamespace()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(
Entity\Artist::class
Expand All @@ -183,7 +183,7 @@ public function entityCanBeDefinedToAnotherNamespace()
*/
public function returnsListOfEntities()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);

Expand All @@ -195,7 +195,7 @@ public function returnsListOfEntities()
*/
public function canSpecifyNumberOfReturnedInstances()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);

Expand Down
6 changes: 3 additions & 3 deletions test/Doctrine/Fixtures/BidirectionalReferencesTest.php
Expand Up @@ -12,7 +12,7 @@ class BidirectionalReferencesTest extends TestCase
*/
public function bidirectionalOntToManyReferencesAreAssignedBothWays()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person::class, [
Expand All @@ -30,7 +30,7 @@ public function bidirectionalOntToManyReferencesAreAssignedBothWays()
*/
public function unidirectionalReferencesWorkAsUsual()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\Badge::class, [
'owner' => FieldDef::reference(Entity\Person::class)
Expand All @@ -45,7 +45,7 @@ public function unidirectionalReferencesWorkAsUsual()
*/
public function whenTheOneSideIsASingletonItMayGetSeveralChildObjects()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person::class, [
Expand Down
4 changes: 2 additions & 2 deletions test/Doctrine/Fixtures/ExtraConfigurationTest.php
Expand Up @@ -11,7 +11,7 @@ class ExtraConfigurationTest extends TestCase
*/
public function canInvokeACallbackAfterObjectConstruction()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => 'Foo'
Expand All @@ -30,7 +30,7 @@ public function canInvokeACallbackAfterObjectConstruction()
*/
public function theAfterCreateCallbackCanBeUsedToCallTheConstructor()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => 'Foo'
Expand Down
12 changes: 6 additions & 6 deletions test/Doctrine/Fixtures/IncorrectUsageTest.php
Expand Up @@ -11,7 +11,7 @@ class IncorrectUsageTest extends TestCase
*/
public function throwsWhenTryingToDefineTheSameEntityTwice()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);

Expand All @@ -25,7 +25,7 @@ public function throwsWhenTryingToDefineTheSameEntityTwice()
*/
public function throwsWhenTryingToDefineEntitiesThatAreNotEvenClasses()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$this->expectException(\Exception::class);

Expand All @@ -37,7 +37,7 @@ public function throwsWhenTryingToDefineEntitiesThatAreNotEvenClasses()
*/
public function throwsWhenTryingToDefineEntitiesThatAreNotEntities()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$this->assertTrue(class_exists(Entity\NotAnEntity::class, true));

Expand All @@ -51,7 +51,7 @@ public function throwsWhenTryingToDefineEntitiesThatAreNotEntities()
*/
public function throwsWhenTryingToDefineNonexistentFields()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$this->expectException(\Exception::class);

Expand All @@ -65,7 +65,7 @@ public function throwsWhenTryingToDefineNonexistentFields()
*/
public function throwsWhenTryingToGiveNonexistentFieldsWhileConstructing()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, ['name' => 'Alpha']);

Expand All @@ -81,7 +81,7 @@ public function throwsWhenTryingToGiveNonexistentFieldsWhileConstructing()
*/
public function throwsWhenTryingToGetLessThanOneInstance()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);

Expand Down
22 changes: 14 additions & 8 deletions test/Doctrine/Fixtures/PersistingTest.php
Expand Up @@ -13,34 +13,38 @@ class PersistingTest extends TestCase
*/
public function automaticPersistCanBeTurnedOn()
{
$fixtureFactory = new FixtureFactory($this->em);
$entityManager = self::createEntityManager();

$fixtureFactory = new FixtureFactory($entityManager);

$fixtureFactory->defineEntity(Entity\SpaceShip::class, ['name' => 'Zeta']);

$fixtureFactory->persistOnGet();

$ss = $fixtureFactory->get(Entity\SpaceShip::class);
$this->em->flush();
$entityManager->flush();

$this->assertNotNull($ss->getId());
$this->assertSame($ss, $this->em->find(Entity\SpaceShip::class, $ss->getId()));
$this->assertSame($ss, $entityManager->find(Entity\SpaceShip::class, $ss->getId()));
}

/**
* @test
*/
public function doesNotPersistByDefault()
{
$fixtureFactory = new FixtureFactory($this->em);
$entityManager = self::createEntityManager();

$fixtureFactory = new FixtureFactory($entityManager);

$fixtureFactory->defineEntity(Entity\SpaceShip::class, ['name' => 'Zeta']);

$ss = $fixtureFactory->get(Entity\SpaceShip::class);

$this->em->flush();
$entityManager->flush();

$this->assertNull($ss->getId());
$q = $this->em
$q = $entityManager
->createQueryBuilder()
->select('ss')
->from(Entity\SpaceShip::class, 'ss')
Expand All @@ -64,7 +68,9 @@ public function doesNotPersistEmbeddableWhenAutomaticPersistingIsTurnedOn()
}
}

$fixtureFactory = new FixtureFactory($this->em);
$entityManager = self::createEntityManager();

$fixtureFactory = new FixtureFactory($entityManager);

$fixtureFactory->defineEntity(Entity\Name::class, [
'first' => FieldDef::sequence(static function () {
Expand Down Expand Up @@ -99,6 +105,6 @@ public function doesNotPersistEmbeddableWhenAutomaticPersistingIsTurnedOn()
$this->assertInstanceOf(Entity\Commander::class, $commander);
$this->assertInstanceOf(Entity\Name::class, $commander->name());

$this->em->flush();
$entityManager->flush();
}
}
4 changes: 2 additions & 2 deletions test/Doctrine/Fixtures/ReferenceTest.php
Expand Up @@ -12,7 +12,7 @@ class ReferenceTest extends TestCase
*/
public function referencedObjectShouldBeCreatedAutomatically()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person::class, [
Expand All @@ -33,7 +33,7 @@ public function referencedObjectShouldBeCreatedAutomatically()
*/
public function referencedObjectsShouldBeNullable()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class);
$fixtureFactory->defineEntity(Entity\Person::class, [
Expand Down
8 changes: 4 additions & 4 deletions test/Doctrine/Fixtures/ReferencesTest.php
Expand Up @@ -15,7 +15,7 @@ class ReferencesTest extends TestCase
*/
public function referencedObjectsShouldBeCreatedAutomatically()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'crew' => FieldDef::references(Entity\Person::class)
Expand All @@ -42,7 +42,7 @@ public function referencedObjectsShouldBeOverrideable()
{
$count = 5;

$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'crew' => FieldDef::references(Entity\Person::class)
Expand All @@ -69,7 +69,7 @@ public function referencedObjectsShouldBeOverrideable()
*/
public function referencedObjectsShouldBeNullable()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'crew' => FieldDef::references(Entity\Person::class)
Expand All @@ -95,7 +95,7 @@ public function referencedObjectsShouldBeNullable()
*/
public function referencedObjectsCanBeSingletons()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'crew' => FieldDef::references(Entity\Person::class)
Expand Down
6 changes: 3 additions & 3 deletions test/Doctrine/Fixtures/SequenceTest.php
Expand Up @@ -13,7 +13,7 @@ class SequenceTest extends TestCase
*/
public function sequenceGeneratorCallsAFunctionWithAnIncrementingArgument()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => FieldDef::sequence(function ($n) {
Expand All @@ -31,7 +31,7 @@ public function sequenceGeneratorCallsAFunctionWithAnIncrementingArgument()
*/
public function sequenceGeneratorCanTakeAPlaceholderString()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => FieldDef::sequence("Beta %d")
Expand All @@ -47,7 +47,7 @@ public function sequenceGeneratorCanTakeAPlaceholderString()
*/
public function sequenceGeneratorCanTakeAStringToAppendTo()
{
$fixtureFactory = new FixtureFactory($this->em);
$fixtureFactory = new FixtureFactory(self::createEntityManager());

$fixtureFactory->defineEntity(Entity\SpaceShip::class, [
'name' => FieldDef::sequence("Gamma ")
Expand Down

0 comments on commit 671f0cd

Please sign in to comment.