Browse Source

Remove usage of echo when executing Composer script

Fabien Potencier 8 năm trước cách đây
mục cha
commit
103624d4ed

+ 11 - 1
src/Composer/Util/ProcessExecutor.php

@@ -99,7 +99,17 @@ class ProcessExecutor
             return;
         }
 
-        echo $buffer;
+        if (null === $this->io) {
+            echo $buffer;
+
+            return;
+        }
+
+        if (Process::ERR === $type) {
+            $this->io->writeError($buffer);
+        } else {
+            $this->io->write($buffer);
+        }
     }
 
     public static function getTimeout()

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

@@ -340,7 +340,7 @@ class EventDispatcherTest extends TestCase
             ->setConstructorArgs(array(
                 $this->createComposerInstance(),
                 $io = $this->getMock('Composer\IO\IOInterface'),
-                new ProcessExecutor,
+                new ProcessExecutor($io),
             ))
             ->setMethods(array('getListeners'))
             ->getMock();
@@ -354,9 +354,11 @@ class EventDispatcherTest extends TestCase
             ->method('writeError')
             ->with($this->equalTo('> echo foo'));
 
-        ob_start();
+        $io->expects($this->once())
+            ->method('write')
+            ->with($this->equalTo('foo'.PHP_EOL));
+
         $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
-        $this->assertEquals('foo', trim(ob_get_clean()));
     }
 
     public function testDispatcherOutputsErrorOnFailedCommand()

+ 11 - 0
tests/Composer/Test/Util/ProcessExecutorTest.php

@@ -35,6 +35,17 @@ class ProcessExecutorTest extends TestCase
         $this->assertEquals("foo".PHP_EOL, $output);
     }
 
+    public function testUseIOIsNotNullAndIfNotCaptured()
+    {
+        $io = $this->getMock('Composer\IO\IOInterface');
+        $io->expects($this->once())
+            ->method('write')
+            ->with($this->equalTo('foo'.PHP_EOL));
+
+        $process = new ProcessExecutor($io);
+        $process->execute('echo foo');
+    }
+
     public function testExecuteCapturesStderr()
     {
         $process = new ProcessExecutor;