edmondscommerce/ behat-db-context
用于管理测试数据库的Behat上下文
Requires
- behat/behat: ~3.3
Suggests
- edmondscommerce/behat-framework: Parent framework, allows easier behat setup
This package is auto-updated.
Last update: 2024-08-29 04:31:40 UTC
README
由Edmonds Commerce提供
为管理您的测试数据库提供Behat上下文。
安装
通过Composer安装
"edmondscommerce/behat-db-context": "dev-master@dev"
在Behat配置中包含上下文
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"
功能
确保您正在使用测试数据库
该上下文通过平台检测来确保您的应用程序目前正在使用您在 behat.yml
中配置的测试数据库,使用 databaseName
。
如果您没有使用正确的数据库,则将收到异常,并且测试将不会继续。
要为您的平台添加支持,您可以按照下面“为新的平台添加支持”部分中详细说明的步骤操作。
导入新的测试数据库
每次运行测试套件时,该上下文都会导入测试数据库的新版本。它使用 pathToSqlDump
配置在您的 behat.yml
中的SQL转储,并将其导入到数据库 databaseName
。
注意:此上下文假设您已将MySQL凭据配置在 .my.cnf
。
可选的测试数据库导入
当 importTestingDatabase
设置为 false
时,将跳过导入步骤。这在本地工作测试时很有用,但最终运行测试套件时应始终设置为 true
。
即使设置为 false
,也会运行检查以确认您是否正在使用正确的数据库以及您的自定义断言(见下文),以确保一切配置正确。
自定义断言
为了灵活地确认数据库是否处于正确的状态,上下文支持自定义断言。这些是简单的 COUNT
SQL查询,需要返回 1
才能通过。您可以提供任意多的这些。
例如
SELECT COUNT(*) FROM core_config_data WHERE path = 'web/unsecure/base_url' AND value = 'https://www.base.url.com/'
为新的平台添加支持
所有平台代码都包含在 Platform 类中。为了扩展它,您需要提供两个函数;一个用于处理平台检测,另一个用于处理数据库检测。
平台检测
添加检测方法
您需要添加一个可以从项目根目录检测平台的方法。Magento平台检测只需查找 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); } // ...
添加检测方法
然后您需要将此方法添加到检测方法中
// ... 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.'); } // ...
并将您的平台作为常量添加
// ... const MAGENTO_ONE = 'magento'; // ...
数据库检测
现在您已设置平台检测,您需要处理数据库配置检测。
添加断言方法
您需要添加一个方法,可以断言平台数据库当前配置为使用测试数据库。如果平台配置不正确,则应抛出异常。
例如,以下是Magento 1断言
// ... 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" ); } // ...
添加断言方法
然后您需要将此特定平台的断言添加到通用断言方法中
// ... 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... } } // ...
您的平台现在受到支持!