Jon
Jon

Reputation: 784

Phpunit on Symfony 3.4 You have requested a non-existent service

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

Upvotes: 1

Views: 1775

Answers (1)

Kamil Ścisłowski
Kamil Ścisłowski

Reputation: 124

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')

Upvotes: 1

Related Questions