yireo/shopware6-integration-test-helper

帮助您测试 Shopware 插件的实用工具

安装: 55

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

类型:shopware-platform-plugin

1.0.0 2023-09-19 15:21 UTC

This package is auto-updated.

Last update: 2024-09-21 12:38:43 UTC


README

Shopware 6 插件和库,帮助您更容易地构建基于 PHPUnit 的集成测试

功能

  • 自定义 PHPUnit 引导
    • 支持各种环境变量
  • 各种 PHP 特性
    • assertBundleIsInstalled(string $bundleName)
    • assertServiceExists(string $serviceId)
    • assertEntityExtensionExists(string $entityExtensionClass)
  • PHP 类 \Yireo\IntegrationTestHelper\Test\Integration\AbstractTestCase
    • 已包含所有 PHP 特性

安装

安装此插件并激活它

composer require yireo/shopware6-integration-test-helper --dev
bin/console plugin:refresh
bin/console plugin:install --activate YireoIntegrationTestHelper

PHPUnit 配置

接下来,配置您的 PHPUnit 文件以使用文件 vendor/yireo/shopware6-integration-test-helper/src/test_bootstrap.php 作为引导。还要配置 DATABASE_URL(指向与常规 Shopware 数据库不同的数据库)。

这里有一个 PHPUnit 文件的示例

<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
         bootstrap="vendor/yireo/shopware6-integration-test-helper/src/test_bootstrap.php"
         colors="true" cacheResult="false">
    <php>
        <ini name="error_reporting" value="E_ALL"/>
        <server name="KERNEL_CLASS" value="Shopware\Core\Kernel"/>
        <env name="FORCE_INSTALL" value="1"/>
        <env name="FORCE_INSTALL_PLUGINS" value="1"/>
        <env name="FORCE_INSTALL_ADDITIONAL_PLUGINS" value="YireoExample"/>
        <env name="APP_ENV" value="test"/>
        <env name="APP_DEBUG" value="1"/>
        <env name="APP_SECRET" value="s$cretf0rt3st"/>
        <env name="SHELL_VERBOSITY" value="-1"/>
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
        <env name="LOCK_DSN" value="flock" />
        <env name="DATABASE_URL" value="mysql://root:root@mysql:3306/shopware6_test" />
    </php>
</phpunit>

除了使用 PHPUnit 文件,您也可以将环境变量添加到文件 .env.test 中。

  • FORCE_INSTALL1 时,Shopware 将进行重新安装;
  • FORCE_INSTALL_PLUGINS1 时,插件将被刷新、安装并激活;
  • FORCE_INSTALL_ADDITIONAL_PLUGINS 包含逗号分隔的插件列表时,每个插件将被安装并激活。请确保至少包括您自己的插件(在此示例中为 YireoExample);

PHP 父类的使用

namespace Swag\Example\Test\Integration;

use Yireo\IntegrationTestHelper\Test\Integration\AbstractTestCase;

class BasicPluginTest extends AbstractTestCase
{
    public function testIfBundleIsRegistered()
    {
        $this->assertBundleIsInstalled();
    }
}

PHP 特性的使用

namespace Swag\Example\Test\Integration;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Yireo\IntegrationTestHelper\Traits\AssertBundleIsInstalled;

class BasicPluginTest extends TestCase
{
    use IntegrationTestBehaviour;
    use AssertBundleIsInstalled;
    
    public function getName(): string
    {
        return self::class; // Shopware 6.5 requires you to implement this method
    }
   
    public function testIfBundleIsRegistered()
    {
        $this->assertBundleIsInstalled();
    }
}