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 backupGlobals="false" backupStaticAttributes="false" bootstrap="./Tests/Functional/Bootstrap.php">
<testsuite name="SwagExampleTest Test Suite">
<directory>./Tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<exclude>
<directory suffix=".php">./Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
The starting point for testing your own plugin is the __phpunit.xml[.dist]___ in your plugin root directory and the Bootstrap.php
file in the ./Tests/Functional/
directory 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 helper bootstrap starts the testing kernel and makes several functions like Shopware()
available for you. You can then use the service container.
You can then place your test which could look like this:
<?php
class CalculatorTest extends Shopware\Tests\Functional\Components\Plugin\TestCase
{
protected static $ensureLoadedPlugins = [
'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 root directory 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.