3

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

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

我试图在标准的 Dockware Container 上运行它。

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>

错误:

$ 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.
4

1 回答 1

3

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

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

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

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

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

于 2021-11-30T14:55:23.663 回答