/** * Constructor * * @param mixed $Command * Command to run. * @param IConfig $Config * Command configuration. * * <ul> * <li><b>Timeout</b>: <i>int</i> Timeout to wait while receiving input and output.</li> * <li><b>After</b>: <i>callable</i> Callback to call before run starts. (Not supported by most commands).</li> * <li><b>Before</b>: <i>callable</i> Callback to call after run has completed. (Not supported by most commands).</li> * <li><b>Description</b>: <i>string</i> Description of command.</li> * </ul> * * @param IMediator $Mediator * Mediator for command. * @param string $ID * Command Name / Label */ public function __construct($Command, IConfig $Config, IMediator $Mediator = null, $ID = null) { // Make closures serializable if ($Command instanceof \Closure) { $Command = new SerializableClosure($Command); } if (is_null($ID)) { $ID = $this->createID(); } // Is $Command callable? if (!is_callable($Command)) { throw new InvalidArgumentException(0); } elseif (!$Config->offsetExists('Timeout')) { throw new InvalidArgumentException(1, '%header% $Config[Timeout] is not set'); } // Set properties $this->_Config = $Config; $this->_Command = $Command; $this->_DataMapper = new ArrayObject(); // ID $this->ID = $ID; // Mediator if ($Mediator) { $this->setMediator($Mediator); } }
/** * @covers ::append */ public function test_append() { # Test valid $Test = new ArrayObject(array('foo' => 1, 'bar' => 1, 'object' => new ArrayObject(array('foo' => 1)))); $this->Config->append($Test); $this->assertEquals($Test, $this->Config[0], 'IConfig[0] should equal $Test'); $this->Config->append(1); $this->assertEquals(1, $this->Config[1], 'IConfig[test] should equal 1'); $this->Config->append('foo'); $this->assertEquals('foo', $this->Config[2], 'IConfig[test] should equal `foo`'); # Test invalid try { $this->Config->append(array()); $this->fail('Unable to generate exception with invalid value'); } catch (\UnexpectedValueException $e) { $this->assertContains('Instance of ArrayAccess expected', $e->getMessage(), 'Invalid exception: ' . $e->getMessage()); } }
/** * Constructor * * @param mixed $Command * Command to run. * @param IConfig $Config * Command configuration. * * <ul> * <li><b>Timeout</b>: <i>int</i> Timeout to wait while receiving input and output.</li> * <li><b>After</b>: <i>callable</i> Callback to call before run starts. (Not supported by most commands).</li> * <li><b>Before</b>: <i>callable</i> Callback to call after run has completed. (Not supported by most commands).</li> * <li><b>Description</b>: <i>string</i> Description of command.</li> * </ul> * * @param IMediator $Mediator * Mediator for command. * @param string $ID * Command Name / Label. */ public function __construct($Command, IConfig $Config = null, IMediator $Mediator = null, $ID = null) { // Set properties $this->_Command = $Command; $this->_Config = $Config ?: new Config(self::$_DefaultConfig + array()); // CheckConfig if (!$Config->offsetExists('Timeout')) { throw new InvalidArgumentException(1, '%header% $Config[Timeout] is not set'); } // Mediator if ($Mediator) { $this->setMediator($Mediator); } // ID $this->ID = is_null($ID) ? $this->createID() : $ID; // DataMapper $this->_DataMapper = new ArrayObject(); }
/** * Constructor * * @param mixed $Command * Command to run. * @param IConfig $Config * Command configuration. * * <ul> * <li><b>Timeout</b>: <i>int</i> Timeout in seconds for a response from shellcommand.</li> * <li><b>CWD</b>: <i>string</i> Path to directory to use as native path of shellcommand.</li> * <li><b>Enviroment</b>: <i>array</i> Enviroment variables.</li> * <li><b>Extras</b>: <i>array</i> Extras passed to <code>proc_open()</code>.</li> * </ul> * * @param IMediator $Mediator * Mediator for command. * @param string $ID * Command Name / Label. */ public function __construct($Command, IConfig $Config, IMediator $Mediator = null, $ID = null) { // Check proc_open and proc_close if (!is_callable('proc_open') || !is_callable('proc_close')) { // @codeCoverageIgnoreStart throw new CommandException('Unable to create shell command. Either proc_open() or proc_close() have been disabled.'); // @codeCoverageIgnoreEnd // CheckConfig } elseif (!$Config->offsetExists('Timeout')) { throw new InvalidArgumentException(1, '%header% $Config[Timeout] is not set'); } elseif (!$Config->offsetExists('CWD')) { throw new InvalidArgumentException(1, '%header% $Config[CWD] is not set'); } elseif (!$Config->offsetExists('Environment')) { throw new InvalidArgumentException(1, '%header% $Config[Enviroment] is not set'); } elseif (!$Config->offsetExists('Extras')) { throw new InvalidArgumentException(1, '%header% $Config[Extras] is not set'); } elseif (!is_array($Config['Extras'])) { throw new InvalidArgumentException(1, '%header% $Config[Extras] is not an array'); } // Set properties $this->_Command = strval($Command); $this->_Config = $Config; // Mediator if ($Mediator) { $this->setMediator($Mediator); } // ID $this->ID = is_null($ID) ? $this->createID() : $ID; // DataMapper $this->_DataMapper = new ArrayObject(); }
/** * Constructor * * @see \BLW\Model\Command\Shell Command\Shell() * * @throws \BLW\Model\InvalidArgumentException If <code>$Configuration</code> is not readable. * @throws \RuntimeException If there is an error starting PhantomJS. * * @param \BLW\Type\IFile $Executable * Path to PhantomJS executable. * @param \BLW\Type\IFile $ConfigFile * Path to PhantomJS configuration JSON file. * @param \BLW\Type\IConfig $Config * Engine configuration: * * <ul> * <li><b>Timeout</b>: <i>int</i> Communication timeout between PHP and PhantomJS process.</li> * <li><b>CWD</b>: <i>string</i> Current working directory for PhantomJS. <code>null</code> for current working directory.</li> * <li><b>Enviroment</b>: <i>array</i> Enviroment variables passed to PhantomJS. <code>null</code> for PHP enviroment variables.</li> * <li><b>Extras</b>: <i>array</i> Extra parameters passed to proc_open()</li> * </ul> */ public function __construct(IFile $Executable, IFile $ConfigFile, IConfig $Config) { // Parent constructor parent::__construct(null, 'PhantomJS'); switch (true) { // Validate ConfigFile case !$ConfigFile->isReadable(): throw new InvalidArgumentException(1); // Validate $Config // Validate $Config case !isset($Config['Timeout']): case !$Config->offsetExists('CWD'): case !$Config->offsetExists('Environment'): case !$Config->offsetExists('Extras'): throw new InvalidArgumentException(2); // All okay // All okay default: // Properties $this->_Executable = $Executable; $this->_ConfigFile = $ConfigFile; $this->_Config = $Config; $this->_Mediator = new Mediator(); // Start PhantomJS process if (!$this->phantomStart() || !$this->_Process->getStatus('running')) { // @codeCoverageIgnoreStart throw new RuntimeException('Unable to start PhantomJS'); // @codeCoverageIgnoreEnd } } }