3 use MediaWiki\Shell\Command
;
8 class CommandTest
extends PHPUnit_Framework_TestCase
{
10 * @expectedException PHPUnit_Framework_Error_Notice
12 public function testDestruct() {
13 if ( defined( 'HHVM_VERSION' ) ) {
14 $this->markTestSkipped( 'destructors are unreliable in HHVM' );
16 $command = new Command();
17 $command->params( 'true' );
20 private function requirePosix() {
21 if ( wfIsWindows() ) {
22 $this->markTestSkipped( 'This test requires a POSIX environment.' );
27 * @dataProvider provideExecute
29 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
30 $this->requirePosix();
32 $command = new Command();
34 ->params( $commandInput )
37 $this->assertSame( $expectedExitCode, $result->getExitCode() );
38 $this->assertSame( $expectedOutput, $result->getStdout() );
41 public function provideExecute() {
43 'success status' => [ 'true', 0, '' ],
44 'failure status' => [ 'false', 1, '' ],
45 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
49 public function testEnvironment() {
50 $this->requirePosix();
52 $command = new Command();
54 ->params( [ 'printenv', 'FOO' ] )
55 ->environment( [ 'FOO' => 'bar' ] )
57 $this->assertSame( "bar\n", $result->getStdout() );
60 public function testOutput() {
63 $this->requirePosix();
65 $command = new Command();
67 ->params( [ 'ls', "$IP/index.php" ] )
69 $this->assertSame( "$IP/index.php", trim( $result->getStdout() ) );
71 $command = new Command();
73 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
76 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
79 public function testT69870() {
80 $commandLine = wfIsWindows()
82 ?
( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
83 : 'printf "%-333333s" "*"';
85 // Test several times because it involves a race condition that may randomly succeed or fail
86 for ( $i = 0; $i < 10; $i++
) {
87 $command = new Command();
88 $output = $command->unsafeParams( $commandLine )
91 $this->assertEquals( 333333, strlen( $output ) );