Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added code and test for Unicode File Converter Exercise
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
....YoursSolutions/PHP-frubini/src/TDDMicroExercises/PHP/TelemetrySystem/TelemetryClient.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ons/PHP-frubini/src/TDDMicroExercises/PHP/TelemetrySystem/TelemetryDiagnosticControls.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...rsSolutions/PHP-frubini/src/TDDMicroExercises/PHP/TurnTicketDispenser/TicketDispenser.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TurnTicketDispenser; | ||
|
||
class TicketDispenser | ||
{ | ||
|
||
public function getTurnTicket() | ||
{ | ||
$newTurnNumber = TurnNumberSequence::getNextTurnNumber(); | ||
$turnTicket = new TurnTicket($newTurnNumber); | ||
|
||
return $turnTicket; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...olutions/PHP-frubini/src/TDDMicroExercises/PHP/TurnTicketDispenser/TurnNumberSequence.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TurnTicketDispenser; | ||
|
||
class TurnNumberSequence | ||
{ | ||
private static $_turnNumber = 0; | ||
|
||
public static function getNextTurnNumber() | ||
{ | ||
return self::$_turnNumber++; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...s.YoursSolutions/PHP-frubini/src/TDDMicroExercises/PHP/TurnTicketDispenser/TurnTicket.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |