Browse Source

fixes composer/composer#3546

Rob 10 years ago
parent
commit
d9b8b3611b

+ 7 - 1
src/Composer/EventDispatcher/EventDispatcher.php

@@ -223,7 +223,13 @@ class EventDispatcher
             return $event;
         }
 
-        $expected = $reflected->getClass()->name;
+        $typehint = $reflected->getClass();
+
+        if (!$typehint instanceof \ReflectionClass) {
+            return $event;
+        }
+
+        $expected = $typehint->getName();
 
         if (!$event instanceof $expected && $expected === 'Composer\Script\CommandEvent') {
             $event = new CommandEvent($event->getName(), $event->getComposer(), $event->getIO(), $event->isDevMode(), $event->getArguments());

+ 18 - 3
tests/Composer/Test/EventDispatcher/EventDispatcherTest.php

@@ -29,7 +29,7 @@ class EventDispatcherTest extends TestCase
     {
         $io = $this->getMock('Composer\IO\IOInterface');
         $dispatcher = $this->getDispatcherStubForListenersTest(array(
-            "Composer\Test\EventDispatcher\EventDispatcherTest::call"
+            'Composer\Test\EventDispatcher\EventDispatcherTest::call'
         ), $io);
 
         $io->expects($this->once())
@@ -43,7 +43,17 @@ class EventDispatcherTest extends TestCase
     {
         $io = $this->getMock('Composer\IO\IOInterface');
         $dispatcher = $this->getDispatcherStubForListenersTest(array(
-            "Composer\Test\EventDispatcher\EventDispatcherTest::convertEvent"
+            'Composer\Test\EventDispatcher\EventDispatcherTest::expectsCommandEvent'
+        ), $io);
+
+        $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
+    }
+    
+    public function testDispatcherDoesNotAttemptConversionForListenerWithoutTypehint()
+    {
+        $io = $this->getMock('Composer\IO\IOInterface');
+        $dispatcher = $this->getDispatcherStubForListenersTest(array(
+            'Composer\Test\EventDispatcher\EventDispatcherTest::expectsVariableEvent'
         ), $io);
 
         $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
@@ -216,7 +226,12 @@ class EventDispatcherTest extends TestCase
         throw new \RuntimeException();
     }
 
-    public static function convertEvent(CommandEvent $event)
+    public static function expectsCommandEvent(CommandEvent $event)
+    {
+        return false;
+    }
+
+    public static function expectsVariableEvent($event)
     {
         return false;
     }