MediaWiki REL1_30
CommandTest.php
Go to the documentation of this file.
1<?php
2
4use Wikimedia\TestingAccessWrapper;
5
9class CommandTest extends PHPUnit_Framework_TestCase {
10 private function requirePosix() {
11 if ( wfIsWindows() ) {
12 $this->markTestSkipped( 'This test requires a POSIX environment.' );
13 }
14 }
15
19 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
20 $this->requirePosix();
21
22 $command = new Command();
23 $result = $command
24 ->params( $commandInput )
25 ->execute();
26
27 $this->assertSame( $expectedExitCode, $result->getExitCode() );
28 $this->assertSame( $expectedOutput, $result->getStdout() );
29 }
30
31 public function provideExecute() {
32 return [
33 'success status' => [ 'true', 0, '' ],
34 'failure status' => [ 'false', 1, '' ],
35 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
36 ];
37 }
38
39 public function testEnvironment() {
40 $this->requirePosix();
41
42 $command = new Command();
43 $result = $command
44 ->params( [ 'printenv', 'FOO' ] )
45 ->environment( [ 'FOO' => 'bar' ] )
46 ->execute();
47 $this->assertSame( "bar\n", $result->getStdout() );
48 }
49
50 public function testOutput() {
51 global $IP;
52
53 $this->requirePosix();
54
55 $command = new Command();
56 $result = $command
57 ->params( [ 'ls', "$IP/index.php" ] )
58 ->execute();
59 $this->assertSame( "$IP/index.php", trim( $result->getStdout() ) );
60
61 $command = new Command();
62 $result = $command
63 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
64 ->includeStderr()
65 ->execute();
66 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
67 }
68
72 public function testNullsAreSkipped() {
73 $command = TestingAccessWrapper::newFromObject( new Command );
74 $command->params( 'echo', 'a', null, 'b' );
75 $command->unsafeParams( 'c', null, 'd' );
76 $this->assertEquals( "'echo' 'a' 'b' c d", $command->command );
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}
wfIsWindows()
Check if the operating system is Windows.
$command
Definition cdb.php:64
testNullsAreSkipped()
Test that null values are skipped by params() and unsafeParams()
testExecute( $commandInput, $expectedExitCode, $expectedOutput)
provideExecute
Class used for executing shell commands.
Definition Command.php:35
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2225
$IP
Definition update.php:3