Plugin testing

Unit tests

If you want to start writing a new plugin keep in mind that unit testing helps you to deliver higher quality plugins. With help of the cli tools you can easily start with plugin skeleton which has all relevant snippets to start directly with testing. One of the first things that is important for testing is a phpunit.xml[.dist]. It could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/Bootstrap.php">
<testsuite name="MyPlugin Test Suite">
    <directory>tests</directory>
</testsuite>
</phpunit>

Basics

The starting point for testing your own plugin ist the phpunit.xml[.dist]_ and the Bootstrap.php file in the \tests folder of your plugin.

With the Bootstrap.php you can setup your environment properly to prepare it for testing. You can initialize the kernel, register event subscribers and namespaces or you could initialize the shop context to make currencies available. To prepare your plugin for testing it is necessary to require the Shopware helper bootstrap.

<?php

require __DIR__ . '/../../../../tests/Functional/bootstrap.php';
The path to this file depends on wheter you are using the legacy plugin system or the new 5.2 Plugin system.

The helper bootstrap starts the testing kernel and makes several functions like Shopware() available for you. You can then use the service container.

Writing tests

You can then place your test which could look like this:

<?php

class CalculatorTest extends Shopware\Components\Test\Plugin\TestCase
{
    protected static $ensureLoadedPlugins = array(
        'MyPlugin' => [
            'some_config' => 'foo'
        ]
    );

    public function testMyService()
    {
        $service = new MyService();
        $result = $service->add(1, 1);

        $this->assertEquals(2, $result);
    }
}

You can run this test from your plugin folder by typing phpunit if you have installed phpunit globally. Otherwise you could use the onboard phpunit version shopware comes with.

With help of the $ensureLoadedPlugins static you can assure that your plugin is installed and activated and you can even configure it. It is not required to assure that and the less your test needs the Shopware stack, the better the test is written.

We have written a tiny example plugin just to show how you could start testing your work. You can find a installable ZIP package of it here.