Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
Add support for zombie options (#183)
Browse files Browse the repository at this point in the history
Add support for ZombieJS options
  • Loading branch information
berliner authored and aik099 committed Jul 18, 2017
1 parent 7957999 commit af53345
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
3 changes: 2 additions & 1 deletion bin/mink-zombie-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var pointers = [];
var buffer = '';
var host = process.env.HOST || '127.0.0.1';
var port = process.env.PORT || 8124;
var options = process.env.OPTIONS ? JSON.parse(process.env.OPTIONS) : {};

var zombieVersionCompare = function (v2, op) {
var version_compare = function (v1, v2, operator) {
Expand Down Expand Up @@ -108,7 +109,7 @@ net.createServer(function (stream) {

stream.on('end', function () {
if (browser == null) {
browser = new zombie();
browser = new zombie(options);

// Clean up old pointers
pointers = [];
Expand Down
40 changes: 39 additions & 1 deletion src/NodeJS/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ abstract class Server
*/
protected $threshold;

/**
* @var array
*/
private $options;

/**
* @var string The full path to the NodeJS modules directory.
*/
Expand All @@ -69,14 +74,16 @@ abstract class Server
* @param string $serverPath Path to server script
* @param int $threshold Threshold value in micro seconds
* @param string $nodeModulesPath Path to node_modules directory
* @param array $options Options array for zombiejs
*/
public function __construct(
$host = '127.0.0.1',
$port = 8124,
$nodeBin = null,
$serverPath = null,
$threshold = 2000000,
$nodeModulesPath = ''
$nodeModulesPath = '',
$options = array()
) {
if (null === $nodeBin) {
$nodeBin = 'node';
Expand All @@ -97,6 +104,7 @@ public function __construct(

$this->setServerPath($serverPath);
$this->setThreshold($threshold);
$this->setOptions($options);
}

/**
Expand Down Expand Up @@ -273,6 +281,32 @@ public function getThreshold()
return $this->threshold;
}

/**
* Setter options value
*
* @param array $options Options array
*
* @throws \LogicException When server is already running.
*/
public function setOptions(array $options)
{
if ($this->isRunning()) {
throw new \LogicException('Unable to change options of a running server.');
}

$this->options = $options;
}

/**
* Getter options value
*
* @return array Options array
*/
public function getOptions()
{
return $this->options;
}

/**
* Getter process object
*
Expand Down Expand Up @@ -323,6 +357,10 @@ public function start(Process $process = null)
$processBuilder->setEnv('NODE_PATH', $this->nodeModulesPath);
}

if (!empty($this->options)) {
$processBuilder->setEnv('OPTIONS', json_encode($this->options));
}

$process = $processBuilder->getProcess();
}
$this->process = $process;
Expand Down
36 changes: 26 additions & 10 deletions tests/Custom/NodeJS/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ protected function doEvalJS(Connection $conn, $str, $returnType = 'js')
protected function getServerScript()
{
return <<<JS
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT;
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT
, options = process.env.OPTIONS ? JSON.parse(process.env.OPTIONS) : {};
JS;
}

Expand Down Expand Up @@ -58,11 +59,13 @@ public function testCreateServerWithDefaults()
$this->assertEquals('/path/to/server', $server->getServerPath());
$this->assertEquals(2000000, $server->getThreshold());
$this->assertEquals('', $server->getNodeModulesPath());
$this->assertEquals(array(), $server->getOptions());

$expected = <<<JS
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT;
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT
, options = process.env.OPTIONS ? JSON.parse(process.env.OPTIONS) : {};
JS;
$this->assertEquals($expected, $server->serverScript);
}
Expand Down Expand Up @@ -90,7 +93,8 @@ public function testCreateCustomServer()
null,
null,
5000000,
'../../'
'../../',
array('waitDuration' => '15s')
);

$this->assertEquals('123.123.123.123', $server->getHost());
Expand All @@ -99,11 +103,13 @@ public function testCreateCustomServer()
$this->assertEquals('/path/to/server', $server->getServerPath());
$this->assertEquals(5000000, $server->getThreshold());
$this->assertEquals('../../', $server->getNodeModulesPath());
$this->assertEquals(array('waitDuration' => '15s'), $server->getOptions());

$expected = <<<JS
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT;
var zombie = require('zombie')
, host = process.env.HOST
, port = process.env.PORT
, options = process.env.OPTIONS ? JSON.parse(process.env.OPTIONS) : {};
JS;
$this->assertEquals($expected, $server->serverScript);
}
Expand Down Expand Up @@ -158,6 +164,16 @@ public function testSetThresholdOnRunningServer()
$server->setThreshold('test');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Unable to change options of a running server.
*/
public function testSetOptionsOnRunningServer()
{
$server = $this->getRunningServer();
$server->setOptions(array('waitDuration' => '15s'));
}

/**
* @group legacy
*/
Expand Down

0 comments on commit af53345

Please sign in to comment.