定制 hgraca/doctrine-test-db-regeneration-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hgraca/doctrine-test-db-regeneration-bundle

最新稳定版本:v1.1.2

Composer 安装命令:

composer require hgraca/doctrine-test-db-regeneration-bundle

包简介

Symfony 2/3/4 bundle to generate the test DB (if needed) for every test suite run or test method run.

README 文档

README

Author Software License Latest Version Total Downloads

Build Status Scrutinizer Code Quality Code Intelligence Status CodeCov

This is a Symfony 2/3/4 bundle, inspired on some code @lcobucci wrote back in 2017, to generate the test DB (if needed) for every test suite run or test method run.

When we have Integration tests that need to use a DB, the best practice is to have the tests use a SQLite DB for those tests. However, keeping the SQLite DB file in our repo is not a good practice, and using the same file for all tests neither, because tests should run in isolation from each other.

This bundle provides the functionality to generate the test DB SQLite file, and regenerate it for every test that needs a DB.

By default, at the beginning of a test run it will create a test database, based on the Doctrine fixtures, and a serialized fixtures object.

After generating those initial files, it will copy the backup DB SQLite file into the path of the DB SQLite file being used by the tests, but only before the tests implementing the tag interface DatabaseAwareTestInterface so that we don't regenerate the DB for tests that don't need it.

This last functionality, however, can be disabled so that we can use this bundle with another bundle like the DAMA\DoctrineTestBundle, which provides similar functionality which will probably give you better performance. In such case, this bundle is only useful for generating the initial test DB.

Installation & Setup

  1. install via composer

    composer require --dev hgraca/doctrine-test-db-regeneration-bundle
  2. Enable the bundle for your test environment in your AppKernel.php, ie:

    if (in_array($env, ['dev', 'test'])) {
        ...
        if ($env === 'test') {
            $bundles[] = new Hgraca\DoctrineTestDbRegenerationBundle\HgracaDoctrineTestDbRegenerationBundle();
        }
    }
  3. The bundle exposes a configuration that looks like this by default:

    hgraca_doctrine_test_db_regeneration:
        doctrine_service_id: 'doctrine'
        fixtures_loader_service_id: 'doctrine.fixtures.loader'
        test_db_bkp_path: '%kernel.cache_dir%/test.bkp.db'
        # The list of extra services to add to the container, 
        # in case you want to reuse this already built container.
        extra_service_list: [] 
  4. Add the PHPUnit test listener in your xml config (e.g. app/phpunit.xml)

    <phpunit>
        ...
    <listeners>
        <!-- At the beginning of every test run, it will generate the test DB and create a backup of it and 
             a backup of the fixtures references. At the beginning of every test it will recover the test DB backup. -->
        <listener class="\Hgraca\DoctrineTestDbRegenerationBundle\EventSubscriber\DbRegenerationPHPUnitEventSubscriber" />
    </listeners>
    </phpunit>

    There are a few options you can use to tweak how it works. Below you can see the options with their default values:

    <phpunit>
        ...
    <listeners>
        <listener class="\Hgraca\DoctrineTestDbRegenerationBundle\EventSubscriber\DbRegenerationPHPUnitEventSubscriber">
            <arguments>
                <integer>1</integer> <!-- $shouldRemoveDbAfterEveryTest -->
                <integer>1</integer> <!-- $shouldRegenerateOnEveryTest -->
                <integer>0</integer> <!-- $shouldReuseExistingDbBkp -->
            </arguments>
        </listener>
    </listeners>
    </phpunit>

    I strongly advise using DAMA\DoctrineTestBundle as well, as in a platform with 1596 integration tests, it reduced the test execution time from 2m to 46s.

    If you are using DAMA\DoctrineTestBundle you will want to set it up like this:

    <phpunit>
        ...
    <listeners>
        <!-- Since we are using DAMA\DoctrineTestBundle we don't need to recover the test DB at 
             every test run, so we turn it off. However, this listener must go first, so it 
             creates the test DB before DAMA\DoctrineTestBundle tries to use it. -->
        <listener class="\Hgraca\DoctrineTestDbRegenerationBundle\EventSubscriber\DbRegenerationPHPUnitEventSubscriber">
            <arguments>
                <integer>0</integer> <!-- $shouldRemoveDbAfterEveryTest -->
                <integer>0</integer> <!-- $shouldRegenerateOnEveryTest -->
            </arguments>
        </listener>
        <!-- it begins a database transaction before every testcase and rolls it back after
             the test finished, so tests can manipulate the database without affecting other tests -->
        <listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
    </listeners>
    </phpunit>
  5. Make your tests, that need DB access, implement the tag interface DatabaseAwareTestInterface, so it only recovers the DB for those tests.

  6. Done! From now on the test database will be generated (if needed) at the beginning of running the tests, and at the beginning of every test tagged as DatabaseAwareTestInterface, if you did not disable this feature.

Available commands

Run the test suites:

make test

Create th test coverage report:

make coverage

Fix the code standards:

make cs-fix

Manage the project dependencies:

make dep-install
make dep-update

Build the docker container used for testing:

make build-container-tst

hgraca/doctrine-test-db-regeneration-bundle 适用场景与选型建议

hgraca/doctrine-test-db-regeneration-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.81k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hgraca/doctrine-test-db-regeneration-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hgraca/doctrine-test-db-regeneration-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 hgraca/doctrine-test-db-regeneration-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-05