How to rollback transactions when doing functional testing with Symfony2

前端 未结 3 710
深忆病人
深忆病人 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');
    }
    
    0 讨论(0)
  • I would recommend to use this bundle: https://packagist.org/packages/dama/doctrine-test-bundle

    Its very easy to setup and will roll-back any database changes automatically after every single testcase. No need to implement any custom things.

    0 讨论(0)
  • 2021-02-09 23:52

    You could use a dedication test database for testing

    Another option is using Codeception. This is a unit testing bundle that works with Symfony. If you use this, you can configure it to use a test database and then "clean it up" after each testing cycle.
    An example yaml configuartion to do that would be something like, it is the cleanup: true that does what you want;

    class_name: UnitTester
    modules:
        enabled:
            - Asserts
            - Symfony2:
                app_path: '../../app'
                var_path: '../../app'
                environment: 'test'
            - Doctrine2:
                depends: Symfony2
                cleanup: true
            - \AppBundle\Helper\Unit
    
    0 讨论(0)
提交回复
热议问题