Load All Fixtures when one has a constructor parameter - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, March 22, 2022

Load All Fixtures when one has a constructor parameter

I would know how is possible to load all fixtures when one of them contain a constructor parameter.

For now, I use this method to load all fixtures :

/**
* @return string[]
* @codeCoverageIgnore
* load all fixtures in the good order regarding relations
*/

// Names are samples
public function getDependencies()
{
  return [
           UserFixture::class,
           AAAFixture::class,
           BBBFixture::class,
           CCCFixture::class,
           DDDFixture::class,
           EEEFixture::class,
           FFFFixture::class,
           GGGFixture::class
       ];
}

It works good when I use the symfony command :

php bin/console doctrine:fixtures:load --env=test

In my test code I can load fixtures with this methods

public function addFixture(FixtureInterface $fixture): void
{
   $this->getFixtureLoader()->addFixture($fixture);
}


public function executeFixtures(): void
{
   $this->getFixtureExecutor()->execute($this->fixtureLoader->getFixtures());
}


private function getFixtureExecutor()
{
   if (!$this->fixtureExecutor) {
     $entityManager = static::getContainer()->get('doctrine')->getManager();
     $this->fixtureExecutor = new ORMExecutor($entityManager, new 
     ORMPurger($entityManager));
   }

 return $this->fixtureExecutor;
}


private function getFixtureLoader()
{
   if (!$this->fixtureLoader) {
      $this->fixtureLoader = new ContainerAwareLoader(static::getContainer());
   }

   return $this->fixtureLoader;
}

And into my tests files :

self::$fixtureService->addFixture(new UserFixture($hasher));
self::$fixtureService->addFixture(new BBBFixture());
self::$fixtureService->executeFixtures();

But I would do this :

self::$fixtureService->addFixture(new AllFixture());
self::$fixtureService->executeFixtures();

But when I do this, an error is generated because UserFixture need the hasher password interface.

// UserFixture.php //

private UserPasswordHasherInterface $hasher;

/**
* UserFixture constructor.
* @param UserPasswordHasherInterface $hasher
* @codeCoverageIgnore
*/
public function __construct(UserPasswordHasherInterface $hasher)
{
  $this->hasher = $hasher;
}

I wouldn't do this :

self::$fixtureService->addFixture(new UserFixture($hasher));
self::$fixtureService->addFixture(new AllFixture()); // without userFixture in it

self::$fixtureService->executeFixtures();

Can someone tell me how I can solve this ?



source https://stackoverflow.com/questions/71559971/load-all-fixtures-when-one-has-a-constructor-parameter

No comments:

Post a Comment