Created
October 29, 2013 15:21
-
-
Save mageekguy/7216691 to your computer and use it in GitHub Desktop.
Bobo la tête !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$this | |
->assert('Server should throw an exception is it can not create its socket') | |
->given( | |
$server = new testedclass(), | |
$infoLogger = new \mock\aixia\ka\at\server\logger(), | |
$this->calling($infoLogger)->log->doesNothing(), | |
$socketTagger = new \mock\aixia\ka\socket\tagger(), | |
$server | |
->setSocketTagger($socketTagger) | |
->setInfoLogger($infoLogger) | |
, | |
$this->function->socket_create = false, | |
$this->function->socket_last_error = $errorCode = rand(1, PHP_INT_MAX), | |
$this->function->socket_strerror = $errorMessage = uniqid(), | |
$this->function->socket_close = function() {} | |
) | |
->then | |
->exception(function() use ($server) { $server->start(uniqid(), rand(1, PHP_INT_MAX)); }) | |
->isInstanceOf('aixia\ka\at\server\exception') | |
->hasMessage('Unable to start server: ' . $errorMessage) | |
->mock($infoLogger)->call('log')->withArguments('Unable to start server: ' . $errorMessage)->once() | |
->function('socket_close')->wasCalled()->never() | |
->assert('Server should throw an exception is it can not bind its socket') | |
->given( | |
$this->function->socket_create = $socket = 'Server socket', | |
$this->function->socket_bind = false, | |
$this->function->socket_last_error = $errorCode = rand(1, PHP_INT_MAX), | |
$this->function->socket_strerror = $errorMessage = uniqid() | |
) | |
->then | |
->exception(function() use ($server, & $ip, & $port) { $server->start($ip = uniqid(), $port = rand(1, PHP_INT_MAX)); }) | |
->isInstanceOf('aixia\ka\at\server\exception') | |
->hasMessage('Unable to bind a socket to \'' . $ip . ':' . $port . '\': ' . $errorMessage) | |
->mock($infoLogger)->call('log')->withArguments('Unable to bind a socket to \'' . $ip . ':' . $port . '\': ' . $errorMessage)->once() | |
->function('socket_close')->wasCalledWithArguments($socket)->once() | |
->assert('Server should throw an exception is it can not listen to its socket') | |
->given( | |
$this->function->socket_bind = true, | |
$this->function->socket_listen = false | |
) | |
->then | |
->exception(function() use ($server, & $ip, & $port) { $server->start($ip = uniqid(), $port = rand(1, PHP_INT_MAX)); }) | |
->isInstanceOf('aixia\ka\at\server\exception') | |
->hasMessage('Unable to listen to \'' . $ip . ':' . $port . '\': ' . $errorMessage) | |
->mock($infoLogger)->call('log')->withArguments('Unable to listen to \'' . $ip . ':' . $port . '\': ' . $errorMessage)->once() | |
->function('socket_listen') | |
->wasCalledWithArguments($socket) | |
->before( | |
$this->function('socket_close')->wasCalledWithArguments($socket)->once() | |
)->once() | |
->assert('Server should do nothing if there is no client') | |
->given( | |
$this->function->socket_listen = true, | |
$this->function->socket_select = 0, | |
$this->calling($socketTagger)->getTag = $serverSocketTag = uniqid(), | |
$clientFactory = new \mock\aixia\ka\at\server\client\factory(), | |
$server->setClientFactory($clientFactory), | |
$controller = new \mock\aixia\ka\at\server\controller(), | |
$this->calling($controller)->serverShouldRun = true, | |
$this->calling($controller)->serverShouldRun[2] = false, | |
$server->setController($controller) | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($controller)->call('serverShouldRun')->twice() | |
->function('socket_listen')->wasCalledWithArguments($socket) | |
->before( | |
$this->function('socket_select')->wasCalledWithArguments(array($serverSocketTag => $socket), array($serverSocketTag => $socket), array($serverSocketTag => $socket), null, null)->once() | |
)->once() | |
->mock($clientFactory)->call('build')->never() | |
->function('socket_close')->wasCalledWithArguments($socket) | |
->after($this->function('socket_select')) | |
->once() | |
->assert('Server should read socket if a client knocking on the door') | |
->given( | |
$this->function->socket_accept = false, | |
$this->function->socket_select = function(& $read, & $write, & $except) use ($serverSocketTag, $socket) { $read = array($serverSocketTag => $socket); return 1; } | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($controller)->call('serverShouldRun')->twice() | |
->function('socket_listen')->wasCalledWithArguments($socket) | |
->before( | |
$this->function('socket_select')->wasCalledWithArguments(array($serverSocketTag => $socket), array($serverSocketTag => $socket), array($serverSocketTag => $socket), null, null) | |
->before( | |
$this->function('socket_accept')->wasCalledWithArguments($socket)->once() | |
)->once() | |
)->once() | |
->mock($clientFactory)->call('build')->never() | |
->function('socket_close')->wasCalledWithArguments($socket) | |
->after($this->function('socket_select')) | |
->once() | |
->assert('Server should build a client then socket accept connection') | |
->if( | |
$this->calling($controller)->serverShouldRun[2] = true, | |
$this->calling($controller)->serverShouldRun[3] = false, | |
$this->calling($socketTagger)->getTag[1] = $serverSocketTag, | |
$this->calling($socketTagger)->getTag[2] = $client1SocketTag = uniqid(), | |
$this->calling($socketTagger)->getTag[3] = $client2SocketTag = uniqid(), | |
$this->function->socket_accept[1] = $client1Socket = uniqid(), | |
$this->function->socket_accept[2] = $client2Socket = uniqid(), | |
$this->function->socket_accept[3] = false, | |
$this->nextMockedMethod | |
->closeConnection | |
->canNotCallItsParent() | |
->doesNothing(), | |
$this->calling($clientFactory)->build[1] = $client1 = new \mock\aixia\ka\at\server\client(uniqid()), | |
$this->calling($clientFactory)->build[2] = $client2 = new \mock\aixia\ka\at\server\client(uniqid()), | |
$this->nextMockedMethod->__construct->canHaveNoArgument(), | |
$this->calling($client1)->getNetworkName = $client1NetworkName = uniqid(), | |
$this->calling($client1)->getHeartbeat = $client1Heartbeat = new \mock\aixia\ka\at\command\request\heartbeat(), | |
$this->calling($client2)->getNetworkName = $client2NetworkName = uniqid(), | |
$this->calling($client2)->getHeartbeat = $client2Heartbeat = new \mock\aixia\ka\at\command\request\heartbeat() | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($controller)->call('serverShouldRun')->thrice() | |
->function('socket_accept')->wasCalledWithArguments($socket)->twice() | |
->mock($clientFactory) | |
->call('build') | |
->withArguments($client1Socket)->once() | |
->withArguments($client2Socket)->once() | |
->assert('Server should authenticate clients') | |
->if( | |
$this->calling($controller)->serverShouldRun[3] = true, | |
$this->calling($controller)->serverShouldRun[5] = false, | |
$this->function->socket_select[1] = function(& $read, & $write, & $except) use ($serverSocketTag, $socket) { $read = array($serverSocketTag => $socket); return 1; }, | |
$this->function->socket_select[2] = function(& $read, & $write, & $except) use ($client1SocketTag, $client1Socket) { $read = array($client1SocketTag => $client1Socket); return 1; }, | |
$this->function->socket_select[3] = function(& $read, & $write, & $except) use ($serverSocketTag, $socket) { $read = array($serverSocketTag => $socket); return 1; }, | |
$this->function->socket_select[4] = function(& $read, & $write, & $except) use ($client2SocketTag, $client2Socket) { $read = array($client2SocketTag => $client2Socket); return 1; } | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($controller)->call('serverShouldRun')->exactly(5) | |
->function('socket_accept')->wasCalledWithArguments($socket)->twice() | |
->mock($clientFactory) | |
->call('build') | |
->withArguments($client1Socket)->once() | |
->withArguments($client2Socket)->once() | |
->mock($client1) | |
->call('getHeartbeat') | |
->before($this->mock($client1)->call('acknowledge')->withArguments($client1Heartbeat)->once()) | |
->once() | |
->call('closeConnection') | |
->before($this->function('socket_close')->wasCalledWithArguments($socket)) | |
->once() | |
->mock($client2) | |
->call('getHeartbeat') | |
->before($this->mock($client2)->call('acknowledge')->withArguments($client2Heartbeat)->once()) | |
->once() | |
->call('closeConnection') | |
->before($this->function('socket_close')->wasCalledWithArguments($socket)) | |
->once() | |
->function('socket_close')->wasCalledWithArguments($socket)->once() | |
->mock($infoLogger)->call('log') | |
->withArguments('Client ' . $client1NetworkName . ' knocks on the door')->once() | |
->withArguments('Client ' . $client2NetworkName . ' knocks on the door')->once() | |
->assert('Server should log invalid client') | |
->if( | |
$this->calling($client1)->getHeartbeat->throw = new \exception($exceptionMessage = uniqid()) | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($client1) | |
->call('getHeartbeat') | |
->once() | |
->call('acknowledge')->never() | |
->mock($client2) | |
->call('getHeartbeat') | |
->before($this->mock($client2)->call('acknowledge')->withArguments($client2Heartbeat)->once()) | |
->once() | |
->mock($infoLogger)->call('log')->withArguments('Unable to get valid heartbeat from ' . $client1NetworkName . ', kick it!')->once() | |
->assert('Server should get clients response') | |
->if( | |
$this->calling($client1)->getModemId[1] = null, | |
$this->calling($client1)->getModemId = uniqid(), | |
$this->calling($client1)->getResponse = null, | |
$this->calling($client1)->getResponse[2] = $client1Response = new command\response(uniqid()), | |
$this->calling($client2)->getModemId[1] = null, | |
$this->calling($client2)->getModemId = uniqid(), | |
$this->calling($client2)->getResponse = null, | |
$this->calling($client2)->getResponse[2] = $client2Response = new command\response(uniqid()), | |
$this->calling($controller)->serverShouldRun[5] = true, | |
$this->calling($controller)->serverShouldRun[8] = false, | |
$this->function->socket_select[5] = function(& $read, & $write, & $except) use ($client1SocketTag, $client1Socket) { $read = array($client1SocketTag => $client1Socket); return 1; }, | |
$this->function->socket_select[6] = function(& $read, & $write, & $except) use ($client1SocketTag, $client1Socket, $client2SocketTag, $client2Socket) { $read = array($client1SocketTag => $client1Socket, $client2SocketTag => $client2Socket); return 1; }, | |
$this->function->socket_select[7] = function(& $read, & $write, & $except) use ($client2SocketTag, $client2Socket) { $read = array($client2SocketTag => $client2Socket); return 1; } | |
) | |
->then | |
->object($server->start(uniqid(), rand(1, PHP_INT_MAX)))->isIdenticalTo($server) | |
->mock($client1) | |
->call('getResponse') | |
->twice() | |
->mock($client2) | |
->call('getResponse') | |
->twice() | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment