Symfony 5 – global helpers, container, fixtures – is it worth it?

I am curious to see what other users think about my idea. I’ve done that to broaden my horizons just a little bit in the area of Symfony that I don’t get to see very often. It’s also a good way to see what works well and what doesn’t.

So in Laravel, we have those global helpers like factory(). I think this one, in particular, is really cool and useful. It might even be useful outside of testing.

I wanted to experiment with that a little bit in Symfony. This is what I came up with:

HELPERS FILE (helpers.php) – autoloaded via composer files option

<?php

use AppKernel;
use IlluminateSupportCollection;
use NelmioAliceThrowableLoadingThrowable;
use SymfonyComponentDependencyInjectionContainerInterface;
use AppServicesToolsDummyDataService;

if (!function_exists('app')) {
    /**
     * Get new instance of Application Kernel.
     *
     * @return Kernel
     */
    function app(): Kernel
    {
        $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
        $kernel->boot();

        return $kernel;
    }
}

if (!function_exists('service')) {
    /**
     * Get instance of Dependency Injection Container.
     *
     * @return ContainerInterface
     */
    function service(): ContainerInterface
    {
        return app()->getContainer();
    }
}


if (!function_exists('fixtures')) {
    /**
     * Load fixture from the given location. If not location is provided all the fixtures from default location will
     * be loaded (if there's no conflicts between them).
     *
     * @param string|null $fixtureLocation
     * @return Collection
     * @throws LoadingThrowable
     */
    function fixtures(?string $fixtureLocation = null): Collection
    {
        try {
            return service()->get(DummyDataService::class)->createAllDummyData($fixtureLocation);
        } catch (Exception $exception) {
            // TODO: log somewhere maybe in the future
            echo $exception->getMessage();
        }
    }
}

As you can see:

  • I have created app helper just like the one that Laravel has which gives me back a new instance of Kernel. I tried to find a way of hijacking the existing Kernel or just container but I think Symfony just doesn’t provide such a tool. In fact HERE they are saying that it’s very rare to have a good reason to play with the container. I agree with that 100%. However, as they said – sometimes it might be ok. Depends on what you want to do and why you want to do it. Is there any better way of doing that?

  • Next helper is simply used to provide convenient access to the container.

  • Last one, fixures() is the one I am after all along. It allows loading fixtures(dummy data) into the database using nelmio/alice bundle. You can have these fixtures in files like DummyData -> Containers -> container.yml and by default, it will look in the DummyData directory unless you will provide some other path in the argument.

  • Oh, by the way – fixtures() helper returns Laravel’s collections so you can easily loop through it and maybe create relations with other dummy data entities.

And finally take a look at the functional test I used that in (of course this is not a full test, it’s just to present the possibilities and just how it looks like):

class ContainerControllerTest extends ApiTestCase
{
    use UsesTestDatabase;

    public function test_read_container_by_slug()
    {
        fixtures('containers');

        $this->httpClient->xmlHttpRequest('GET', 'api/v1/containers?teaser=Thi');

        $this->assertEquals(404, $this->httpClient->getResponse()->getStatusCode());
    }
}

What do you think? Any constructive criticism is always welcomed.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *