1

I'm writing some unit tests for a plugin I've made using codeception. I need to test the installation of another plugin during a test.

Defining the plugin in codeception.yml doesn't work, the plugin isn't installed in composer yet. Anyway it's important the plugin is installed just before the test happens, or during.

I've tried copying the plugin in my tests folder and define a local repository in my composer.json. Then install the plugin through the Craft composer service. But it doesn't work either, I suppose because the plugin isn't registered in the Craft store.

Same problem if I have the plugin on my github account.

Is it my only option to install the plugin manually using composer php functions here ? If so would you have some links to examples on how to do that ?
Or is there another way to approach this ?

Thank you for your time

1 Answer 1

2

For info I managed installing the plugin using composer php functions, here's the code :

use Composer\Factory;
use Composer\IO\BufferIO;
use Composer\Installer;
use Composer\Json\JsonFile;
use Composer\Package\Locker;
use Craft;

$io = new BufferIO;
$jsonPath = realpath(__DIR__ . '/../../../composer.json');
$lockPath = realpath(__DIR__ . '/../../../composer.lock');
$config = json_decode(file_get_contents($jsonPath), true);
//Add a local repository and a requirement :
$config['require']["myvendor/myplugin"] = '*';
$config['repositories']['0'] = [
    "type" => "path",
    "url" => "tests/plugins/*",
    "options" => [
        "symlink" => true
    ]
];
$composer = Factory::create($io, $config);
$im = $composer->getInstallationManager();
$locker = new Locker($io, new JsonFile($lockPath, null, $io), $im, file_get_contents($jsonPath));
$composer->setLocker($locker);
$installer = Installer::create($io, $composer);
//This is needed on my setup (am running this outside of my docker container), but can be removed
$installer->setIgnorePlatformRequirements(true);
$installer->setDevMode(true)->setOptimizeAutoloader(true);
$installer->setUpdate(true)->setUpdateAllowList(['myvendor/myplugin' => '*']);
$installer->run();
codecept_debug($io->getOutput());
//Need to reinitialize the plugins service to read the latest from vendor/craftcms/plugins.php
Craft::$app->plugins->init();
Craft::$app->plugins->installPlugin('myplugin');

Hope that helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.