【问题标题】:Symfony 4 unit test Detached entity cannot be removedSymfony 4单元测试无法删除分离的实体
【发布时间】:2018-06-22 19:38:48
【问题描述】:

我在使用新版本的 symfony symfony 4 时遇到了一些问题。

我正在尝试测试一个 rest api,但我遇到了这个错误。

Doctrine\ORM\ORMInvalidArgumentException:无法删除分离的实体 App\Entity\User@000000005b034b8800000000320e4469

在我的单元测试中,我创建了一个用户,然后在拆卸时我想清理数据库。

单元测试:

namespace App\Controller;

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class UserControllerTest extends WebTestCase
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * {@inheritDoc}
     */
    protected function setUp()
    {
        $kernel = self::bootKernel();

        $this->em = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

    protected function tearDown()
    {
        parent::tearDown();
        $users = $this->em->getRepository(User::class)->findAll();
        foreach ($users as $user) {
            $this->em->remove($user);
            $this->em->flush($users);
        }
        $this->em->close();
        $this->em = null; // avoid memory leaks
    }

    public function testGetAllEmpty()
    {
        $client = static::createClient();
        $client->request('GET', '/users');
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertEquals([], json_decode($client->getResponse()->getContent()));
    }

    public function testGetAllNotEmpty()
    {
        $this->createUser();
        $client = static::createClient();
        $client->request('GET', '/users');
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $content = json_decode($client->getResponse()->getContent(), true);
//        $this->assertCount(1, $content);
//        var_dump($content);
//        $this->assertCount(1, $content["pseudo"]);
    }

    public function createUser()
    {
        $user = new User();
        $user->setPseudo('mitsi');
        $user->setPassword('dev');
        $this->em->persist($user);
        $this->em->flush();
        return $user;
    }
}

控制器

<?php

namespace App\Controller;


use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request;

class UserController extends Controller {
    public function getAll()
    {
        return new JsonResponse($this->getDoctrine()->getRepository(User::class)->findAll());
    }

    public function get($id)
    {

    }

    public function add(Request $request)
    {

    }

    public function edit($id)
    {

    }

    public function delete($id)
    {

    } 
}

用户

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $pseudo;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $password;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getPseudo()
    {
        return $this->pseudo;
    }

    /**
     * @param mixed $pseudo
     */
    public function setPseudo($pseudo): void
    {
        $this->pseudo = $pseudo;
    }

    /**
     * @return mixed
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @param mixed $password
     */
    public function setPassword($password): void
    {
        $this->password = $password;
    }
}

有什么问题?

谢谢。

【问题讨论】:

  • 无法将此标记为重复,因为没有可接受的答案,但这可能有用:stackoverflow.com/questions/38073802/…
  • 在删除实体之前调用合并或在静态(自我)中设置字段都没有解决问题。其他想法?

标签: php symfony unit-testing doctrine-orm


【解决方案1】:

好的,所以我已经实现了其他测试......现在它可以工作了!

【讨论】:

  • 如果您认为这是对您正在搜索的问题的认可答案,您会满意吗?它会帮助你对任何东西进行排序吗?
  • 你的解决方案是什么?
猜你喜欢
  • 2016-10-30
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
相关资源
最近更新 更多