如何在Symfony 5.4中的Phpunit testcase中访问EntityManager?

发布于 2025-01-31 14:49:46 字数 1863 浏览 3 评论 0 原文

在Symfony 5.4上,我正在对REST API进行测试。 当我尝试访问实体管理器时,我总是会遇到错误,

我遵循此文档:

这是我的测试代码:

<?php
namespace App\Tests;

use App\Repository\LocationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class DataTest extends WebTestCase     // WebTestCase  (not KernelTestCase)
{
    private EntityManagerInterface $entityManager;
    private LocationRepository $locationRepository;

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

    protected function tearDown(): void
    {
        parent::tearDown();

        // doing this is recommended to avoid memory leaks
        $this->entityManager->close();
        $this->entityManager = null;
    }

    public function test401(): void
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/api/locations');
        $response = $client->getResponse();
        $this->assertResponseStatusCodeSame(401);
    }
...

我得到此错误:

Testing App\Tests\DataTest
EEEE                                                                4 / 4 (100%)

There were 4 errors:

1) App\Tests\DataTest::test401
Error: Call to undefined method ContainerDyLAo2g\EntityManager_9a5be93::get()

如何解决此问题?

on symfony 5.4, I'm testing on a rest api.
I always get errors when I try to access the entity manager

I followed this documentation:
https://symfony.com/doc/current/testing/database.html#functional-testing-of-a-doctrine-repository

here is my test code:

<?php
namespace App\Tests;

use App\Repository\LocationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class DataTest extends WebTestCase     // WebTestCase  (not KernelTestCase)
{
    private EntityManagerInterface $entityManager;
    private LocationRepository $locationRepository;

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

    protected function tearDown(): void
    {
        parent::tearDown();

        // doing this is recommended to avoid memory leaks
        $this->entityManager->close();
        $this->entityManager = null;
    }

    public function test401(): void
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/api/locations');
        $response = $client->getResponse();
        $this->assertResponseStatusCodeSame(401);
    }
...

I get this error:

Testing App\Tests\DataTest
EEEE                                                                4 / 4 (100%)

There were 4 errors:

1) App\Tests\DataTest::test401
Error: Call to undefined method ContainerDyLAo2g\EntityManager_9a5be93::get()

How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2025-02-07 14:49:46

WebTestcase 中,您可以这样访问Entity Manager,

$this->entityManager = static::getContainer()->get(EntityManagerInterface::class)

您也可以在此处查看文档 https://symfony.com/doc/current/testing.html#retrieving-services-in-the-the-test

In WebTestCase you can access the entity manager like this

$this->entityManager = static::getContainer()->get(EntityManagerInterface::class)

You can also check the documentation here https://symfony.com/doc/current/testing.html#retrieving-services-in-the-test

柠檬色的秋千 2025-02-07 14:49:46

EntityManagerInterface上没有方法 get()。使用方法 getRepository(string $ className)获取存储库:

$this->locationRepository = $this->entityManager>getRepository(LocationEntity::class);

There is no method get() on the EntityManagerInterface. Use the method getRepository(string $className) to fetch the repository:

$this->locationRepository = $this->entityManager>getRepository(LocationEntity::class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文