shopware 6 插件的单元测试未找到自定义服务

Ezy*_*cod 5 shopware shopware6

我编写了一个插件,现在我想在其中创建测试。我按照官方文档并根据我的开发环境更改了 phpunit.xml.dist 。

测试正在运行,但容器不知道我的自定义定义/存储库。是不是我设置错了什么?

我尝试在标准 Dockware 容器上运行它。

phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.1/phpunit.xsd"
         colors="true"
         bootstrap="../../../vendor/shopware/core/TestBootstrap.php"
         cacheResult="false">

    <php>
        <ini name="error_reporting" value="-1"/>
        <server name="KERNEL_CLASS" value="Shopware\Production\Kernel"/>
        <env name="APP_ENV" value="test"/>
        <env name="APP_DEBUG" value="1"/>
        <env name="APP_SECRET" value="*"/>
        <env name="SHELL_VERBOSITY" value="-1"/>
    </php>

    <testsuites>
        <testsuite name="base">
            <directory>src/Test</directory>
        </testsuite>
        <testsuite name="employee">
            <directory>src/Components/Employee/Test</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory suffix=".php">./</directory>
        </whitelist>
    </filter>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

错误:

$ bash ./bin/phpunit.sh 
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

E.                                                                  2 / 2 (100%)

Time: 00:00.029, Memory: 40.50 MB

There was 1 error:

1) ***\EmployeeRouteTest::testEmployeeIsCreatedCorrectly
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "***_employee.repository". Did you mean one of these: "app_template.repository", "mail_template.repository", "media_folder.repository"?

/var/www/html/vendor/symfony/dependency-injection/Container.php:271
/var/www/html/vendor/symfony/dependency-injection/Container.php:219
/var/www/html/vendor/symfony/framework-bundle/Test/TestContainer.php:111
/var/www/html/custom/plugins/***/src/Components/Employee/Test/EmployeeRouteTest.php:30

ERRORS!
Tests: 2, Assertions: 5, Errors: 1.
Run Code Online (Sandbox Code Playgroud)

j_e*_*ing 5

您必须确保您的插件已在执行测试的数据库中安装并激活。

您可以通过plugin:install在自定义 phpunit 脚本中执行命令来完成此操作,也可以在 TestBootstrap 文件中安装插件。

有点取决于您的单元测试设置,尤其是测试执行期间的数据库设置。

默认情况下,商店软件单元测试是针对特殊{db-name}_test数据库执行的,因此您的插件可能已安装并在默认数据库中处于活动状态,但单元测试使用不同的数据库运行。

如果您使用 shopware db 方式,则有一个./psh.phar init-test-databases命令会将内容从默认数据库复制到测试数据库。如果您有自动安装插件的自定义安装命令,请确保init-test-databases也调用该命令,以便您的插件也会在测试数据库中标记为已安装并激活。

编辑

自从编写原始答案以来,我们添加了一个帮助程序,以便更轻松地引导应用程序以进行测试。

我们引入了一个TestBootstrapper可以配置为以您想要的方式引导应用程序的工具。

最简单的方法是创建一个TestBootstrap.php包含以下内容的文件:

require PATH_TO_SHOPWARE_CORE . '/TestBootstrapper.php';

(new TestBootstrapper())
    ->bootstrap();
Run Code Online (Sandbox Code Playgroud)

并使用该文件作为 phpunit 的引导文件。

如果您想确保在测试期间安装插件,您可以配置:

require PATH_TO_SHOPWARE_CORE . '/TestBootstrapper.php';

(new TestBootstrapper())
    ->addActivePlugins('YourPluginName')
    ->bootstrap();
Run Code Online (Sandbox Code Playgroud)

有关如何使用 TestBootstrapper 的真实示例,您可以查看PayPal 插件