如何从 Symfony 5 中的功能测试类访问 EntityManager?

时间:2021-06-16 14:40:59

标签: symfony dependency-injection phpunit functional-testing symfony5

我正在开发 Symfony 5 应用程序。在功能测试 (WebTestCase) 中,我想检查请求的数据库结果。为此,我需要访问 / 一个 EntityManger。所以我像这样尝试(按照 Symfony 测试教程的 "Functional Testing of A Doctrine Repository" 部分的建议):

AbstractWebTestCase

abstract class AbstractWebTestCase extends WebTestCase
{
    private $entityManager;

    public function getEntityManager()
    {
        return $this->entityManager;
    }

    // a convenience method for creating an authenticated user
    protected function createAuthenticatedUser($username, $password)
    {
        $client = static::createClient();
        $client->request('POST', '/api/login', [], [], ['CONTENT_TYPE' => 'application/json'],
            json_encode(['username' => $username,'password' => $password,]));
        $data = json_decode($client->getResponse()->getContent(), true);
        $client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $data['token']));
        return $client;
    }

    protected function setUp(): void
    {
        parent::setUp();
        $kernel = self::bootKernel();
        $this->entityManager = $kernel->getContainer()->get('doctrine')->getManager();
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->entityManager->close();
        $this->entityManager = null;
    }
}

FooControllerTest

class FooControllerTest extends AbstractWebTestCase
{
    public function testFoo(): void
    {
        $client = $this->createAuthenticatedUser('foo@foo.com', 'pwd');
        $client->request('POST', '/api/uri/foo', [], [], ['CONTENT_TYPE' => 'application/json'], '{...}'
        );
        ...
        $this->assertResponseIsSuccessful();
    }
}

但是现在我的测试遇到了错误:

<块引用>

App\Tests\Controller\MyConcreteControllerTest::testDoSomething LogicException: 不支持在调用“Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()”之前启动内核,内核应该只启动一次。

如何解决此问题并在功能测试中访问 /an EntityManager

1 个答案:

答案 0 :(得分:0)

/A 的解决方案是仅在 static::createClient() 中调用 setUp()

AbstractWebTestCase

abstract class AbstractWebTestCase extends WebTestCase
{
    private $entityManager;
    private $client;

    public function getEntityManager()
    {
        return $this->entityManager;
    }

    // a convenience method for creating an authenticated user
    protected function createAuthenticatedUser($username, $password)
    {
        $this->client->request('POST', '/api/login', [], [], ['CONTENT_TYPE' => 'application/json'],
            json_encode(['username' => $username,'password' => $password,]));
        $data = json_decode($this->client->getResponse()->getContent(), true);
        $this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $data['token']));
        return $this->client;
    }

    protected function setUp(): void
    {
        parent::setUp();
        $this->client = static::createClient();
        $this->entityManager = static::$container->get('doctrine')->getManager();
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        $this->entityManager->close();
        $this->entityManager = null;
    }
}

有更好/更优雅的解决方案吗?

相关问题