<p>For info I managed installing the plugin using composer php functions, here's the code :</p>
<pre><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'][&quot;myvendor/myplugin&quot;] = '*';
$config['repositories']['0'] = [
    &quot;type&quot; =&gt; &quot;path&quot;,
    &quot;url&quot; =&gt; &quot;tests/plugins/*&quot;,
    &quot;options&quot; =&gt; [
        &quot;symlink&quot; =&gt; true
    ]
];
$composer = Factory::create($io, $config);
$im = $composer-&gt;getInstallationManager();
$locker = new Locker($io, new JsonFile($lockPath, null, $io), $im, file_get_contents($jsonPath));
$composer-&gt;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-&gt;setIgnorePlatformRequirements(true);
$installer-&gt;setDevMode(true)-&gt;setOptimizeAutoloader(true);
$installer-&gt;setUpdate(true)-&gt;setUpdateAllowList(['myvendor/myplugin' =&gt; '*']);
$installer-&gt;run();
codecept_debug($io-&gt;getOutput());
//Need to reinitialize the plugins service to read the latest from vendor/craftcms/plugins.php
Craft::$app-&gt;plugins-&gt;init();
Craft::$app-&gt;plugins-&gt;installPlugin('myplugin');
</code></pre>
<p>Hope that helps</p>

CC BY-SA 4.0
2021-08-01T07:05:00.360Z
2021-08-01T07:37:35.473Z
2021-08-01T07:37:35.473Z
boris
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>Same problem if I have the plugin on my github account.</p>
<p>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 ?<br />
Or is there another way to approach this ?</p>
<p>Thank you for your time</p>

<plugin-development><craft3><tests>
Install a plugin in a unit test
# Install a plugin in a unit test

(Asked by: username_0 on 2021-08-01)

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

**Tags:** plugin-development, craft3, tests


---

## 1 answers

### Answer by username_0 on 2021-08-01. Score: 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


---

