Skip to content

Commit

Permalink
Fix init of asset fallback on the first installation
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Aug 19, 2019
1 parent d877c0c commit dca69a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Foxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Foxy;

use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvents;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
Expand Down Expand Up @@ -109,6 +112,9 @@ public static function getSubscribedEvents()
InstallerEvents::PRE_DEPENDENCIES_SOLVING => array(
array('init', 100),
),
PackageEvents::POST_PACKAGE_INSTALL => array(
array('initOnInstall', 100),
),
ScriptEvents::POST_INSTALL_CMD => array(
array('solveAssets', 100),
),
Expand Down Expand Up @@ -138,6 +144,20 @@ public function activate(Composer $composer, IOInterface $io)
$this->assetManager->setFallback($this->assetFallback);
}

/**
* Init the plugin just after the first installation.
*
* @param PackageEvent $event The package event
*/
public function initOnInstall(PackageEvent $event)
{
$operation = $event->getOperation();

if ($operation instanceof InstallOperation && 'foxy/foxy' === $operation->getPackage()->getName()) {
$this->init();
}
}

/**
* Init the plugin.
*/
Expand Down
32 changes: 32 additions & 0 deletions Tests/FoxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

use Composer\Composer;
use Composer\Config;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\RootPackageInterface;
use Composer\Script\Event;
use Foxy\Foxy;
use Foxy\Solver\SolverInterface;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Tests for foxy.
Expand Down Expand Up @@ -91,6 +93,36 @@ public function testActivate()
$this->assertTrue(true);
}

public function testActivateOnInstall()
{
$package = $this->getMockBuilder('Composer\Package\Package')
->disableOriginalConstructor()
->getMock()
;
$package->expects($this->once())
->method('getName')
->willReturn('foxy/foxy')
;

$operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\InstallOperation')
->disableOriginalConstructor()->getMock();
$operation->expects($this->once())
->method('getPackage')
->willReturn($package)
;

/** @var MockObject|PackageEvent $event */
$event = $this->getMockBuilder('Composer\Installer\PackageEvent')->disableOriginalConstructor()->getMock();
$event->expects($this->once())
->method('getOperation')
->willReturn($operation)
;

$foxy = new Foxy();
$foxy->activate($this->composer, $this->io);
$foxy->initOnInstall($event);
}

/**
* @expectedException \Foxy\Exception\RuntimeException
* @expectedExceptionMessage The asset manager "invalid_manager" doesn't exist
Expand Down

0 comments on commit dca69a7

Please sign in to comment.