Skip to content

Instantly share code, notes, and snippets.

@GeeH
Created April 23, 2013 10:53
Show Gist options
  • Save GeeH/5442628 to your computer and use it in GitHub Desktop.
Save GeeH/5442628 to your computer and use it in GitHub Desktop.
How the hell do you unit test this???
<?php
/**
* Gary Hockin
* 23/04/2013
*/
namespace FAFSServer\Service;
class FAFSServer
{
/**
* @var array
*/
protected $config;
/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
public function start()
{
// get config values (or use some sane defaults)
$ip = isset($this->config['ip']) ? $this->config['ip'] : '127.0.0.1';
$port = isset($this->config['port']) ? $this->config['port'] : '6610';
$length = isset($this->config['maxLength']) ? $this->config['maxLength'] : 1024;
// Create socket server
$socket = stream_socket_server("udp://$ip:$port", $errorNo, $errorMessage, STREAM_SERVER_BIND);
if ($errorNo || $errorMessage || get_resource_type($socket) !== 'stream') {
throw new \Exception("Error creating server: $errorMessage", $errorNo);
}
// Loop through data as it's received
while ($data = stream_socket_recvfrom($socket, $length, null, $receivedFrom)) {
echo "$data \r\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment