Merge "Skin: Make skins aware of their registered skin name"
[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 testStdout() {
61 $this->requirePosix();
62
63 $command = new Command();
64
65 $result = $command
66 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
67 ->execute();
68
69 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
70 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
71 }
72
73 public function testStdoutRedirection() {
74 $this->requirePosix();
75
76 $command = new Command();
77
78 $result = $command
79 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
80 ->includeStderr( true )
81 ->execute();
82
83 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
84 $this->assertNull( $result->getStderr() );
85 }
86
87 public function testOutput() {
88 global $IP;
89
90 $this->requirePosix();
91 chdir( $IP );
92
93 $command = new Command();
94 $result = $command
95 ->params( [ 'ls', 'index.php' ] )
96 ->execute();
97 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
98 $this->assertSame( null, $result->getStderr() );
99
100 $command = new Command();
101 $result = $command
102 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
103 ->includeStderr()
104 ->execute();
105 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
106 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
107 $this->assertSame( null, $result->getStderr() );
108
109 $command = new Command();
110 $result = $command
111 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
112 ->execute();
113 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
114 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
115 }
116
117 public function testT69870() {
118 $commandLine = wfIsWindows()
119 // 333 = 331 + CRLF
120 ? ( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
121 : 'printf "%-333333s" "*"';
122
123 // Test several times because it involves a race condition that may randomly succeed or fail
124 for ( $i = 0; $i < 10; $i++ ) {
125 $command = new Command();
126 $output = $command->unsafeParams( $commandLine )
127 ->execute()
128 ->getStdout();
129 $this->assertEquals( 333333, strlen( $output ) );
130 }
131 }
132 }