Skip to content

Commit b82b7be

Browse files
committed
refactor: unify post-install and post-update event handling to run sync command
Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent 0f450d9 commit b82b7be

File tree

2 files changed

+26
-76
lines changed

2 files changed

+26
-76
lines changed

src/Composer/Plugin.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,50 +60,33 @@ public function getCapabilities(): array
6060
public static function getSubscribedEvents(): array
6161
{
6262
return [
63-
ScriptEvents::POST_INSTALL_CMD => 'onPostInstall',
64-
ScriptEvents::POST_UPDATE_CMD => 'onPostUpdate',
63+
ScriptEvents::POST_INSTALL_CMD => 'runSyncCommand',
64+
ScriptEvents::POST_UPDATE_CMD => 'runSyncCommand',
6565
];
6666
}
6767

6868
/**
6969
* Handles the automated script installation.
7070
*
71-
* This method MUST be triggered by `POST_INSTALL_CMD` and SHALL delegate
72-
* the actual work to the `installScripts` utility.
71+
* This method MUST execute the `dev-tools:sync` command after relevant Composer operations to ensure
72+
* the development tools are correctly synchronized with the current project state.
7373
*
7474
* @param Event $event the Composer script event context
7575
*
7676
* @return void
7777
*/
78-
public function onPostInstall(Event $event): void
78+
public function runSyncCommand(Event $event): void
7979
{
8080
$event->getComposer()
81-
->getEventDispatcher()
82-
->dispatchScript('dev-tools:sync', true);
83-
}
84-
85-
/**
86-
* Handles the automated script synchronization after updates.
87-
*
88-
* This method MUST be triggered by `POST_UPDATE_CMD` and SHALL ensure
89-
* that all development scripts are correctly aligned in the root configuration.
90-
*
91-
* @param Event $event the Composer script event context
92-
*
93-
* @return void
94-
*/
95-
public function onPostUpdate(Event $event): void
96-
{
97-
$event->getComposer()
98-
->getEventDispatcher()
99-
->dispatchScript('dev-tools:sync', true);
81+
->getLoop()
82+
->getProcessExecutor()
83+
->execute('vendor/bin/dev-tools dev-tools:sync');
10084
}
10185

10286
/**
10387
* Handles activation lifecycle events for the Composer session.
10488
*
105-
* The method MUST ensure the `dev-tools` script capability exists inside `composer.json` extras.
106-
* It SHOULD append it if currently missing.
89+
* This method MUST adhere to the standard Composer plugin activation protocol, even if no specific logic is required.
10790
*
10891
* @param Composer $composer the primary package configuration instance over Composer
10992
* @param IOInterface $io interactive communication channels

tests/Composer/PluginTest.php

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
namespace FastForward\DevTools\Tests\Composer;
2020

21-
use Composer\Package\RootPackageInterface;
22-
use Composer\EventDispatcher\EventDispatcher;
2321
use Composer\Composer;
2422
use Composer\IO\IOInterface;
2523
use Composer\Plugin\Capability\CommandProvider;
2624
use Composer\Script\Event as ScriptEvent;
25+
use Composer\Util\Loop;
26+
use Composer\Util\ProcessExecutor;
2727
use FastForward\DevTools\Composer\Capability\DevToolsCommandProvider;
2828
use FastForward\DevTools\Composer\Plugin;
2929
use PHPUnit\Framework\Attributes\CoversClass;
@@ -125,62 +125,29 @@ public function activateWillDoNothing(): void
125125
* @return void
126126
*/
127127
#[Test]
128-
public function onPostInstallWillInstallScripts(): void
128+
public function runSyncCommandWillExecuteDevToolsSync(): void
129129
{
130130
$event = $this->prophesize(ScriptEvent::class);
131131

132-
// Mock RootPackageInterface para getPackage()
133-
$package = $this->prophesize(RootPackageInterface::class);
134-
$package->getName()
135-
->willReturn('fast-forward/dev-tools');
136-
$this->composer->getPackage()
137-
->willReturn($package->reveal());
132+
// Mock ProcessExecutor
133+
$processExecutor = $this->prophesize(ProcessExecutor::class);
134+
$processExecutor->execute('vendor/bin/dev-tools dev-tools:sync')
135+
->shouldBeCalled();
138136

139-
// Mock EventDispatcher
140-
$eventDispatcher = $this->prophesize(EventDispatcher::class);
141-
$eventDispatcher->dispatchScript('dev-tools:sync', true)
142-
->willReturn(0);
143-
$this->composer->getEventDispatcher()
144-
->willReturn($eventDispatcher->reveal());
137+
// Mock Loop
138+
$loop = $this->prophesize(Loop::class);
139+
$loop->getProcessExecutor()
140+
->willReturn($processExecutor->reveal());
145141

146-
$event->getComposer()
147-
->willReturn($this->composer->reveal());
148-
$event->getIO()
149-
->willReturn($this->io->reveal());
150-
151-
$this->plugin->onPostInstall($event->reveal());
152-
153-
self::assertTrue(true); // Evita teste risky
154-
}
155-
156-
/**
157-
* @return void
158-
*/
159-
#[Test]
160-
public function onPostUpdateWillInstallScripts(): void
161-
{
162-
$event = $this->prophesize(ScriptEvent::class);
163-
164-
// Mock RootPackageInterface para getPackage()
165-
$package = $this->prophesize(RootPackageInterface::class);
166-
$package->getName()
167-
->willReturn('fast-forward/dev-tools');
168-
$this->composer->getPackage()
169-
->willReturn($package->reveal());
170-
171-
// Mock EventDispatcher
172-
$eventDispatcher = $this->prophesize(EventDispatcher::class);
173-
$eventDispatcher->dispatchScript('dev-tools:sync', true)
174-
->willReturn(0);
175-
$this->composer->getEventDispatcher()
176-
->willReturn($eventDispatcher->reveal());
142+
// Mock Composer
143+
$composer = $this->prophesize(Composer::class);
144+
$composer->getLoop()
145+
->willReturn($loop->reveal());
177146

178147
$event->getComposer()
179-
->willReturn($this->composer->reveal());
180-
$event->getIO()
181-
->willReturn($this->io->reveal());
148+
->willReturn($composer->reveal());
182149

183-
$this->plugin->onPostUpdate($event->reveal());
150+
$this->plugin->runSyncCommand($event->reveal());
184151

185152
self::assertTrue(true); // Evita teste risky
186153
}

0 commit comments

Comments
 (0)