-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
base: 2.x
Are you sure you want to change the base?
Conversation
@@ -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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
@@ -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( |
There was a problem hiding this comment.
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?
Co-authored-by: Nicolas PHILIPPE <nikophil@gmail.com>
This is intended to resolve #786, which isn't the case yet. Right now it's only a test proving the problem described in the issue.
As I don't know foundry that well, I have a little difficulties, with the naming as I don't know the terminology used in foundry. So inputs are welcome, but we get to a review state at some point anyway. :)
Update: There is something not right in my test. There order should be different, and then it works. Back to the IDE..