如何从 Symfony 5 中的功能测试 class 访问 EntityManager?
How to access the EntityManager from a functional test class in Symfony 5?
我正在开发 Symfony 5 应用程序。在功能测试 (WebTestCase
) 中,我想检查请求的数据库结果。为此,我需要访问 / an 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: Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()" is not supported, the kernel should only be booted once.
如何解决这个问题并在功能测试中获得对 EntityManager
的访问权限?
/A解决方法是只在setUp()
中调用static::createClient()
:
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;
}
}
是否有更好/更优雅的解决方案?
我正在开发 Symfony 5 应用程序。在功能测试 (WebTestCase
) 中,我想检查请求的数据库结果。为此,我需要访问 / an 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: Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient()" is not supported, the kernel should only be booted once.
如何解决这个问题并在功能测试中获得对 EntityManager
的访问权限?
/A解决方法是只在setUp()
中调用static::createClient()
:
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;
}
}
是否有更好/更优雅的解决方案?