Skip to content

Commit

Permalink
Added code and test for Unicode File Converter Exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ramzerof committed Nov 9, 2013
1 parent d7eb478 commit 58a0c60
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
@@ -0,0 +1,93 @@
<?php

namespace TDDMicroExercises\PHP\TelemetrySystem;

class TelemetryClient
{
const DiagnosticMessage = "AT#UD";

private $_onlineStatus;
private $_diagnosticMessageJustSent = false;

/*private $_connectionEventsSimulator = new Random();
private $_randomMessageSimulator = new Random();*/

public function getOnlineStatus()
{
return $this->_onlineStatus;
}

public function connect( $telemetryServerConnectionString )
{
if( is_null($telemetryServerConnectionString) || empty($telemetryServerConnectionString) )
{
throw new \InvalidArgumentException();
}

// Fake the connection with 20% chances of success
//$success = rand(1, 10) <= 2;
$success = rand(1, 10) <= 2;
$this->_onlineStatus = $success;
}

public function disconnect()
{
$this->_onlineStatus = false;
}

public function send( $message )
{
if( is_null($message) || empty($message) ) {
throw new \InvalidArgumentException();
}

// The simulation of Send() actually just remember if the last message sent was a diagnostic message.
// This information will be used to simulate the Receive(). Indeed there is no real server listening.
if ( $message == TelemetryClient::DiagnosticMessage )
{
$this->_diagnosticMessageJustSent = true;

}
else
{
$this->_diagnosticMessageJustSent = false;
}

}

public function receive()
{
$message = '';

if($this->_diagnosticMessageJustSent)
{
// Simulate the reception of the diagnostic message
$message = "LAST TX rate................ 100 MBPS\r\n"
. "HIGHEST TX rate............. 100 MBPS\r\n"
. "LAST RX rate................ 100 MBPS\r\n"
. "HIGHEST RX rate............. 100 MBPS\r\n"
. "BIT RATE.................... 100000000\r\n"
. "WORD LEN.................... 16\r\n"
. "WORD/FRAME.................. 511\r\n"
. "BITS/FRAME.................. 8192\r\n"
. "MODULATION TYPE............. PCM/FM\r\n"
. "TX Digital Los.............. 0.75\r\n"
. "RX Digital Los.............. 0.10\r\n"
. "BEP Test.................... -5\r\n"
. "Local Rtrn Count............ 00\r\n"
. "Remote Rtrn Count........... 00";

$this->_diagnosticMessageJustSent = false;
}
else
{
$messageLength = rand( 50, 110 );
for($i = $messageLength; $i > 0; $i-- )
{
$message += chr( rand( 40, 126 ) ) ;
}
}

return $message;
}
}
@@ -0,0 +1,49 @@
<?php

namespace TDDMicroExercises\PHP\TelemetrySystem;

class TelemetryDiagnosticControls
{
const DiagnosticChannelConnectionString = "*111#";

private $_telemetryClient;
private $_diagnosticInfo = '';

public function __construct()
{
$this->_telemetryClient = new TelemetryClient();
}

public function getDiagnosticInfo()
{
return $this->_diagnosticInfo;
}

public function setDiagnosticInfo( $diagnosticInfo )
{
$this->_diagnosticInfo = $diagnosticInfo;
}

public function checkTransmission()
{
$this->_diagnosticInfo = '';
$this->_telemetryClient->disconnect();

$retryLeft = 3;
while ( $this->_telemetryClient->getOnlineStatus() == false && $retryLeft > 0 )
{
$this->_telemetryClient->connect(
TelemetryDiagnosticControls::DiagnosticChannelConnectionString
);
$retryLeft -= 1;
}

if($this->_telemetryClient->getOnlineStatus() == false)
{
throw new \Exception('Unable to connect.');
}

$this->_telemetryClient->send(TelemetryClient::DiagnosticMessage);
$this->_diagnosticInfo = $this->_telemetryClient->receive();
}
}
@@ -0,0 +1,15 @@
<?php

namespace TDDMicroExercises\PHP\TurnTicketDispenser;

class TicketDispenser
{

public function getTurnTicket()
{
$newTurnNumber = TurnNumberSequence::getNextTurnNumber();
$turnTicket = new TurnTicket($newTurnNumber);

return $turnTicket;
}
}
@@ -0,0 +1,13 @@
<?php

namespace TDDMicroExercises\PHP\TurnTicketDispenser;

class TurnNumberSequence
{
private static $_turnNumber = 0;

public static function getNextTurnNumber()
{
return self::$_turnNumber++;
}
}
@@ -0,0 +1,18 @@
<?php

namespace TDDMicroExercises\PHP\TurnTicketDispenser;

class TurnTicket
{
private $_turnNumber;

public function __construct($turnNumber)
{
$this->_turnNumber = $turnNumber;
}

public function getTurnNumber()
{
return $this->_turnNumber;
}
}

0 comments on commit 58a0c60

Please sign in to comment.