doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.77k stars 224 forks source link

Dependent fixture with no dependencies #343

Closed yann-eugone closed 4 years ago

yann-eugone commented 4 years ago

I was working on a fixture base class that determine dependencies "automatically" using entity metadata introspection. I encountered an exception with a fixture class, implementing DependentFixtureInterface and method getDependencies returning an empty array.

This is, regarding the code, a normal behaviour : https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Loader.php#L301-L307

$dependenciesClasses = $fixture->getDependencies();

if (! is_array($dependenciesClasses) || empty($dependenciesClasses)) {
    throw new InvalidArgumentException(sprintf('Method "%s" in class "%s" must return an array of classes which are dependencies for the fixture, and it must be NOT empty.', 'getDependencies', $fixtureClass));
}

But I was wondering : should that class be considered as a regular fixture class during the sorting process ? This is pretty much a fixture without any dependency in the end...

If you are interested in such change, I can offer a PR.

alcaeus commented 4 years ago

This was discussed a couple of years ago. The simple answer is "it has always been that way", which was recently justified with "you shouldn't implement the interface if you don't have dependencies". The topic of dynamic dependencies has been dismissed, in that you should rather split the fixture classes and use common logic (e.g. by extending base classes) to avoid such dynamic dependencies in favour of explicit ones.

yann-eugone commented 4 years ago

I'm a bit worried of the "it has always been that way" response, but you are free to keep it like this.

For the archive, and those who read that in some time, you can just add a DummyFixture as dependency of every other, this is an ugly but perfect workaround.