shopware/shopware

Ensure calling plugin is loaded in TestBootstrap.php (or a separate bootstrap file just for plugins)

svlauer opened this issue · 0 comments

Please describe the feature you would like to see implemented.

When used to test a plugin, src/Core/TestBootstrap.php does not ensure that the calling plugin is activated in the test database. This leads to error messages in particular if services from the plugin are used in the tests (and probably in other circumstances). These error messages make it seem like something is wrong with the service declaration (or implementing classes), when in fact it is a testing issue.
(This cost me a day trying to figure out what is wrong with my custom entity, when there in fact wasn't anything wrong with it ...)

(The issue is more serious in the production template, I believe, since there is no ./psh.phar init-test-databases. Instead, the test database is initialized whenever tests are run for the first time. If I am not mistaken, this means that whether or not the calling plugin is activated in the test database depends on whether the plugin happens to be activated in the main database at the time the first unit test is run.)

Suggested change: Ensure that calling plugin is loaded, like so:

<?php declare(strict_types=1);

namespace Shopware\Core;

require __DIR__ . '/TestBootstrapper.php';

(new TestBootstrapper())
    ->setPlatformEmbedded(false)
    ->addCallingPlugin()
    ->setForceInstallPlugins(true)
    ->bootstrap();

I am happy to roll a pull request for this, but I am unsure whether the above change should/can go into the existing TestBootstrap.php file - there might be situations where the forced loading of the calling plugin is unwanted (e.g. core/non-plugin tests). So there really are two options:

  1. Adjust the existing src/Core/TestBootstrap.php file as above.
  2. Create a separate file, e.g. src/Core/TestBootstrapPlugins.php that contains the above code.

(Note: I first filed a documentation issue about this: 476: Document necessary step for testing in production template, but then I realized that every plugin would have to implement the same custom bootstrap file, making me think that this should be fixed in core instead.)