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

Services not being injected on DataFixtures #579

Closed
renorram opened this issue Aug 20, 2019 · 3 comments
Closed

Services not being injected on DataFixtures #579

renorram opened this issue Aug 20, 2019 · 3 comments

Comments

@renorram
Copy link

Bug Report

Q A
BC Break no
Version 3.5.3

Summary

I have an UserFixture class that injects UserPasswordEncoderInterface but when I execute the doctrine:mongodb:fixtures:load the interface is not injected on the class causing a error.

Current behavior

The interface UserPasswordEncoderInterface is not injected on the fixture throwing a FatalThrowableError

How to reproduce

  1. Create an User document that implements UserInterface, just with the interface methods.
  2. Create a fixture that inserts an user password interface:
<?php

namespace App\DataFixtures\MongoDB;

use App\Document\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserFixtures extends Fixture
{
    private $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }

    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setEmail('admin@admin.com');
        $user->setPassword($this->passwordEncoder->encodePassword(
            $user,
            'admin123'
        ));

        $manager->persist($user);


        $manager->flush();
    }
}

Result:

/var/www/html $ php bin/console doctrine:mongodb:fixtures:load --append -vvv
21:07:05 INFO      [php] User Deprecated: The "Symfony\Component\HttpKernel\Kernel::getRootDir()" method is deprecated since Symfony 4.2, use getProjectDir() instead.
[
  "exception" => ErrorException {
    #message: "User Deprecated: The "Symfony\Component\HttpKernel\Kernel::getRootDir()" method is deprecated since Symfony 4.2, use getProjectDir() instead."
    #code: 0
    #file: "./vendor/symfony/http-kernel/Kernel.php"
    #line: 326
    #severity: E_USER_DEPRECATED
    trace: {
      ./vendor/symfony/http-kernel/Kernel.php:326 { …}
      ./vendor/doctrine/mongodb-odm-bundle/Command/LoadDataFixturesDoctrineODMCommand.php:95 { …}
      ./vendor/symfony/console/Command/Command.php:255 { …}
      ./vendor/symfony/console/Application.php:939 { …}
      ./vendor/symfony/framework-bundle/Console/Application.php:87 { …}
      ./vendor/symfony/console/Application.php:273 { …}
      ./vendor/symfony/framework-bundle/Console/Application.php:73 { …}
      ./vendor/symfony/console/Application.php:149 { …}
      ./bin/console:42 {
        › $application = new Application($kernel);
        › $application->run($input);
        ›
      }
    }
  }
]

In UserFixtures.php line 14:

  [Symfony\Component\Debug\Exception\FatalThrowableError]
  Too few arguments to function App\DataFixtures\MongoDB\UserFixtures::__construct(), 0 passed in /var/www/html/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php on line 19
  3 and exactly 1 expected


Exception trace:
 () at /var/www/html/src/DataFixtures/MongoDB/UserFixtures.php:14
 App\DataFixtures\MongoDB\UserFixtures->__construct() at /var/www/html/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php:193
 Doctrine\Common\DataFixtures\Loader->createFixture() at /var/www/html/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php:373
 Doctrine\Common\DataFixtures\Loader->loadFromIterator() at /var/www/html/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php:65
 Doctrine\Common\DataFixtures\Loader->loadFromDirectory() at /var/www/html/vendor/doctrine/mongodb-odm-bundle/Command/LoadDataFixturesDoctrineODMCommand.php:105
 Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:939
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:87
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:273
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:73
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /var/www/html/bin/console:42

doctrine:mongodb:fixtures:load [--fixtures [FIXTURES]] [-b|--bundles [BUNDLES]] [--append] [--dm DM] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

Expected behavior

The interface be injected on the fixture to create the User normally.

@renorram
Copy link
Author

closed this as is related to DoctrineMongoDB Bundle.

@alcaeus alcaeus transferred this issue from doctrine/mongodb-odm Aug 21, 2019
@alcaeus
Copy link
Member

alcaeus commented Aug 21, 2019

I've transferred the issue over, no need to close and re-open.

To answer your question, in 3.5 fixtures have to be ContainerAware to access services. Fixture services were added in #529, to be released in 3.6.0. With 4.0.0, fixture services will be the only type of fixture remaining, just like in DoctrineFixturesBundle.

@renorram
Copy link
Author

@alcaeus thanks for the feedback!

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

No branches or pull requests

2 participants