doctrine / DoctrineFixturesBundle

Symfony integration for the doctrine/data-fixtures library
MIT License
2.46k stars 202 forks source link

Using fixtures in functional tests #316

Open Kris1992 opened 4 years ago

Kris1992 commented 4 years ago

Hi, I had problem with load fixtures in behat based functional tests. When I load fixture class with Dependency Injection inside it, it works fine, but when I try to load another fixture which depends on first one by getDependencies() method I take following error:

Type error: Too few arguments to function App\DataFixtures\UserFixtures::__construct(), 0 passed in vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php on line 231 and exactly 2 expected (Behat\Testwork\Call\Exception\FatalThrowableError)

Below is code from behat loading process and fixtures :

Behat loader:

` private function loadFixtures($fixturesNames) { //use Doctrine\Common\DataFixtures\Loader; //I tried $loader = new ContainerAwareLoader($this->kernel->getContainer()); too but with the same result :(

    $loader = new Loader();

    foreach ($fixturesNames as $fixtureName) {
        $fixture = $this->kernel->getContainer()->get('App\DataFixtures\\'.$fixtureName.'Fixtures');
        $loader->addFixture($fixture);
    }

    $executor = new ORMExecutor($this->getEntityManager());
    $executor->execute($loader->getFixtures(), true);
}

`

Fixture one (if I run this fixture in bahat alone it works)

` class UserFixtures extends BaseFixture {

private $passwordEncoder;
private $userImageManager;

//That contructor makes problem
public function __construct(UserPasswordEncoderInterface $passwordEncoder, ImagesManagerInterface $userImageManager)
{
    $this->passwordEncoder = $passwordEncoder;
    $this->userImageManager = $userImageManager;
}

`

Fixture 2: class FooFixtures extends BaseFixture implements DependentFixtureInterface { public function getDependencies() { //When I do that if, behat works ok, but if it's possible I don't want that if here because some fixtures needs only UserFixtures::class and I cannot push empty array here if ($_ENV['APP_ENV'] !== 'test') { return [ UserFixtures::class, Foo2Fixtures::class, Foo3Fixtures::class, Foo4Fixtures::class, ]; } else { return [ //UserFixtures::class, Foo2Fixtures::class, Foo3Fixtures::class, Foo4Fixtures::class, ]; } }

I'm not sure it strictly depends of DoctrineFixturesBundle, but I look for answer and maybe someone from here know the solution. Thanks for any reply.

geoff-maddock commented 3 years ago

Looking to do this as well - come to any solution to loading fixtures in functional tests?

fullbl commented 3 years ago

personally I load them once and use dama/doctrine-test-bundle to make them run into a transition!