Php 将原则注入测试-Symfony 4.1

Php 将原则注入测试-Symfony 4.1,php,symfony,testing,Php,Symfony,Testing,在Symfony 3.4中,我使用此方法在测试类中获取条令实体: 测试类(代码片段): $kernel = self::bootKernel(); $em = $kernel->getContainer() ->get('doctrine') ->getManager(); 这是不赞成的 我正试图将实体管理器注入到我的测试中,如下所示: services.yml Tests\AppBundle\NewUserTest:

在Symfony 3.4中,我使用此方法在测试类中获取条令实体:

测试类(代码片段):

    $kernel = self::bootKernel();

    $em = $kernel->getContainer()
        ->get('doctrine')
        ->getManager();
这是不赞成的

我正试图将实体管理器注入到我的测试中,如下所示:

services.yml

Tests\AppBundle\NewUserTest:
    public: true
    autowire: true
    calls:
        - [ setEntityManager, ['@doctrine.orm.entity_manager']]
测试类(snippet,namespace=“Tests\AppBundle”):

我无法使用构造函数注入进行注入(因为WebTestCases需要一组我无法访问的构造函数参数)

有人能帮我解决这个问题吗?我一直在到处找这个问题,没有解决办法。有一些类似的问题,但不是在测试环境中


谢谢:)

我想你可以为它创建客户端。客户拥有您需要的所有服务

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class WebTest extends WebTestCase
{
    private $client;

    public function setUp()
    {
        $this->client = static::createClient();
    }

    public function getLoggedInClient()
    {
        $session = $this->client->getContainer()->get('session');
    }

}

测试不是服务,所以我怀疑您的第二种方法是否有效。你收到的折旧通知是什么?这似乎表明第一种方法仍然有效。通常情况下,文档会在出现问题时发出警告。@Cerad在Symfony 3.4中我得到了这样的信息:“58x:将“false”作为第二个参数传递给Symfony\Component\HttpKernel\Kernel::getBundle(),从3.4开始就不推荐使用,并将在4.0中删除。”我假设您是从以前的Symfony版本升级的?通知有点暗示您仍然有一个第三方包,它还没有完全准备好3.4。或者你有一个旧版本的phpunit。
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class WebTest extends WebTestCase
{
    private $client;

    public function setUp()
    {
        $this->client = static::createClient();
    }

    public function getLoggedInClient()
    {
        $session = $this->client->getContainer()->get('session');
    }

}