/** * @param CM_Cli_Command $commandMock * @param string|null $errorMessageExpected * @return PHPUnit_Framework_MockObject_MockObject */ protected function _getCommandManagerMock($commandMock, $errorMessageExpected = null) { $keepAliveExpected = $commandMock->getKeepalive(); $processMock = $this->_getProcessMock($keepAliveExpected); $mockBuilder = $this->getMockBuilder('CM_Cli_CommandManager'); $mockBuilder->setMethods(['getCommands', '_getProcess', '_findLock', '_lockCommand', 'unlockCommand', '_outputError', '_checkUnusedArguments']); $commandManagerMock = $mockBuilder->getMock(); $commandManagerMock->expects($this->any())->method('getCommands')->will($this->returnValue(array($commandMock))); if (null === $errorMessageExpected) { $commandManagerMock->expects($this->never())->method('_outputError'); } else { $commandManagerMock->expects($this->once())->method('_outputError')->with($errorMessageExpected); } $commandManagerMock->expects($this->any())->method('_getProcess')->will($this->returnValue($processMock)); $commandManagerMock->expects($this->any())->method('_checkUnusedArguments'); return $commandManagerMock; }
/** * @param string $transactionName * @param CM_Cli_Command $command * @param CM_Cli_Arguments $arguments * @param CM_InputStream_Interface $streamInput * @param CM_OutputStream_Interface $streamOutput * @param CM_OutputStream_Interface $streamError * @return callable */ protected function _getProcessWorkload($transactionName, CM_Cli_Command $command, CM_Cli_Arguments $arguments, CM_InputStream_Interface $streamInput, CM_OutputStream_Interface $streamOutput, CM_OutputStream_Interface $streamError) { return function (CM_Process_WorkloadResult $result) use($transactionName, $command, $arguments, $streamInput, $streamOutput, $streamError) { try { $parameters = $command->extractParameters($arguments); $this->_checkUnusedArguments($arguments); if ($command->getKeepalive()) { CM_Service_Manager::getInstance()->getNewrelic()->ignoreTransaction(); } else { CM_Service_Manager::getInstance()->getNewrelic()->startTransaction($transactionName); } $command->run($parameters, $streamInput, $streamOutput, $streamError); } catch (CM_Cli_Exception_InvalidArguments $ex) { $this->_outputError('ERROR: ' . $ex->getMessage() . PHP_EOL); if (isset($command)) { $this->_outputError('Usage: ' . $arguments->getScriptName() . ' ' . $command->getHelp()); } else { $this->_outputError($this->getHelp()); } $result->setException($ex); } catch (CM_Cli_Exception_Internal $ex) { $this->_outputError('ERROR: ' . $ex->getMessage() . PHP_EOL); $result->setException($ex); } }; }