How to rollback transactions when doing functional testing with Symfony2

前端 未结 3 709
深忆病人
深忆病人 2021-02-09 23:08

I\'m trying to write a functional test for my project in Symfony2. I\'d like to test if a user can access a page, fill up a form and submit it. I\'m trying to find a way to roll

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-09 23:35

    Are you doing more than one request with the Client? If so, you problem might be that the client shutdowns the kernel after one request was performed. But you can disable that with $this->client->disableReboot() So this snippet snippet should be idempotent:

    public function setUp()
    {
        $this->client = $this->createClient(['environment' => 'test']);
        $this->client->disableReboot();
        $this->em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
        $this->em->beginTransaction();
    }
    
    public function tearDown()
    {
        $this->em->rollback();
    }
    
    public function testCreateNewEntity()
    {
        $this->client->request('GET', '/create/entity/form');
        $this->client->request('POST', '/create/entity/unique/123');
    }
    

提交回复
热议问题