Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure that nested factories can be created even if class already exists #787

Draft
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Maker/Factory/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How I read the code, we need a guard, so this is not executed, when the class already exits. And it does second time the $tester->execute(['class' => Address::class, '--test' => true]); is called.

Copy link
Member

@nikophil nikophil Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see my other comment about autoloading)

I don't see how this could fix your initial problem 🤔

My thoughts about your problem is that by adding --test, you'd expect Foundry to check the existence of the factories in tests directory, but we don't do this, since this check is done thanks to foundry.factory tag, and this tag is not added implicitly for the services in tests directory. (for the record, the tag is added automatically for all classes loaded in the container, which extend Factory class).

I'm wondering if you do something like this, it would not resolve this problem:

# config/packages/zenstruck_foundry.yaml
when@dev: &dev
    zenstruck_foundry: ~
    services:
        Tests\Fixtures\Factories:
            resource: '../../tests/Fixtures/Factories'

when@test: *dev

this is kinda a bit strange, because, it would load some tests classes in dev environment. But doing bin/console make:factory --test uses dev environment 🤷 And of course, we could not add this kind of strangeness to Foundry 😅

I think there is not an easy solution. Maybe we should deprecate --test option and tell people that if they want to create factories in tests dir, they should use --env=test. Or maybe we should add a configuration node in which users can declare where they store their factories, so that we can tag them (or maybe no need to tag, at least add them in another way to the FactoryMap)

any thoughs @kbond?

$factoryClass,
__DIR__.'/../../../skeleton/Factory.tpl.php',
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/Maker/MakeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading