2
votes

I've some trouble with new version of symfony, symfony 4.

I'm trying to test a rest api but I have this error.

Doctrine\ORM\ORMInvalidArgumentException: Detached entity App\Entity\User@000000005b034b8800000000320e4469 cannot be removed

In my unit test, I create a user, then in the tear down I want to clean the database.

Unit Tests:

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;
    }
}

The Controller

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

    } 
}

The User

<?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;
    }
}

What is the problem ?

Thank you.

1
Can't mark this as a duplicate because there's no accepted answer, but this might be useful: stackoverflow.com/questions/38073802/…iainn
Neither call merge before removing the entity nor set the field in static (self) solved the issue. Others ideas ?MitsiDev

1 Answers

-13
votes

Okay, so I've implemented the others tests... and now it works !