JFD
JFD

Reputation: 33

PHPUnit testing a Shopware 6 Plugin with a dependency to a third party plugin

i'd like to unit test a shopware 6 plugin that has dependencies to a third party plugin. This is my service.xml:

<service id="MyPlugin\Services\ProductImportService">
            <argument type="service" id="product.repository"/>
            <argument type="service" id="product_visibility.repository"/>
            <argument type="service" id="tax.repository"/>
            <argument type="service" id="s_plugin_netzp_events.repository"/>
            <argument type="service" id="s_plugin_netzp_events_slots.repository"/>
        </service>

I use the repositories for some entities that are created by the third party plugin. My TestBootstrap.php looks like this:

<?php declare(strict_types=1);

use Shopware\Core\TestBootstrapper;

$loader = (new TestBootstrapper())
    ->addCallingPlugin()
    ->addActivePlugins('NetzpEvents6', 'MyPlugin')
    ->setForceInstallPlugins(true)
    ->bootstrap()
    ->getClassLoader();

$loader->addPsr4('MyPlugin\\tests\\', __DIR__);

when i start unit tests i got this error message:

www-data@44e8cf57d3d6:~/html$ ./vendor/bin/phpunit --configuration="custom/plugins/MyPlugin" PHPUnit 9.5.28 by Sebastian Bergmann and contributors.

Error in bootstrap script: Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The service "MyPlugin\Services\ProductImportService" has a dependency on a non-existent service "s_plugin_netzp_events.repository".

i've created a services_test.xml file to override services.xml and removed the arguments. With that, unit tests can be executed but i cannot instantiate my ProductImportService class because of missing dependencies.

The error only happens with unit testing and i'm still wondering why. There is no service with the id s_plugin_netzp_events.repository in netzP's event plugin and i think that causes the error. But why can i use this as an argument to access this database table in my Service but not in a Unit Test Case? Thank you for your help

Upvotes: 2

Views: 277

Answers (1)

Radek Kohut
Radek Kohut

Reputation: 41

I had the same issue, solution is to change the order of methods you call on TestBootstrapper() class, call method addActivePlugins() before addCallingPlugin():

<?php declare(strict_types=1);

use Shopware\Core\TestBootstrapper;

$loader = (new TestBootstrapper())    
    ->addActivePlugins('NetzpEvents6', 'MyPlugin')
    ->addCallingPlugin()
    ->setForceInstallPlugins(true)
    ->bootstrap()
    ->getClassLoader();

$loader->addPsr4('MyPlugin\\tests\\', __DIR__);

like this the TestBootstrapper will first install 'NetzpEvents6' and then your plugin

Upvotes: 1

Related Questions