Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShellTest.php
Go to the documentation of this file.
1 <?php
8 
9 class ShellTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $commandRenderer;
15 
19  protected $logger;
20 
21  protected function setUp()
22  {
23  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
24  ->disableOriginalConstructor()
25  ->getMock();
26  $this->commandRenderer = new \Magento\Framework\Shell\CommandRenderer();
27  }
28 
37  protected function _testExecuteCommand(\Magento\Framework\Shell $shell, $command, $commandArgs, $expectedResult)
38  {
39  $this->expectOutputString('');
40  // nothing is expected to be ever printed to the standard output
41  $actualResult = $shell->execute($command, $commandArgs);
42  $this->assertEquals($expectedResult, $actualResult);
43  }
44 
51  public function testExecute($command, $commandArgs, $expectedResult)
52  {
53  $this->_testExecuteCommand(
54  new \Magento\Framework\Shell($this->commandRenderer, $this->logger),
55  $command,
56  $commandArgs,
57  $expectedResult
58  );
59  }
60 
68  public function testExecuteLog($command, $commandArgs, $expectedResult, $expectedLogRecords)
69  {
70  $quoteChar = substr(escapeshellarg(' '), 0, 1);
71  // environment-dependent quote character
72  foreach ($expectedLogRecords as $logRecordIndex => $expectedLogMessage) {
73  $expectedLogMessage = str_replace('`', $quoteChar, $expectedLogMessage);
74  $this->logger->expects($this->at($logRecordIndex))
75  ->method('info')
76  ->with($expectedLogMessage);
77  }
78  $this->_testExecuteCommand(
79  new \Magento\Framework\Shell($this->commandRenderer, $this->logger),
80  $command,
81  $commandArgs,
82  $expectedResult
83  );
84  }
85 
89  public function executeDataProvider()
90  {
91  // backtick symbol (`) has to be replaced with environment-dependent quote character
92  return [
93  'STDOUT' => ['php -r %s', ['echo 27181;'], '27181', ['php -r `echo 27181;` 2>&1', '27181']],
94  'STDERR' => [
95  'php -r %s',
96  ['fwrite(STDERR, 27182);'],
97  '27182',
98  ['php -r `fwrite(STDERR, 27182);` 2>&1', '27182'],
99  ],
100  'piping STDERR -> STDOUT' => [
101  // intentionally no spaces around the pipe symbol
102  'php -r %s|php -r %s',
103  ['fwrite(STDERR, 27183);', 'echo fgets(STDIN);'],
104  '27183',
105  ['php -r `fwrite(STDERR, 27183);` 2>&1|php -r `echo fgets(STDIN);` 2>&1', '27183'],
106  ],
107  'piping STDERR -> STDERR' => [
108  'php -r %s | php -r %s',
109  ['fwrite(STDERR, 27184);', 'fwrite(STDERR, fgets(STDIN));'],
110  '27184',
111  ['php -r `fwrite(STDERR, 27184);` 2>&1 | php -r `fwrite(STDERR, fgets(STDIN));` 2>&1', '27184'],
112  ]
113  ];
114  }
115 
121  public function testExecuteFailure()
122  {
123  $shell = new \Magento\Framework\Shell($this->commandRenderer, $this->logger);
124  $shell->execute('non_existing_command');
125  }
126 
133  public function testExecuteFailureDetails($command, $commandArgs, $expectedError)
134  {
135  try {
136  /* Force command to return non-zero exit code */
137  $commandArgs[count($commandArgs) - 1] .= ' exit(42);';
138  $this->testExecute($command, $commandArgs, ''); // no result is expected in a case of a command failure
139  } catch (\Magento\Framework\Exception\LocalizedException $e) {
140  $this->assertInstanceOf('Exception', $e->getPrevious());
141  $this->assertEquals($expectedError, $e->getPrevious()->getMessage());
142  $this->assertEquals(42, $e->getPrevious()->getCode());
143  }
144  }
145 }
_testExecuteCommand(\Magento\Framework\Shell $shell, $command, $commandArgs, $expectedResult)
Definition: ShellTest.php:37
if(!file_exists($installConfigFile)) if(!defined('TESTS_INSTALLATION_DB_CONFIG_FILE')) $shell
Definition: bootstrap.php:46
testExecute($command, $commandArgs, $expectedResult)
Definition: ShellTest.php:51
testExecuteFailureDetails($command, $commandArgs, $expectedError)
Definition: ShellTest.php:133
testExecuteLog($command, $commandArgs, $expectedResult, $expectedLogRecords)
Definition: ShellTest.php:68