Menu

Sponsored By: Password Angel - Share passwords, API keys, credentials and more with secure, single-use links.

Fixing: ServiceNotFoundException - service or alias has been removed or inlined when the container was compiled

When writing unit tests for App\Helper\MyHelper I received the following error

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException : The "App\Helper\MyHelper" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

This resulted from having created a service and trying to load it during a KernelTestCase, however, as I had not used the service within my code anywhere yet, Symfony removed the service from the container, hence the error above.

My KernelTestCase looked like something like this


<?php

declare(strict_types=1);

namespace Tests\App\Helper;

use App\Helper\MyHelper;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class MyHelperTest extends KernelTestCase
{
    public function testResolve(): void
    {
        $helper = static::getContainer()->get(MyHelper::class);

        $outcome = $helper->resolve(...);

        static::assertEquals('... expected ...', $outcome);
    }
}

The resolution for this was to use the helper class in my code, so I used dependency injection to get the helper class into the service that needed it. The kernel test case then passed successfully as the helper was no longer being removed from the container.

Enjoyed this article?

Thank you for reading this article! If you found the information valuable and enjoyed your time here, consider supporting my work by buying me a coffee. Your contribution helps fuel more content like this. Cheers to shared knowledge and caffeine-fueled inspiration!

Buy me a coffee

Originally published at https://chrisshennan.com/blog/fixing-servicenotfoundexception-service-or-alias-has-been-removed-or-inlined-when-the-container-was-compiled

Subscribe to my newsletter...

... and receive the musings of an aspiring #indiehacker directly to your inbox once a month.

These musings will encompass a range of subjects such as Web Development, DevOps, Startups, Bootstrapping, #buildinpublic, SEO, personal opinions, and experiences.

I won't send you spam and you can unsubscribe at any time.