Symfony 4单元测试分离的实体无法删除

时间:2018-01-13 15:58:50

标签: php symfony unit-testing doctrine-orm

我对symfony的新版本有一些麻烦,symfony 4。

我正在尝试测试一个rest api,但是我有这个错误。

  

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

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

单元测试:     

with value_t as
( 
  select row_t,row_number() OVER (partition by row_t order by rownum )rn,
  regexp_substr(val, '[^,]+', 1, LEVEL) val from Table1
CONNECT BY LEVEL <= regexp_count(val, '[^,]+') 
AND prior row_t = row_t 
AND prior sys_guid() is not null
  ) select row_t, max( case when rn = 1 THEN val end ) val_1,
  max( case when rn = 2 THEN val end ) val_2,
  max( case when rn = 3 THEN val end ) val_3
  from value_t
  group by row_t;

控制器

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)
    {

    } 
}

有什么问题?

谢谢。

1 个答案:

答案 0 :(得分:-2)

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