diff --git a/src/Maker/Factory/FactoryGenerator.php b/src/Maker/Factory/FactoryGenerator.php index 76148a4b..02418c9a 100644 --- a/src/Maker/Factory/FactoryGenerator.php +++ b/src/Maker/Factory/FactoryGenerator.php @@ -97,6 +97,9 @@ function(string $newClassName) use ($factoryClass) { } } + // Should never do this if file/class already exists. + // A class_exists($factoryClass), return false, I don't know if it's not part of the autoloader, that the class is not found. + // I suspect that it's part of the problem. $generator->generateClass( $factoryClass, __DIR__.'/../../../skeleton/Factory.tpl.php', diff --git a/tests/Integration/Maker/MakeFactoryTest.php b/tests/Integration/Maker/MakeFactoryTest.php index 5d8af22d..1df971e8 100644 --- a/tests/Integration/Maker/MakeFactoryTest.php +++ b/tests/Integration/Maker/MakeFactoryTest.php @@ -17,6 +17,7 @@ use Zenstruck\Foundry\Maker\Factory\FactoryGenerator; use Zenstruck\Foundry\Tests\Fixture\Document\GenericDocument; use Zenstruck\Foundry\Tests\Fixture\Document\WithEmbeddableDocument; +use Zenstruck\Foundry\Tests\Fixture\Entity\Address; use Zenstruck\Foundry\Tests\Fixture\Entity\Category; use Zenstruck\Foundry\Tests\Fixture\Entity\Contact; use Zenstruck\Foundry\Tests\Fixture\Entity\GenericEntity; @@ -118,6 +119,28 @@ public function can_create_factory_in_test_dir(): void $this->assertFileExists(self::tempFile('tests/Factory/CategoryFactory.php')); } + /** + * @test + */ + public function can_create_factory_in_test_dir_with_nested_factory_already_created(): void + { + if (!\getenv('DATABASE_URL')) { + self::markTestSkipped('doctrine/orm not enabled.'); + } + + $tester = $this->makeFactoryCommandTester(); + + $tester->execute(['class' => Contact::class, '--test' => true]); + + $this->assertFileExists(self::tempFile('tests/Factory/ContactFactory.php')); + $this->assertFileExists(self::tempFile('tests/Factory/AddressFactory.php')); + + // here, we're faking multiple calls to `bin/console make:factory` + // so it's perfectly acceptable to manually load the newly created class + require_once self::tempFile('tests/Factory/AddressFactory.php'); + $tester->execute(['class' => Address::class, '--test' => true]); + } + /** * @test * @dataProvider scaToolProvider