1

I am working on a Symfony 3.4 installation that was upgraded from version 2.

When I try to run a unit test with this command

./bin/phpunit tests/MyBundle/ -c tests/phpunit.xml

I get the following error:

    Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "XXXX\MyBundle\Services\ListServices".

/apps/xxxx/unitTest/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:348
/apps/xxxx/unitTest/tests/MyBundle/Services/ListServicesTest.php:19

I have made the Services public

  services:
    _defaults:
      autowire: true
      autoconfigure: true
      public: true

But that did not work. I also tried adding a Compiler pass, but that was it's own can of worms...

my phpunit.xml

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
                   backupGlobals="false"
                   colors="true"
                   convertErrorsToExceptions   = "true"
                   convertNoticesToExceptions  = "true"
                   convertWarningsToExceptions = "true"
                   processIsolation            = "false"
                   stopOnFailure               = "false"
                   syntaxCheck                 = "false"
                   stderr                      = "true"
                   bootstrap="bootstrap_test.php"
>
<php>
    <ini name="error_reporting" value="-1" />
    <server name="KERNEL_CLASS" value="AppKernel" />
    <server name="KERNEL_DIR" value="../app/" />
    <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    <env name="APP_ENV" value="test"/>
</php>


<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../tests/*</directory>
    </testsuite>
</testsuites>

<filter>
    <whitelist>
        <directory>../src</directory>
        <exclude>
            <directory>../src/*/*Bundle/Resources</directory>
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/*Bundle/DataFixtures</directory>
            <directory>../src/*/*Bundle/Admin</directory>
            <directory>../src/*/*Bundle/DependencyInjection</directory>
            <directory>../src/*/*Bundle/Twig</directory>
        </exclude>
    </whitelist>
</filter>
</phpunit>

this is my test file

namespace Tests\MyBundle\Services;

use XXXX\MyBundle\Services\ListServices;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ListServicesTest extends WebTestCase
{
    /**
     * @test
     */    
public function testGetCurrentList()
    {
        $kernel = new AppKernel('dev', false);
        $kernel->boot();
        $container = $kernel->getContainer();

        $listService = $container->get(ListServices::class);

        $this->assertNotNull($listService->getCurrentList());

    }
}

Any ideas on how to get the unit tests to work? thanks

6
  • Where do you import that service? Are you sure the namespace is correct? Commented Mar 1, 2019 at 15:07
  • please see my edit, i added the test code. Yes, the namespace is correct. Commented Mar 1, 2019 at 15:15
  • can you show me how you import that class? can you show the whole test file? Commented Mar 1, 2019 at 15:32
  • Just edited the post to show the complete file. Commented Mar 1, 2019 at 15:41
  • You have this in your error Efgam\FidBundle\Services\ListServices and use XXXX\MyBundle\Services\ListServices this in test? Commented Mar 1, 2019 at 15:44

1 Answer 1

1

You are trying to get the service by it's name (class name) and not by it's alias from the container. Service should be registered in the container:

services:
    your_service_alias:
      class: YourApp\Services\ListServices
      arguments: (if any)
        - "@some_dependency"

Then in your test you should get the service by it's alias

$container->get('your_service_alias')
Sign up to request clarification or add additional context in comments.

1 Comment

That actually solved the problem. It gave me another, but that is progress i guess ;) thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.