【问题标题】:Symfony: refresh test database for phpunit testing with autoincrements reset on fixtures addedSymfony:刷新测试数据库以进行 phpunit 测试,并在添加的夹具上重置自动增量
【发布时间】:2020-06-06 16:27:58
【问题描述】:

我想在每次运行 bin/phpunit 时创建一个新的内存 sqlite 测试数据库来运行我的测试。到目前为止我发现的解决方案清空了测试数据库,但是当我添加夹具时,它们的 ID 从擦除前的最后一个自动增量开始。

【问题讨论】:

    标签: sqlite symfony phpunit fixtures


    【解决方案1】:

    对于面临这个问题的人,我发布了一个我从这里 (https://www.sitepoint.com/quick-tip-testing-symfony-apps-with-a-disposable-database/) 开始想出的解决方案,并进行了一些调查和反复试验:

    教义会议

    #api/config/packages/test/doctrine.yaml
    doctrine:
        dbal:
            driver: 'pdo_sqlite'
            memory:  true
            charset: UTF8
    

    WebTestCaseWithDatabase

    <?php
    
    namespace App\Tests;
    
    use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
    use Doctrine\Common\DataFixtures\Loader;
    use Doctrine\Common\DataFixtures\Purger\ORMPurger;
    use Doctrine\ORM\EntityManager;
    use Doctrine\ORM\Tools\SchemaTool;
    use Metadata\ClassMetadata;
    use Symfony\Bundle\FrameworkBundle\KernelBrowser;
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class WebTestCaseWithDatabase extends WebTestCase
    {
        /**
         * @var KernelBrowser
         */
        protected $client;
    
        /**
         * @var EntityManager
         */
        protected $em;
    
        /**
         * @var SchemaTool
         */
        protected $schemaTool;
    
        /**
         * @var ClassMetadata[]
         */
        protected $metaData;
    
    
        protected function setUp()
        {
            parent::setUp();
    
            // This is tricky. You need to boot the kernel to create the DDBB.                   
            // But if you boot it with static::bootKernel(),                        
            // an error will be thrown if you create the client in your tests, 
            // because static::createClient() tries boot again the kernel.
            // That's why I create the client and boot the kernel only once here.
            $this->client = static::createClient();
    
            // Make sure we are in the test environment
            if ('test' !== self::$kernel->getEnvironment()) {
                throw new \LogicException('Tests cases with fresh database must be executed in the test environment');
            }
            // Get the entity manager from the service container
            $this->em = self::$kernel->getContainer()->get('doctrine')->getManager();
    
            // Run the schema update tool using our entity metadata
            $this->metaData = $this->em->getMetadataFactory()->getAllMetadata();
            $this->schemaTool = new SchemaTool($this->em);
            $this->schemaTool->updateSchema($this->metaData);
        }
    
        // Helper function to add fixtures
        public function addFixture($className)
        {
            $loader = new Loader();
            $loader->addFixture(new $className);
    
            $purger = new ORMPurger($this->em);
            $executor = new ORMExecutor($this->em, $purger);
            $executor->execute($loader->getFixtures());
        }
    
        // Trunkate the whole database on tearDown
        protected function tearDown(): void
        {
            parent::tearDown();
    
            // Purge all the fixtures data when the tests are finished
            $purger = new ORMPurger($this->em);
            // Purger mode 2 truncates, resetting autoincrements
            $purger->setPurgeMode(2);
            $purger->purge();
        }
    }
    

    示例测试

    <?php
    
    namespace App\Tests;
    
    use App\DataFixtures\ExampleFixture;
    
    class ExampleTest extends WebTestCaseWithDatabase
    {
        protected function setUp()
        {
            parent::setUp();
    
            // Add whatever fixtures you need here
            $this->addFixture(ExampleFixture::class);
        }
    
        // Add your tests 
        public function test_something()
        {
            // test code
        }
    }
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      如果您正在寻找可以为您执行此操作的软件包,dmaicher/doctrine-test-bundle 可以自动执行此操作。每个测试都将针对一个新的数据库运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-25
        • 2011-10-06
        • 2014-08-02
        • 1970-01-01
        • 2013-02-14
        • 2011-06-02
        • 2016-03-24
        相关资源
        最近更新 更多