Unable to get service form DI container, service not found

Notice: This thread is very old.
RichardT
Member | 43
+
0
-

I am trying to extract service form service container and pass it as dependency to my uut, but I am obtaining error “Service not found”. What am I missing?

Nette\DI\MissingServiceException: Service 'Doctrine\ORM\EntityManagerInterface' not found.

   in src/DI/Container.php(154)
   in src/DI/Container.php(105) Nette\DI\Container->createService()
   in document_root/tests/MyServiceTest.phpt(33) Nette\DI\Container->getService()
   in src/Framework/TestCase.php(128) MyApp\MyService->setUp()
   in src/Framework/TestCase.php(113) Tester\TestCase->runTest()
   in src/Framework/TestCase.php(51) Tester\TestCase->runMethod()
   in document_root/tests/MyServiceTest.phpt(45) Tester\TestCase->run()

Here are simplified relevant files

/app/models/MyService.php

<?php

namespace MyApp;

use Doctrine\ORM\EntityManagerInterface;
use Kdyby\Doctrine\EntityManager;

class MyService
{
    /**
     * @var EntityManagerInterface
     */
    protected $entityManager;

    public function __construct(
        EntityManager $entityManager
    ) {
        $this->entityManager = $entityManager;
    }
}

/tests/MyServiceTest.phpt

<?php

/**
 * @TestCase
 */
namespace MyApp;

use Doctrine\ORM\EntityManagerInterface;
use Nette;
use Tester;
use Tester\Assert;

$container = require __DIR__ . '/bootstrap.php';


class MyServiceTest extends Tester\TestCase
{
    /** @var Nette\DI\Container */
    private $container;

    /** @var MyService */
    private $uut;

    public function __construct(Nette\DI\Container $container)
    {
        $this->container = $container;
    }

    public function setUp()
    {
        /* @var EntityManagerInterface $entityManager */
        $entityManager = $this->container->getService(MyService::class);
        $this->uut = new MyService($entityManager);
    }

    public function testExample()
    {
        Assert::true(true);
    }

}

$test = new MyApp($container);
$test->run();

/tests/bootstrap.php

<?php

require __DIR__ . '/../vendor/autoload.php';

if (!class_exists('Tester\Assert')) {
	echo "Install Nette Tester using `composer update --dev`\n";
	exit(1);
}

Tester\Environment::setup();

$configurator = new Nette\Configurator;

$configurator->addParameters(array(
    'appDir' => __DIR__ . '/../app',
));

$configurator->setDebugMode(FALSE);
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->createRobotLoader()
	->addDirectory(__DIR__ . '/../app')
	->register();

$configurator->addConfig(__DIR__ . '/../app/config/config.neon');
$configurator->addConfig(__DIR__ . '/../app/config/config.test.neon');
$configurator->addConfig(__DIR__ . '/../app/config/services.neon');

return $configurator->createContainer();

/app/config/config.neon

#
# WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
# https://nette.org/en/security-warning
#
php:
    date.timezone: Europe/Prague

application:
    errorPresenter: Error
    mapping:
        *: App\*Module\Presenters\*Presenter

session:
    expiration: 14 days

services:
    - App\Model\UserManager
    - App\Forms\SignFormFactory
    router: App\RouterFactory::createRouter

extensions:
    doctrine: Kdyby\Doctrine\DI\OrmExtension
    fixtures: Zenify\DoctrineFixtures\DI\FixturesExtension
    console: Kdyby\Console\DI\ConsoleExtension
    events: Kdyby\Events\DI\EventsExtension
    annotations: Kdyby\Annotations\DI\AnnotationsExtension

fixtures:
    alice:
        locale: "en_US"

/app/config/config.test.neon

database:
    dsn: 'sqlite::memory:'

doctrine:
    driver: pdo_sqlite
    metadata:
        Entity: %appDir%/model/Entity

/app/config/services.neon

services:
    - MyApp\MyService
RichardT
Member | 43
+
0
-

Got it,

just need to use $this->container->getByType() instead of $this->container->getService()