PHPUnit测试shell执行

PHPUnit测试shell执行,php,phpunit,Php,Phpunit,我有一个类负责与shell的交互,是否有任何方法可以使用PHPUnit测试像这样的函数 public function runCommand($command, $stdin = null) { $descriptorspec = array( array("pipe", "r"), // stdin array("pipe", "w"), // stdout array("pipe", "w"), // stderr );

我有一个类负责与shell的交互,是否有任何方法可以使用PHPUnit测试像这样的函数

public function runCommand($command, $stdin = null)
{
    $descriptorspec = array(
        array("pipe", "r"), // stdin
        array("pipe", "w"), // stdout
        array("pipe", "w"), // stderr
    );

    $environment = array();

    $proc = proc_open(
        $command,
        $descriptorspec,
        $pipes,
        __DIR__,
        $environment
    );

    if (!is_resource($proc)) {
        return false;
    }

    if ($stdin !== null) {
        fwrite($pipes[0], $stdin);
        fclose($pipes[0]);
    }

    $result = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    if (proc_close($proc) !== 0) {
        return false;
    }

    return $result;
}

这是我在发布问题后想到的。因为我在linux上测试,所以我创建了一个bash脚本:

#!/bin/bash
echo -ne "exec_works"
然后在测试中运行它:

public function testShellExecution()
{
    // root tests directory constant, set in PHPUnit bootstrap file
    $path = TESTDIR . "/Resources/exec_test.sh";

    $this->assertEquals(
        "exec_works",
        $this->shellCommander->runCommand("bash $path")
    );
}
缺点是这样的测试只能在linux环境下通过(我从未使用过MAC,所以我不知道它是否运行bash脚本),但在windows上肯定会失败,因为windows无法在本机上运行bash脚本


解决方法是为每个操作系统创建可执行脚本,并测试检查哪个操作系统服务器使用并运行适当的脚本。

我没有看到任何执行shell命令的
exec()
命令。proc\u open,它不是在shell中执行$command吗?@fedorqui
$proc=proc\u open(
你说得对!对不起,我不知道这个函数。那么执行一个基本的
运行命令(“ls-l/tmp”)怎么样?