return the upper-most event in chain
parent
81a4f74b5b
commit
b51cfce8e6
|
@ -103,16 +103,31 @@ class Event extends BaseEvent
|
|||
return $this->originatingEvent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the originating event.
|
||||
*
|
||||
* @param \Composer\EventDispatcher\Event $event
|
||||
* @return $this
|
||||
*/
|
||||
/**
|
||||
* Set the originating event.
|
||||
*
|
||||
* @param \Composer\EventDispatcher\Event $event
|
||||
* @return $this
|
||||
*/
|
||||
public function setOriginatingEvent(BaseEvent $event)
|
||||
{
|
||||
$this->originatingEvent = $event;
|
||||
$this->originatingEvent = $this->calculateOriginatingEvent($event);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the upper-most event in chain.
|
||||
*
|
||||
* @param \Composer\EventDispatcher\Event $event
|
||||
* @return \Composer\EventDispatcher\Event
|
||||
*/
|
||||
private function calculateOriginatingEvent(BaseEvent $event)
|
||||
{
|
||||
if ($event instanceof Event && $event->getOriginatingEvent()) {
|
||||
return $this->calculateOriginatingEvent($event->getOriginatingEvent());
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Composer\Test\Script;
|
||||
|
||||
|
||||
use Composer\Composer;
|
||||
use Composer\Config;
|
||||
use Composer\Script\Event;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
class EventTest extends TestCase {
|
||||
|
||||
public function testEventSetsOriginatingEvent() {
|
||||
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
||||
$composer = $this->createComposerInstance();
|
||||
|
||||
$originatingEvent = new \Composer\EventDispatcher\Event('originatingEvent');
|
||||
|
||||
$scriptEvent = new Event('test', $composer, $io, true);
|
||||
|
||||
$this->assertNull(
|
||||
$scriptEvent->getOriginatingEvent(),
|
||||
'originatingEvent is initialized as null'
|
||||
);
|
||||
|
||||
$scriptEvent->setOriginatingEvent($originatingEvent);
|
||||
|
||||
$this->assertSame(
|
||||
$originatingEvent,
|
||||
$scriptEvent->getOriginatingEvent(),
|
||||
'getOriginatingEvent() SHOULD return test event'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public function testEventCalculatesNestedOriginatingEvent() {
|
||||
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
||||
$composer = $this->createComposerInstance();
|
||||
|
||||
|
||||
$originatingEvent = new \Composer\EventDispatcher\Event('upperOriginatingEvent');
|
||||
$intermediateEvent = new Event('intermediate', $composer, $io, true);
|
||||
$intermediateEvent->setOriginatingEvent($originatingEvent);
|
||||
|
||||
$scriptEvent = new Event('test', $composer, $io, true);
|
||||
$scriptEvent->setOriginatingEvent($intermediateEvent);
|
||||
|
||||
$this->assertNotSame(
|
||||
$intermediateEvent,
|
||||
$scriptEvent->getOriginatingEvent(),
|
||||
'getOriginatingEvent() SHOULD NOT return intermediate events'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$originatingEvent,
|
||||
$scriptEvent->getOriginatingEvent(),
|
||||
'getOriginatingEvent() SHOULD return upper-most event'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private function createComposerInstance()
|
||||
{
|
||||
$composer = new Composer;
|
||||
$config = new Config;
|
||||
$composer->setConfig($config);
|
||||
$package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
|
||||
$composer->setPackage($package);
|
||||
|
||||
return $composer;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue