theofidry / AliceDataFixtures

Nelmio Alice extension to persist the loaded fixtures.
MIT License
311 stars 71 forks source link

Not easy to use outside symfony #175

Open ktom opened 3 years ago

ktom commented 3 years ago

Hello,

I've been trying to use the lib outside of a symfony project, in a package using doctrine.

Following the basic exemple from the doc i've been trying to do something like

<?php

$files = [
    'path/to/src/fixtures/dummy.yml',
    'path/to/src/fixtures/related_dummy.yml',
];

// $loader = $container->get('fidry_alice_data_fixtures.loader.doctrine');

// => instantiate by hand instead of relying on the container
$loader = new \Fidry\AliceDataFixtures\Loader\PurgerLoader(
    // HUUUGGGE OBJECT GRAPH with classes from
    // \Fidry\AliceDataFixtures\
    // and dozens from \Nelmio\Alice\
)

$objects = $loader->load($files);

I gave up on the huge object graph part :'(

Do you have any exemple of how to use the lib outside of SF or any advice on how I could achieve this ? Thank you in advance

theofidry commented 3 years ago

For nelmio alice there is a native loader which should help out. For this library part though there's not been a need for it yet so you would indeed need to create everything yourself by hand or alternatively register the appropriate services to your container

ktom commented 3 years ago

Thanks for the feedback,

I've been checking the native loader and indeed it's a great way to have a pre-configured loader but it's not easy to customize except by extending it.

I've finished recreating the object graph it's more or less 250 lines of code ^^

Would you consider a PR on alice introducing a builder for the loaders, something like:

class LoaderBuilderTest extends TestCase
{
    public function test_it_creates_a_prefconfigured_files_loader()
    {
        $loader = LoaderBuilder::simpleFilesLoader()
            ->getLoader();

        $this->assertInstanceOf(SimpleFilesLoader::class, $loader);
    }

    public function test_it_can_change_property_accessor()
    {
        $loader = LoaderBuilder::simpleFilesLoader()
            ->withPropertyAccessory(new MyProperyAccessor())
            ->getLoader();

        $this->assertInstanceOf(/**  */);
    }
}

I don't know what king of meaningfull tests could be done here as the goal of the builder is to set the internals of the class ... I'm also open to other suggestions that could help me use the lib outside of symfony

theofidry commented 2 years ago

@ktom would it be possible to have more details on what the graph looks like? I don't think you should have much from NelmioAlice as there is a NativeLoader or something equivalent to help out with it.

But otherwise I think it's a tedious task as it would require a native loader for different bridges (ORM, ODM, PHPCR, Eloquent...)