From 5eb1d51db33866a52bc3ebf0336d51fd5e2d5570 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 15 Jan 2025 15:47:02 +0100 Subject: [PATCH 1/4] fix: Ensure that nested factories can be created even if class already exists --- tests/Integration/Maker/MakeFactoryTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Integration/Maker/MakeFactoryTest.php b/tests/Integration/Maker/MakeFactoryTest.php index 5d8af22d..5613eb8e 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,22 @@ 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]); + $tester->execute(['class' => Address::class, '--test' => true]); + $this->assertFileExists(self::tempFile('tests/Factory/ContactFactory.php')); + $this->assertFileExists(self::tempFile('tests/Factory/AddressFactory.php')); + } + /** * @test * @dataProvider scaToolProvider From 0e46795cb5f5df04f9e156687758b58bbdbf4dd5 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 15 Jan 2025 22:13:06 +0100 Subject: [PATCH 2/4] wip: Add comment for code, that should not be reach in the context of nested creation --- src/Maker/Factory/FactoryGenerator.php | 3 +++ 1 file changed, 3 insertions(+) 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', From 51be0a2db0e292448f9dd8d09c5d97c43091ef5a Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 16 Jan 2025 14:40:14 +0100 Subject: [PATCH 3/4] Update tests/Integration/Maker/MakeFactoryTest.php Co-authored-by: Nicolas PHILIPPE --- tests/Integration/Maker/MakeFactoryTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Maker/MakeFactoryTest.php b/tests/Integration/Maker/MakeFactoryTest.php index 5613eb8e..a1b636c4 100644 --- a/tests/Integration/Maker/MakeFactoryTest.php +++ b/tests/Integration/Maker/MakeFactoryTest.php @@ -129,10 +129,17 @@ public function can_create_factory_in_test_dir_with_nested_factory_already_creat } $tester = $this->makeFactoryCommandTester(); - $tester->execute(['class' => Contact::class, '--test' => true]); - $tester->execute(['class' => Address::class, '--test' => true]); + + $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]); } /** From e7f55e01c91a275a47129d48406bd6c268d22c1b Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 16 Jan 2025 14:51:27 +0100 Subject: [PATCH 4/4] fix: add missing ) --- tests/Integration/Maker/MakeFactoryTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/Integration/Maker/MakeFactoryTest.php b/tests/Integration/Maker/MakeFactoryTest.php index a1b636c4..1df971e8 100644 --- a/tests/Integration/Maker/MakeFactoryTest.php +++ b/tests/Integration/Maker/MakeFactoryTest.php @@ -129,16 +129,15 @@ public function can_create_factory_in_test_dir_with_nested_factory_already_creat } $tester = $this->makeFactoryCommandTester(); - - $tester->execute(['class' => Contact::class, '--test' => true]); - + + $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'; - + require_once self::tempFile('tests/Factory/AddressFactory.php'); $tester->execute(['class' => Address::class, '--test' => true]); }