定制 edmondscommerce/behat-db-context 二次开发

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

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

edmondscommerce/behat-db-context

Composer 安装命令:

composer require --dev edmondscommerce/behat-db-context

包简介

A Behat Context for managing the testing database

README 文档

README

By Edmonds Commerce

A Behat context for managing your testing database.

Installation

Install via composer

"edmondscommerce/behat-db-context": "dev-master@dev"

Include Context in Behat Configuration

default:
    # ...
    suites:
        default:
        # ...
            contexts:
                - # ...
                - EdmondsCommerce\BehatDbContext\DbContext
            parameters:
              databaseSettings:
                importTestingDatabase: true
                databaseName: some_testing_db
                pathToSqlDump: '../some_testing_db.sql'
                customAssertions:
                  # Must be wrapped in double quotes
                  - "SELECT COUNT(*) FROM some_table WHERE some_thing = some_value"
                  - "SELECT COUNT(*) FROM some_table WHERE another_thing = another_value"

Features

Ensures You're Using the Testing Database

The context employs platform detection to ensure that your application is currently using the test database you configured in behat.yml using databaseName.

If you're not using the correct database then you'll receive an exception and the tests won't proceed.

To add support for you're platform you can follow the steps detailed below in the 'Adding Support For New Platforms' section.

Import Fresh Testing Database

The context imports a fresh version of the testing database each time you run your test suite. It imports the SQL dump configured in your behat.yml using pathToSqlDump and imports this into the database databaseName.

NOTE: this context assumes you have your MYSQL credentials configured in .my.cnf.

Optional Testing Database Import

When importTestingDatabase is set to false the import step will be skipped. This is useful while working on the tests locally but should always be set to true when finally running the test suite.

Even when this is set to false the check to confirm you're using the correct database and your custom assertions (see below) will still be run to ensure everything is configured correctly.

Custom Assertions

In order to flexibly confirm the database is in the correct state the context supports custom assertions. These are simple COUNT SQL queries which need to return 1 in order to pass. You can provide as many of these as you like.

For example:

SELECT COUNT(*) FROM core_config_data WHERE path = 'web/unsecure/base_url' AND value = 'https://www.base.url.com/'

Adding Support For New Platforms

All platform code is contained within the Platform class. In order to extend this you need to provide two functions; one which handles platform detection and one which handles database detection.

Platform Detection

Add Detection Method

You need to add a method that can detect the platform from the project root. The Magento platform detection simply does this by looking for local.xml:

    // ...

    const MAGENTO_ONE_PATH_TO_LOCAL_XML = '/public/app/etc/local.xml';

    private static function detectMagentoOnePlatform($projectRoot)
    {
        return is_file($projectRoot . self::MAGENTO_ONE_PATH_TO_LOCAL_XML);
    }
    
    // ...
Add Method to Detect

You then need to add this to the detect method:

    // ...
    
    public static function detect()
    {
        // ...

        while (true) {
            if (self::detectMagentoOnePlatform($searchPath)) {
                return [self::MAGENTO_ONE, $searchPath];
            }

            // ...

            $searchPath = realpath($searchPath . '/../');

            if ($searchPath === false) {
                break;
            }
        }

        throw new \RuntimeException('Failed finding project root.');
    }
    
    // ...

And add your platform as a constant:

// ...

    const MAGENTO_ONE = 'magento';
    
// ...

Database Detection

Now that you have platform detection in place you need to handle database configuration detection.

Add Assertion Method

You need to add a method that can assert that the platforms database is currently configured to use the testing database. If the platform isn't configured correctly it should throw an exception.

Here's the Magento 1 assertion for example:

    // ...
    
    private static function assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName)
    {
        $localXml = simplexml_load_string(
            file_get_contents($projectRoot . Platform::MAGENTO_ONE_PATH_TO_LOCAL_XML)
        );

        if (! isset($localXml->global->resources->default_setup->connection->dbname)) {
            throw new \RuntimeException(
                'You need to configure a dbname in your local.xml'
            );
        }

        if ((string) $localXml->global->resources->default_setup->connection->dbname === $databaseName) {
            return;
        }

        throw new \InvalidArgumentException(
            "You need to configure Magento to use the testing database '$databaseName' in local.xml"
        );
    }
    
    // ...
Add Method to Assert

You then need to add this platform specific assertion to the generic assertion method:

    // ...
    
    public static function assertTestingDatabaseIsBeingUsed($databaseName)
    {
        list($platform, $projectRoot) = self::detect();

        switch ($platform) {
            case self::MAGENTO_ONE:
                self::assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName);
                break;
            // Add your platform specific assertion here...
        }
    }
    
    // ...

Your platform is now supported!

edmondscommerce/behat-db-context 适用场景与选型建议

edmondscommerce/behat-db-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「testing」 「Behat」 「Browser Testing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 edmondscommerce/behat-db-context 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-19