yireo/magento2-integration-test-helper

支持其他模块集成测试的 Magento 2 模块

安装次数: 20,611

依赖者: 11

建议者: 0

安全性: 0

星标: 14

关注者: 2

分支: 0

类型:magento2-module

0.0.13 2024-06-28 13:03 UTC

This package is auto-updated.

Last update: 2024-09-15 11:21:17 UTC


README

此模块添加了各种实用工具,以帮助创建 Magento 2 的集成测试。

安装

使用以下命令进行安装

composer require yireo/magento2-integration-test-helper --dev

启用此模块

./bin/magento module:enable Yireo_IntegrationTestHelper
./bin/magento setup:upgrade

使用此助手增强您的测试

父类

  • \Yireo\IntegrationTestHelper\Test\Integration\AbstractTestCase
  • \Yireo\IntegrationTestHelper\Test\Integration\GraphQlTestCase

这些类提供了一些实用函数,并导入了多个 traits(请参阅 Test/Integration/Traits/)与 PHPUnit 断言。例如,以下测试检查您的模块是否已正确注册

<?php declare(strict_types=1);

namespace Yireo\Example\Test\Integration;

use PHPUnit\Framework\TestCase;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsEnabled;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegistered;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegisteredForReal;

class ModuleTest extends TestCase
{
    use AssertModuleIsEnabled;
    use AssertModuleIsRegistered;
    use AssertModuleIsRegisteredForReal;

    public function testIfModuleIsWorking()
    {
        $this->assertModuleIsEnabled('Yireo_Example');
        $this->assertModuleIsRegistered('Yireo_Example');
        $this->assertModuleIsRegisteredForReal('Yireo_Example');
    }
}

在集成测试配置中切换 TESTS_CLEANUP

在运行集成测试时,您可能需要频繁地将常量 TESTS_CLEANUPdisabled 切换到 enabled 再切换回 disabled。以下命令行可以轻松实现此操作(假设文件实际上是 dev/tests/integration/phpunit.xml,因为您不应该修改 *.dist 版本)

bin/magento integration_tests:toggle_tests_cleanup

它已切换。您也可以直接设置值

bin/magento integration_tests:toggle_tests_cleanup enabled

生成 install-config-mysql.php 返回数组

当安装 Magento - 作为运行集成测试过程的一部分 - 文件 dev/tests/integration/etc/install-config-mysql.php 应返回包含所有相关设置的数组,最重要的是数据库设置。通过使用实用类 Yireo\IntegrationTestHelper\Utilities\InstallConfig,您可以快速生成相关输出,包括 Redis 和 ElasticSearch 等详细信息

<?php declare(strict_types=1);

use Yireo\IntegrationTestHelper\Utilities\InstallConfig;

return (new InstallConfig())
    ->addDb('mysql80_tmpfs')
    ->addRedis()
    ->addElasticSearch('elasticsearch6')
    ->get();

在安装 Magento 时禁用模块

当安装 Magento - 作为运行集成测试过程的一部分 - 文件 dev/tests/integration/etc/install-config-mysql.php 被修改以指向您的测试数据库。还有一个名为 disable-modules 的标志,允许您禁用特定的 Magento 模块。禁用模块可以提高性能。实用类 Yireo\IntegrationTestHelper\Utilities\DisableModules 允许您快速生成要禁用的模块列表。

在以下示例中,首先默认禁用了在全局 app/etc/config.php 中列出的所有 (!) 模块。然后,所有 Magento 核心模块和 Yireo_GoogleTagManager2 模块被启用(但只有当它们在全局配置中标记为活动状态时)

<?php declare(strict_types=1);

use Yireo\IntegrationTestHelper\Utilities\DisableModules;
use Yireo\IntegrationTestHelper\Utilities\InstallConfig;

$disableModules = (new DisableModules())
    ->disableAll()
    ->enableMagento()
    ->enableByName('Yireo_GoogleTagManager2')
    ->toString();

return (new InstallConfig())
    ->setDisableModules($disableModules)
    ->addDb('mysql80_tmpfs')
    ->addRedis()
    ->addElasticSearch('elasticsearch6')
    ->get();

您也可以通过设置环境变量 MAGENTO_MODULE - 例如,在 PHPStorm 的 运行 配置中 - 来代替使用硬编码值,然后通过 enableByMagentoModuleEnv 方法重用该值。这样,您可以在不同的 运行 配置中重用相同的 install-config-mysql.php 文件

$disableModules->disableAll()
    ->enableMagento()
    ->enableByMagentoModuleEnv();

另一个示例,默认情况下启用所有 Magento 模块。然后再次禁用 MSI 和 GraphQL,同时启用所有 Yireo 模块

$disableModules = (new DisableModules())
    ->disableAll()
    ->enableMagento()
    ->disableMagentoInventory()
    ->disableGraphQl()
    ->enableByPattern('Yireo_')
    ->toString();

请注意,如果存在 Yireo_ExampleGraphQl 模块,则它将首先通过 disableGraphQl() 禁用,然后通过 enableByPattern('Yireo_') 重新启用。您方法的顺序很重要!

验证您的配置

此模块还附带了一个 CLI 命令,可以轻松检查当前返回的 setup:install 标志是否合理

$ bin/magento integration_tests:check
+--------------------+--------------------+
| Setting            | Value              |
+--------------------+--------------------+
| TESTS_CLEANUP      | enabled            |
| TESTS_MAGENTO_MODE | developer          |
| DB host            | mysql57_production |
| DB username        | root               |
| DB password        | root               |
| DB name            | m2_test            |
| DB reachable       | Yes                |
| ES host            | localhost          |
| ES port            | 9207               |
| ES reachable       | Yes                |
| Redis host         | 127.0.0.1          |
| Redis port         | 6379               |
| Redis reachable    | Yes                |
+--------------------+--------------------+