Ver código fonte

Add test for intermixing PHP callables and CLI commands in a single event's script stack

Wrapped execution of the PHP callable in its own method in order to mock/test it
John Kary 12 anos atrás
pai
commit
88650f9333

+ 11 - 1
src/Composer/Script/EventDispatcher.php

@@ -95,7 +95,7 @@ class EventDispatcher
                 }
 
                 try {
-                    $className::$methodName($event);
+                    $this->executeEventPhpScript($className, $methodName, $event);
                 } catch (\Exception $e) {
                     $message = "Script %s handling the %s event terminated with an exception";
                     $this->io->write('<error>'.sprintf($message, $callable, $event->getName()).'</error>');
@@ -116,6 +116,16 @@ class EventDispatcher
         }
     }
 
+    /**
+     * @param string $className
+     * @param string $methodName
+     * @param Event $event          Event invoking the PHP callable
+     */
+    protected function executeEventPhpScript($className, $methodName, Event $event)
+    {
+        $className::$methodName($event);
+    }
+
     /**
      * @param  Event $event Event object
      * @return array Listeners

+ 43 - 0
tests/Composer/Test/Script/EventDispatcherTest.php

@@ -63,6 +63,44 @@ class EventDispatcherTest extends TestCase
         $dispatcher->dispatchCommandEvent("post-install-cmd");
     }
 
+    public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
+    {
+        $io = $this->getMock('Composer\IO\IOInterface');
+        $process = $this->getMock('Composer\Util\ProcessExecutor');
+        $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
+            ->setConstructorArgs(array(
+                $this->getMock('Composer\Composer'),
+                $io,
+                $process,
+            ))
+            ->setMethods(array(
+                'getListeners',
+                'executeEventPhpScript',
+            ))
+            ->getMock();
+
+        $io->expects($this->never())
+            ->method('write');
+        $process->expects($this->exactly(2))
+            ->method('execute');
+
+        $listeners = array(
+            'echo -n foo',
+            'Composer\\Test\\Script\\EventDispatcherTest::someMethod',
+            'echo -n bar',
+        );
+        $dispatcher->expects($this->atLeastOnce())
+            ->method('getListeners')
+            ->will($this->returnValue($listeners));
+
+        $dispatcher->expects($this->once())
+            ->method('executeEventPhpScript')
+            ->with('Composer\Test\Script\EventDispatcherTest', 'someMethod')
+            ->will($this->returnValue(true));
+
+        $dispatcher->dispatchCommandEvent("post-install-cmd");
+    }
+
     private function getDispatcherStubForListenersTest($listeners, $io)
     {
         $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
@@ -93,4 +131,9 @@ class EventDispatcherTest extends TestCase
     {
         throw new \RuntimeException();
     }
+
+    public static function someMethod()
+    {
+        return true;
+    }
 }