foundry: Question/Problem: Using Foundry in Doctrine Fixtures with service injection

I want to use UserPasswordEncoderInterface in my UserFactory to set the encoded password after instantiate. When I try to create my Factory ($userAdmin = UserFactory::new();), I get:

Model Factories with dependencies (Model Factory services) cannot be used without the foundry bundle.

but Flex registered the Bundle: Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],

It looks like Foundry has booted, but the injected $factories in \Zenstruck\Foundry\ModelFactoryManager are empty.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

ok, thanks, good to know… this is… very strange! 😅

@nikophil

    App\User\:
        resource: '../src/User/'
        exclude:
            - '../src/User/**/{Entity,ValueObject,CompilerPass}/**' <<< THIS ONE
            - '../src/User/**/ApiPlatform/{Dto,Resource}/**'
            - '../src/User/**/*Exception.php'

maybe your glob patterns do not work as expected? (although they look good to me)

Foundry does not make funky stuff with dependency injection, beside of tagging classes which extend ModelFactory.

hey @nikophil good finding, I commented the exclude lines and now UserFactory is up, with proper tag so, I need to review my excludes… Thank you very much!

What does your config/services.yaml look like? Any exclusions for src/User?

I tried a branch new symfony new --version=lts with composer require --dev zenstruck/foundry and couldn’t re-create your issue. I think it’s your config.

https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#stories-as-services describes that the tag is needed (below the example). Could probably make that pop a bit more with it as a note.

I’ve encountered this issue:

  • had GlobalStory as service with param injected in constructor by DI
#services_test.yaml
services:   
    App\Story\GlobalStory:
        arguments:
            $env: 'test'
  • only used in dev/test env
  • ran bin/console doctrine:fixtures:load --append -n => Stories with dependencies (Story services) cannot be used without the foundry bundle.

Fix: set autoconfigure and autowire true to said service:

#services_test.yaml
services:   
    App\Story\GlobalStory:
        autowire: true
        autoconfigure: true
        arguments:
            $env: 'test'

Hello, I think perhaps your factories are not being autowired/autoconfigured. Can you find the service with bin/console debug:container?