php - 如何在 Symfony 测试中使用实体管理器刷新实体?
问题描述
我正在我的网站上测试一个表单,但我遇到了这个问题,这主要是由实体管理器引起的。
这是错误
- IndexBundle\Tests\Controller\CompanyControllerTest::testCompany Doctrine\ORM\ORMInvalidArgumentException: Entity IndexBundle\Entity\User@000000007e2ef25700000000462028ec 不受管理。如果实体从数据库中获取或通过 EntityManager#persist 注册为新实体,则该实体被管理
这是我的测试
<?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);
}
}
解决方案
推荐阅读
- spring-rabbit - 与 Java 1.6 兼容的 spring-rabbit 示例
- hibernate - 从多对多关系中检索的 HQL 语法
- javascript - not able to target required data with jquery
- php - Yii2 Swift_SmtpTransport gmail 不工作
- mysql - 使用 MySQL 中的某些 JSON 列加载查询得到 {Invalid JSON text: "Invalid value." 在列 } 值的位置 0
- jquery - 在执行单击操作之前,jquery 脚本不起作用
- angular - 标头未发送 angular-5
- java - Java Map 的 RAML 1.0 表示
- ios - 数据验证完成后如何移动到第二个 ViewController
- firebase - 使用 Firebase 中的多个值检索 id