Merge "FeedbackDialog: Improve alignment"
[lhc/web/wiklou.git] / tests / phpunit / includes / shell / CommandTest.php
1 <?php
2
3 use MediaWiki\Shell\Command;
4
5 /**
6 * @group Shell
7 */
8 class CommandTest extends PHPUnit_Framework_TestCase {
9 /**
10 * @expectedException PHPUnit_Framework_Error_Notice
11 */
12 public function testDestruct() {
13 if ( defined( 'HHVM_VERSION' ) ) {
14 $this->markTestSkipped( 'destructors are unreliable in HHVM' );
15 }
16 $command = new Command();
17 $command->params( 'true' );
18 }
19
20 private function requirePosix() {
21 if ( wfIsWindows() ) {
22 $this->markTestSkipped( 'This test requires a POSIX environment.' );
23 }
24 }
25
26 /**
27 * @dataProvider provideExecute
28 */
29 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
30 $this->requirePosix();
31
32 $command = new Command();
33 $result = $command
34 ->params( $commandInput )
35 ->execute();
36
37 $this->assertSame( $expectedExitCode, $result->getExitCode() );
38 $this->assertSame( $expectedOutput, $result->getStdout() );
39 }
40
41 public function provideExecute() {
42 return [
43 'success status' => [ 'true', 0, '' ],
44 'failure status' => [ 'false', 1, '' ],
45 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
46 ];
47 }
48
49 public function testEnvironment() {
50 $this->requirePosix();
51
52 $command = new Command();
53 $result = $command
54 ->params( [ 'printenv', 'FOO' ] )
55 ->environment( [ 'FOO' => 'bar' ] )
56 ->execute();
57 $this->assertSame( "bar\n", $result->getStdout() );
58 }
59
60 public function testOutput() {
61 global $IP;
62
63 $this->requirePosix();
64
65 $command = new Command();
66 $result = $command
67 ->params( [ 'ls', "$IP/index.php" ] )
68 ->execute();
69 $this->assertSame( "$IP/index.php", trim( $result->getStdout() ) );
70
71 $command = new Command();
72 $result = $command
73 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
74 ->includeStderr()
75 ->execute();
76 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
77 }
78
79 public function testT69870() {
80 $commandLine = wfIsWindows()
81 // 333 = 331 + CRLF
82 ? ( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
83 : 'printf "%-333333s" "*"';
84
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 )
89 ->execute()
90 ->getStdout();
91 $this->assertEquals( 333333, strlen( $output ) );
92 }
93 }
94 }