diff --git a/docs/5.x/tests-system.md b/docs/5.x/tests-system.md index 61107384d..fdb3d126e 100644 --- a/docs/5.x/tests-system.md +++ b/docs/5.x/tests-system.md @@ -195,6 +195,45 @@ class ApiTest extends SystemTestCase } ``` +## How to write a test to scan for Matomo core dependencies being used directly in your plugins + +With the release of Matomo `5.1.0`, you can now easily check if your plugin is using any core dependencies by running the `tests:check-direct-dependency-use` command. With the release of Matomo 5, plugins should not use core dependencies directly but instead use them via [proxies](https://developer.matomo.org/guides/migrate-matomo-4-to-5#vendor-proxies). To have a check for this in your test suite and run it automatically, you can also write a system test case by using the sample code below: + +```php +use Piwik\Plugins\TestRunner\Commands\CheckDirectDependencyUse; +use Piwik\Tests\Framework\TestCase\SystemTestCase; +use Piwik\Version; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\NullOutput; + +class CheckDirectDependencyUseCommandTest extends SystemTestCase +{ + public function testCommand() + { + if (version_compare(Version::VERSION, '5.1.0', '<=') || !file_exists(PIWIK_INCLUDE_PATH . '/plugins/TestRunner/Commands/CheckDirectDependencyUse.php')) { + $this->markTestSkipped('tests:check-direct-dependency-use is not available in this version'); + } + + $pluginName = '{YOUR_PLUGIN_NAME}'; + + $checkDirectDependencyUse = new CheckDirectDependencyUse(); + $console = new \Piwik\Console(self::$fixture->piwikEnvironment); + $console->addCommands([$checkDirectDependencyUse]); + + $command = $console->find('tests:check-direct-dependency-use'); + $arguments = [ + 'command' => 'tests:check-direct-dependency-use', + '--plugin' => $pluginName, + '--grep-vendor', + ]; + $inputObject = new ArrayInput($arguments); + $command->run($inputObject, new NullOutput()); + + $this->assertEmpty($checkDirectDependencyUse->usesFoundList[$pluginName]); + } +} +``` + ## Writing tests for commands It is also possible to write system tests for console commands. These tests should extend `Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase`.