Php 如何在Symfony测试中使用实体管理器刷新实体?

Php 如何在Symfony测试中使用实体管理器刷新实体?,php,symfony,testing,doctrine-orm,phpunit,Php,Symfony,Testing,Doctrine Orm,Phpunit,我正在我的网站上测试一个表单,但是我遇到了这个问题,它主要是由实体经理引起的 这是错误 IndexBundle\Tests\Controller\CompanyControllerTest::testCompany 条令\ORM\ORMInvalidArgumentException:Entity IndexBundle\Entity\User@000000007e2ef25700000000462028ec不受管理。如果实体从数据库中获取或通过EntityManager注册为新实体,则该实体将

我正在我的网站上测试一个表单,但是我遇到了这个问题,它主要是由实体经理引起的

这是错误

  • IndexBundle\Tests\Controller\CompanyControllerTest::testCompany 条令\ORM\ORMInvalidArgumentException:Entity IndexBundle\Entity\User@000000007e2ef25700000000462028ec不受管理。如果实体从数据库中获取或通过EntityManager注册为新实体,则该实体将被管理
  • 这是我的测试

     <?php
    
    
    class CompanyControllerTest extends \DataFixturesTestCase
    {
    
    
        /**
         * @var Client|null
         */
        protected $client = null;
        /**
         * @var $session
         */
        protected $session;
    
        /**
         * {@inheritDoc}
         */
        public function setUp(): void
        {
            parent::setUp();
            $this->client = static::createClient(array(
                'PHP_AUTH_USER' => 'company@example.com',
                'PHP_AUTH_PW' => 'test',
            ));
    
        }
    
        public function testCompany()
        {
            $this->logIn();
            $this->testProfile();
    
    
        }
        protected function testProfile(){
            $userTest = $this->entityManager->getRepository(User::class)->findOneBy(['username'=>'company@example.com']);
    
            $crawler = $this->client->request('GET', '/company/profile?edit=user&lang=en');
    
            $formNode= $crawler->selectButton('Continue »');
            $form= $formNode->form(array(
                'user[title]'=> 'Mr',
                'user[name]'=> 'the boss',
                'user[surnames]'=> 'the boss surname',
                'user[birthdate][day]'=> 1,
                'user[birthdate][month]'=> 3,
                'user[birthdate][year]'=> 1990,
                'user[birthPlace]'=> 'valencia',
              
            ));
            $this->client->submit($form);
            $this->entityManager->refresh($userTest);
            $this->assertSame('newEmail@email.com', $userTest->getContactEmail()) ;
    
        }
    
        private function logIn()
        {
            $session = $this->client->getContainer()->get('session');
    
            $firewall = 'secured_area';
    
            $token = new UsernamePasswordToken('company', 'null', $firewall, array('ROLE_COMPANY_SUPER_ADMIN'));
            $session->set('_security_'.$firewall, serialize($token));
            $session->save();
    
            $cookie = new Cookie($session->getName(), $session->getId());
            $this->client->getCookieJar()->set($cookie);
        }
    }
    

    您是否尝试使用DI从容器获取存储库或注入测试用例?似乎是从实体管理器获取存储库的问题。可能很有用,抱歉反应晚了。非常感谢,但它对我不起作用。替换
    $this->entityManager->refresh($userTest)怎么样使用与测试开始时相同的查询
    $userTest=$this->entityManager->getRepository(User::class)->findOneBy(['username'=>'company@example.com']);?但是你需要使用ID而不是电子邮件。