Best way to create a test database and load fixtures on Symfony 2 WebTestCase?

后端 未结 7 1442
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 13:11

I have a WebTestCase that executes some basic routes in my application.

I want to, on the setUp method of PHPUnit, create a test database identical to m

相关标签:
7条回答
  • 2020-12-04 13:47

    Just recently the bundle hautelook/AliceBundle expose two traits to help you solve the use case of loading fixtures in functional tests: RefreshDatabaseTrait and ReloadDatabaseTrait.

    From the doc:

    namespace App\Tests;
    
    use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class NewsTest extends WebTestCase
    {
        use RefreshDatabaseTrait;
    
        public function postCommentTest()
        {
            $client = static::createClient(); // The transaction starts just after the boot of the Symfony kernel
            $crawler = $client->request('GET', '/my-news');
            $form = $crawler->filter('#post-comment')->form(['new-comment' => 'Symfony is so cool!']);
            $client->submit($form);
            // At the end of this test, the transaction will be rolled back (even if the test fails)
        }
    }
    

    And you are good !

    0 讨论(0)
提交回复
热议问题