Symfony,doctrine WebTestCase中的工作单元未定义索引

I want to test my action in the controller for uploading a file. However the file needs a "farm" which is a parent entity.

So I am creating the farm with all subentities before calling the request in WebTestCase.

private function addFarm()
{
    $farm = new Farm();
    $farm->setName('test farm');
    $farm->setDescription('test');
    $farm->setContactName('test');
    $farm->setActive(1);
    $farm->setPhone('123');
    $farm->setFax('123');

    $path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'farmlogo.jpg';

    $image = imagecreate(100, 100);
    imagejpeg($image, $path);

    $farm->setLogo($path);

    $country = new Country($this->uniqueTestValue('country test'), $this->uniqueTestValue('test'));

    $state = new State($this->uniqueTestValue('state test'), $this->uniqueTestValue('test'), $country);

    $city = new City($this->uniqueTestValue('test city'), $state);

    $this->emSave($country);
    $this->emSave($state);
    $this->emSave($city);

    $coordinates = new Coordinates(10, 10);
    $location = new Location($city, $coordinates);

    $address = new Address(
        $this->uniqueTestValue('test'),
        $this->uniqueTestValue('test'),
        $this->uniqueTestValue('zip'),
        $location
    );

    $addressEntity = new AddressEntity(
        $this->client->getContainer()->get('location'),
        $address
    );

    $addressEntity->getLocation()->setCity($city);

    $farm->setAddress($addressEntity);

    $farmManagement = new FarmManagement();
    $farmManagement->setFarm($farm->getId());
    $farmManagement->setUser($this->getUser());
    $this->emSave($farmManagement);

    $this->em->flush();

    return $farm;
}

After calling flush (which I need to get ID of the new farm) I get:

1) [mybundlepath]\Tests\Controller\FarmManageControllerTest::testUploadPhotoAction
Undefined index: 000000002acb549d0000000036bcbc2f

/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2865
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:665
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:714
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:269
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:952
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:335
/var/www/grocery/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:389
/var/www/grocery/src/[mybundlepath]Tests/Controller/FarmManageControllerTest.php:119
[mybundlepath]/Tests/Controller/FarmManageControllerTest.php:164
/usr/share/php/PHPUnit/TextUI/Command.php:192
/usr/share/php/PHPUnit/TextUI/Command.php:130

I have been struggling with it all day, debugging doctrine source code. It somehow looks for City entity proxy spl_object_hash() but it can't be found.

What am I doing wrong? I just want to store the entity in database and get ID.

The emSave() method does only entityManager->persist($entity)