示例#1
0
 public function testCommandClass()
 {
     $commandLine = "echo \"Hello world!\"";
     $c = new Command($commandLine);
     $this->assertEquals($commandLine, $c);
     $stdOut = "./stdout.log";
     $c->setStdOut($stdOut);
     $this->assertEquals($stdOut, $c->getStdOut(), "stdout setter and getter are ok.");
     $stdErr = "./stderr.log";
     $c->setStdErr($stdErr);
     $this->assertEquals($stdErr, $c->getStdErr(), "stderr setter and getter are ok.");
     $c = new Command($commandLine);
     $c->runInBackground();
     if (substr(php_uname(), 0, 7) == "Windows") {
         $stdOutNul = ">NUL";
         $stdErrNul = "2>NUL";
     } else {
         $stdOutNul = "/dev/null";
         $stdErrNul = "2>/dev/null";
     }
     $this->assertEquals($stdOutNul, $c->getStdOut(), "If stdout is not set set it to {$stdOutNul} when running in background.");
     $this->assertEquals($stdErrNul, $c->getStdErr(), "If stderr is not set set it to {$stdErrNul} when running in background.");
     $c = new Command($commandLine);
     $c->setStdErr($stdErr);
     $c->setStdOut($stdOut);
     $c->runInBackground();
     $this->assertEquals($stdOut, $c->getStdOut(), "If stdout is set don't overwrite it when running in background.");
     $this->assertEquals($stdErr, $c->getStdErr(), "If stderr is set don't overwrite it when running in background.");
     $arg1 = "arg1";
     $arg2 = "arg2";
     $arg3 = "arg3";
     $c->addArgument($arg1);
     $c->addArgument($arg3);
     $expectedLine = "{$commandLine} {$arg1} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends arguments.");
     $c->addArgument($arg2, 2);
     $expectedLine = "{$commandLine} {$arg1} {$arg2} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly insertets arguments at given position.");
     $arg2_1 = "arg2_1";
     $c->addArgument($arg2_1, 2, true);
     $expectedLine = "{$commandLine} {$arg1} {$arg2_1} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly replaces arguments.");
     $c = new Command($commandLine);
     $c->addArgument($arg3, 3);
     $c->addArgument($arg1, 1);
     $c->addArgument($arg2, 2);
     $expectedLine = "{$commandLine} {$arg1} {$arg2} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly insertets arguments added in mixed order.");
     $c = new Command($commandLine);
     $c->addOption("-c");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends no-value option.");
     $c->removeOption("-c");
     $this->assertEquals($commandLine, (string) $c, "Command properly removesg options.");
     $c->addOption("-c:", "value_c");
     $expectedLine = "{$commandLine} -c \"value_c\"";
     $this->assertEquals($expectedLine, (string) $c, "Commadn properly appends options with required value.");
     $c = new Command($commandLine);
     $c->addOption("-c::");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends optional value option without value set.");
     $c = new Command($commandLine);
     $c->addOption("-c::", "value_c");
     $expectedLine = "{$commandLine} -c=\"value_c\"";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends optional value option.");
     $c = new Command($commandLine);
     $c->addOption("-c", "value_c");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends no-value option without value even if value is set.");
     $c = new Command($commandLine);
     $c->addOption("-a");
     $c->addOption("-b");
     $expectedLine = "{$commandLine} -a -b";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends multiple options");
 }