Edit on GitHub

Unit Testing

Testing plugins

Individual plugin test cases can be run by running the winter:test command with the --p|plugin= option.

Creating plugin tests

Plugins can be tested by creating a file called phpunit.xml in the base directory with the following content, for example, in a file /plugins/acme/blog/phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="../../../tests/bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
>
    <testsuites>
        <testsuite name="Plugin Unit Test Suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

Then a tests/ directory can be created to contain the test classes. The file structure should mimic the base directory with classes having a Test suffix. Using a namespace for the class is also recommended.

<?php namespace Acme\Blog\Tests\Models;

use Acme\Blog\Models\Post;
use PluginTestCase;

class PostTest extends PluginTestCase
{
    public function testCreateFirstPost()
    {
        $post = Post::create(['title' => 'Hi!']);
        $this->assertEquals(1, $post->id);
    }
}

The test class should extend the base class PluginTestCase and this is a special class that will set up the Winter database stored in memory, as part of the setUp method. It will also refresh the plugin being tested, along with any of the defined dependencies in the plugin registration file. This is the equivalent of running the following before each test:

php artisan winter:up
php artisan plugin:refresh Acme.Blog
[php artisan plugin:refresh <dependency>, ...]

NOTE: If your plugin uses configuration files, then you will need to run System\Classes\PluginManager::instance()->registerAll(true); in the setUp method of your tests. Below is an example of a base test case class that should be used if you need to test your plugin working with other plugins instead of in isolation.

use System\Classes\PluginManager;

class BaseTestCase extends PluginTestCase
{
    public function setUp(): void
    {
        parent::setUp();

        // Get the plugin manager
        $pluginManager = PluginManager::instance();

        // Register the plugins to make features like file configuration available
        $pluginManager->registerAll(true);

        // Boot all the plugins to test with dependencies of this plugin
        $pluginManager->bootAll(true);
    }

    public function tearDown(): void
    {
        parent::tearDown();

        // Get the plugin manager
        $pluginManager = PluginManager::instance();

        // Ensure that plugins are registered again for the next test
        $pluginManager->unregisterAll();
    }
}

Changing database engine for plugins tests

By default Winter CMS uses SQLite stored in memory for the plugin testing environment. You can override this behavior by using environment configuration files (i.e. creating a config/testing/database.php file to override the default config/database.php file).

You can override the /config/database.php file by creating /config/testing/database.php. In this case variables from the latter file will be taken.

System testing

To perform unit testing on the core Winter files, you should download a development copy by using Composer or cloning the Git repository. This will ensure you have the tests/ directory necessary to run unit tests.

Unit tests

Unit tests can be performed by running the winter:test command with the --o|-core option.

Functional tests

Functional tests can be performed by installing the Winter.Dusk in your Winter CMS installation. The Winter.Dusk plugin is powered by Laravel Dusk, a comprehensive testing suite for the Laravel framework that is designed to test interactions with a fully operational Winter CMS instance through a virtual browser.

For information on installing and setting up your Winter CMS install to run functional tests, please review the README for the plugin.

Keep informed

Sign up to our newsletter to receive updates on Winter CMS releases, new features in the works, and much more.
We'll never spam or give this address away.

Latest blog post

Winter v1.2.1 is now available

Published October 19, 2022

View this post Read all posts

Latest Winter CMS release

v1.2.1

Released October 20, 2022
14 UX/UI Improvements, 25 API Changes, 33 Bug Fixes, 4 Security Improvements, 5 Translation Improvements, 1 Performance Improvement, 2 Community Improvements, 2 Dependencies, 0 New Contributors * @cstorus made their first contribution in https://github.com/wintercms/winter/pull/616 * @simonmannsfeld made their first contribution in https://github.com/wintercms/winter/pull/623 * @quangtrongonline made their first contribution in https://github.com/wintercms/winter/pull/636 * @nathanlesage made their first contribution in https://github.com/wintercms/winter/pull/665 * @vllvll made their first contribution in https://github.com/wintercms/winter/pull/669 * @robertalexa made their first contribution in https://github.com/wintercms/winter/pull/668 * @iamyigitkoc made their first contribution in https://github.com/wintercms/winter/pull/624 * @hecc127 made their first contribution in https://github.com/wintercms/winter/pull/682 * @prsuhas made their first contribution in https://github.com/wintercms/winter/pull/723

View details View all releases