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

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

Closed
skuhnow opened this issue Dec 31, 2020 · 19 comments
Closed

Comments

@skuhnow
Copy link

skuhnow commented Dec 31, 2020

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.

@kbond
Copy link
Member

kbond commented Dec 31, 2020

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

@kbond
Copy link
Member

kbond commented Mar 4, 2021

Closing as stale. If you're still having issues, let me know and I'll reopen.

@kbond kbond closed this as completed Mar 4, 2021
@marcinmdev
Copy link

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'

@kbond
Copy link
Member

kbond commented Jan 10, 2023

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.

@Perf
Copy link

Perf commented Dec 11, 2023

@kbond
I had fresh symfony installation (6.4.1) with zenstruck/foundry v1.36.0.
When trying to create fixtures like:

class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        UserFactory::createMany(25);
    }
}

Or in tests, like:

describe('User Resource', function () {
    it('Fetches Users collection', function () {
        UserFactory::createMany(50);

        $res = $this->getJson('users');

        dd($res->getContent(false));
    });
});

I always getting an error:
Model Factories with dependencies (Model Factory services) cannot be used without the foundry bundle.
However, it's installed with Flex, so bundle it added into bundles.php in test and dev envs.
Also, I tried to serach containers with tag foundry.factory or even just foundry and can't find anything.

@kbond
Copy link
Member

kbond commented Dec 11, 2023

@Perf where is your userfacrory located? It needs to be autoconfigured/autowired.

@Perf
Copy link

Perf commented Dec 11, 2023

@Perf where is your userfacrory located? It needs to be autoconfigured/autowired.

it's located under src/ folder (\App\User\Doctrine\Factory\UserFactory) so it should be autoconfigured and autowired. Other classes under src/ are autoconfigured and autowired.

@kbond
Copy link
Member

kbond commented Dec 11, 2023

Hmm, ok, can you find the factory with debug:container?

@kbond
Copy link
Member

kbond commented Dec 11, 2023

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.

@Perf
Copy link

Perf commented Dec 12, 2023

Hmm, ok, can you find the factory with debug:container?

nope, absolutely nothing found

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

parameters:

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    _instanceof:
        App\Shared\Application\Command\CommandHandlerInterface:
            tags:
                - { name: messenger.message_handler, bus: command.bus }

        App\Shared\Application\Query\QueryHandlerInterface:
            tags:
                - { name: messenger.message_handler, bus: query.bus }

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

    App\Shared\:
        resource: '../src/Shared/'
        exclude:
            - '../src/Shared/**/{Entity,ValueObject,CompilerPass}/**'
            - '../src/Shared/**/ApiPlatform/{Dto,Resource}/**'
            - '../src/Shared/**/*Exception.php'
            - '../src/Shared/Infrastructure/Symfony/Kernel.php'

@nikophil
Copy link
Member

Hi @Perf

with this service.yaml, you should be able to retrieve App\User\Doctrine\Factory\UserFactory with $ bin/console debug:container UserFactory... this is not even related to foundry 🤔

don't you have any other config file? maybe for test environment?

@Perf
Copy link

Perf commented Dec 12, 2023

Hi @Perf

with this service.yaml, you should be able to retrieve App\User\Doctrine\Factory\UserFactory with $ bin/console debug:container UserFactory... this is not even related to foundry 🤔

Hi @nikophil

$ bin/console de:cont UserFactory

No services found that match "UserFactory".

$ bin/console de:cont --tag=foundry.factory

No tags found that match "foundry.factory".

don't you have any other config file? maybe for test environment?

I have 2 other configs, but they are empty

imports:
    - { resource: services/shared.yaml }
    - { resource: services/user.yaml }

no configs for test env

@nikophil
Copy link
Member

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.

@Perf
Copy link

Perf commented Dec 12, 2023

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!

@nikophil
Copy link
Member

I'm curious to know the origin of the problem, when you'll find it out!

@Perf
Copy link

Perf commented Dec 12, 2023

I'm curious to know the origin of the problem, when you'll find it out!

I updated excludes to the following and now UserFactory works!

    App\User\:
        resource: '../src/User/'
        exclude:
            - '../src/User/Domain/{Entity,ValueObject}/'
            - '../src/User/Infrastructure/ApiPlatform/{Dto,Resource}/'
            - '../src/User/Infrastructure/Doctrine/DataFixtures/'
            - '../src/User/**/*Exception.php'

    App\Shared\:
        resource: '../src/Shared/'
        exclude:
            - '../src/Shared/Domain/{Entity,ValueObject}/'
            - '../src/Shared/Infrastructure/ApiPlatform/{Dto,Resource}/'
            - '../src/Shared/Infrastructure/Doctrine/DataFixtures/'
            - '../src/Shared/Infrastructure/Symfony/Kernel.php'
            - '../src/Shared/Infrastructure/Symfony/CompilerPass/'
            - '../src/Shared/**/*Exception.php'

Now excludes are more explicit )

@nikophil
Copy link
Member

yes but, which one was excluding the path src/User/Doctrine/Factory/UserFactory? 🤔

@Perf
Copy link

Perf commented Dec 12, 2023

@nikophil

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

@nikophil
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants