Rename ModbusSocket to ModbusConnection, support php5.4

pull/2/head
toimtoimtoim 8 years ago
parent 2fbbbc5842
commit 95ecf4bf5b
  1. 2
      composer.json
  2. 11
      src/IOException.php
  3. 45
      src/ModbusMaster.php
  4. 12
      src/Network/IOException.php
  5. 193
      src/Network/ModbusConnection.php
  6. 111
      src/Network/ModbusConnectionBuilder.php
  7. 120
      src/Network/ModbusConnectionProperties.php
  8. 31
      tests/ModbusMaster/ModbusExceptionTest.php
  9. 14
      tests/PhpType/Bytes2MixedTest.php
  10. 8
      tests/PhpType/Bytes2RealTest.php
  11. 14
      tests/PhpType/Bytes2SignedIntTest.php
  12. 6
      tests/PhpType/Bytes2StringTest.php
  13. 14
      tests/PhpType/Bytes2UnSignedIntTest.php
  14. 14
      tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php
  15. 24
      tests/PhpType/PhpTypeArraySizeExceptionsTest.php

@ -4,7 +4,7 @@
"description": "PhpModbus with namespaces and updated to PHP 7", "description": "PhpModbus with namespaces and updated to PHP 7",
"license": "LGPL", "license": "LGPL",
"require": { "require": {
"php": "^5.5 || ^7.0" "php": "^5.4 || ^7.0"
}, },
"require-dev": { "require-dev": {
"react/socket": "~0.4.0", "react/socket": "~0.4.0",

@ -1,11 +0,0 @@
<?php
namespace PHPModbus;
/**
* Exception class thrown when a socket operation failure happens.
*/
class IOException extends \RuntimeException
{
}

@ -3,6 +3,7 @@
namespace PHPModbus; namespace PHPModbus;
use Exception; use Exception;
use PHPModbus\Network\ModbusConnection;
use PHPModbus\Packet\MaskWriteRegisterPacket; use PHPModbus\Packet\MaskWriteRegisterPacket;
use PHPModbus\Packet\ReadCoilsPacket; use PHPModbus\Packet\ReadCoilsPacket;
use PHPModbus\Packet\ReadInputDiscretesPacket; use PHPModbus\Packet\ReadInputDiscretesPacket;
@ -464,37 +465,45 @@ class ModbusMaster
public function sendAndReceive(callable $buildRequest, callable $parseResponse) public function sendAndReceive(callable $buildRequest, callable $parseResponse)
{ {
$connection = ModbusConnection::getBuilder()
->setHost($this->host)
->setPort($this->port)
->setProtocol($this->socket_protocol)
->setClient($this->client)
->setClientPort($this->client_port)
->setTimeoutSec($this->timeout_sec)
->setReadTimeoutSec($this->socket_read_timeout_sec)
->setWriteTimeoutSec($this->socket_write_timeout_sec)
->setConnectTimeoutSec($this->socket_connect_timeout_sec)
->build();
try { try {
$socket = ModbusSocket::getBuilder() $connection->connect();
->setHost($this->host)
->setPort($this->port)
->setSocketProtocol($this->socket_protocol)
->setClient($this->client)
->setClientPort($this->client_port)
->setTimeoutSec($this->timeout_sec)
->setSocketReadTimeoutSec($this->socket_read_timeout_sec)
->setSocketWriteTimeoutSec($this->socket_write_timeout_sec)
->setSocketConnectTimeoutSec($this->socket_connect_timeout_sec)
->build();
$socket->connect();
$packet = $buildRequest(); $packet = $buildRequest();
$this->status .= 'Sending ' . $this->printPacket($packet); $this->status .= 'Sending ' . $this->printPacket($packet);
$socket->send($packet); $connection->send($packet);
$data = $socket->receive(); $data = $connection->receive();
$this->status .= 'Received ' . $this->printPacket($data); $this->status .= 'Received ' . $this->printPacket($data);
$this->validateResponseCode($data); $this->validateResponseCode($data);
$this->closeConnection($connection);
return $parseResponse($data); return $parseResponse($data);
} finally { } catch (Exception $e) {
$this->status .= implode("\n", $socket->getStatusMessages()); $this->closeConnection($connection);
$socket->close(); throw $e;
} }
} }
private function closeConnection(ModbusConnection $connection)
{
$this->status .= implode("\n", $connection->getStatusMessages());
$connection->close();
}
/** /**
* fc15 * fc15
* *

@ -0,0 +1,12 @@
<?php
namespace PHPModbus\Network;
use PHPModbus\ModbusException;
/**
* Exception class thrown when a modbus network connection operation failure happens.
*/
class IOException extends ModbusException
{
}

@ -12,51 +12,12 @@
* @package Phpmodbus * @package Phpmodbus
* @version $id$ * @version $id$
*/ */
namespace PHPModbus\Network;
namespace PHPModbus;
use InvalidArgumentException; use InvalidArgumentException;
class ModbusConnection extends ModbusConnectionProperties
class ModbusSocket
{ {
/**
* @var string (optional) client IP address when binding client
*/
protected $client = '';
/**
* @var string client port set when binding client to local ip&port
*/
protected $client_port = 502;
/**
* @var float Total response timeout (seconds, decimals allowed)
*/
protected $timeout_sec = 5;
/**
* @var float Socket connect timeout (seconds, decimals allowed)
*/
protected $socket_connect_timeout_sec = 1;
/**
* @var float Socket read timeout (seconds, decimals allowed)
*/
protected $socket_read_timeout_sec = 0.3; // 300 ms
/**
* @var float Socket write timeout (seconds, decimals allowed)
*/
protected $socket_write_timeout_sec = 1;
/**
* @var string Socket protocol (TCP, UDP)
*/
protected $socket_protocol = 'UDP';
/**
* @var string Modbus device IP address
*/
protected $host = '192.168.1.1';
/**
* @var string gateway port
*/
protected $port = 502;
/** /**
* @var resource Communication socket * @var resource Communication socket
*/ */
@ -66,9 +27,23 @@ class ModbusSocket
*/ */
protected $statusMessages = []; protected $statusMessages = [];
public function __construct(ModbusConnectionBuilder $builder)
{
$this->host = $builder->getHost();
$this->port = $builder->getPort();
$this->client = $builder->getClient();
$this->clientPort = $builder->getClientPort();
$this->timeoutSec = $builder->getTimeoutSec();
$this->connectTimeoutSec = $builder->getConnectTimeoutSec();
$this->readTimeoutSec = $builder->getReadTimeoutSec();
$this->writeTimeoutSec = $builder->getWriteTimeoutSec();
$this->protocol = $builder->getProtocol();
}
public static function getBuilder() public static function getBuilder()
{ {
return new ModbusSocketBuilder(); return new ModbusConnectionBuilder();
} }
/** /**
@ -78,15 +53,15 @@ class ModbusSocket
* *
* @return bool * @return bool
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws \PHPModbus\IOException * @throws \PHPModbus\Network\IOException
*/ */
public function connect() public function connect()
{ {
$protocol = null; $protocol = null;
switch ($this->socket_protocol) { switch ($this->protocol) {
case 'TCP': case 'TCP':
case 'UDP': case 'UDP':
$protocol = strtolower($this->socket_protocol); $protocol = strtolower($this->protocol);
break; break;
default: default:
throw new InvalidArgumentException("Unknown socket protocol, should be 'TCP' or 'UDP'"); throw new InvalidArgumentException("Unknown socket protocol, should be 'TCP' or 'UDP'");
@ -97,7 +72,7 @@ class ModbusSocket
// Bind the client stream to a specific local port // Bind the client stream to a specific local port
$opts = array( $opts = array(
'socket' => array( 'socket' => array(
'bindto' => "{$this->client}:{$this->client_port}", 'bindto' => "{$this->client}:{$this->clientPort}",
), ),
); );
} }
@ -107,7 +82,7 @@ class ModbusSocket
"$protocol://$this->host:$this->port", "$protocol://$this->host:$this->port",
$errno, $errno,
$errstr, $errstr,
$this->socket_connect_timeout_sec, $this->connectTimeoutSec,
STREAM_CLIENT_CONNECT, STREAM_CLIENT_CONNECT,
$context $context
); );
@ -124,7 +99,7 @@ class ModbusSocket
stream_set_blocking($this->streamSocket, false); // use non-blocking stream stream_set_blocking($this->streamSocket, false); // use non-blocking stream
$writeTimeoutParts = $this->secsToSecUsecArray($this->socket_write_timeout_sec); $writeTimeoutParts = $this->secsToSecUsecArray($this->writeTimeoutSec);
// set as stream timeout as we use 'stream_select' to read data and this method has its own timeout // set as stream timeout as we use 'stream_select' to read data and this method has its own timeout
// this call will only affect our fwrite parts (send data method) // this call will only affect our fwrite parts (send data method)
stream_set_timeout($this->streamSocket, $writeTimeoutParts['sec'], $writeTimeoutParts['usec']); stream_set_timeout($this->streamSocket, $writeTimeoutParts['sec'], $writeTimeoutParts['usec']);
@ -142,10 +117,10 @@ class ModbusSocket
*/ */
public function receive() public function receive()
{ {
$totalReadTimeout = $this->timeout_sec; $totalReadTimeout = $this->timeoutSec;
$lastAccess = microtime(true); $lastAccess = microtime(true);
$readTimeout = $this->secsToSecUsecArray($this->socket_read_timeout_sec); $readTimeout = $this->secsToSecUsecArray($this->readTimeoutSec);
while (true) { while (true) {
$read = array($this->streamSocket); $read = array($this->streamSocket);
$write = null; $write = null;
@ -234,120 +209,4 @@ class ModbusSocket
{ {
return $this->statusMessages; return $this->statusMessages;
} }
}
}
class ModbusSocketBuilder extends ModbusSocket
{
/**
* @var ModbusSocket instance to be built
*/
private $modbusSocket;
public function __construct()
{
$this->modbusSocket = new ModbusSocket();
}
/**
* Return built instance of ModbusSocket
*
* @return ModbusSocket built instance
*/
public function build()
{
return $this->modbusSocket;
}
/**
* @param string $client
* @return ModbusSocketBuilder
*/
public function setClient($client)
{
$this->modbusSocket->client = $client;
return $this;
}
/**
* @param string $client_port
* @return ModbusSocketBuilder
*/
public function setClientPort($client_port)
{
$this->modbusSocket->client_port = $client_port;
return $this;
}
/**
* @param float $timeout_sec
* @return ModbusSocketBuilder
*/
public function setTimeoutSec($timeout_sec)
{
$this->modbusSocket->timeout_sec = $timeout_sec;
return $this;
}
/**
* @param float $socket_read_timeout_sec
* @return ModbusSocketBuilder
*/
public function setSocketReadTimeoutSec($socket_read_timeout_sec)
{
$this->modbusSocket->socket_read_timeout_sec = $socket_read_timeout_sec;
return $this;
}
/**
* @param float $socket_write_timeout_sec
* @return ModbusSocketBuilder
*/
public function setSocketWriteTimeoutSec($socket_write_timeout_sec)
{
$this->modbusSocket->socket_write_timeout_sec = $socket_write_timeout_sec;
return $this;
}
/**
* @param string $socket_protocol
* @return ModbusSocketBuilder
*/
public function setSocketProtocol($socket_protocol)
{
$this->modbusSocket->socket_protocol = $socket_protocol;
return $this;
}
/**
* @param string $host
* @return ModbusSocketBuilder
*/
public function setHost($host)
{
$this->modbusSocket->host = $host;
return $this;
}
/**
* @param string $port
* @return ModbusSocketBuilder
*/
public function setPort($port)
{
$this->modbusSocket->port = $port;
return $this;
}
/**
* @param float $socket_connect_timeout_sec
* @return ModbusSocketBuilder
*/
public function setSocketConnectTimeoutSec($socket_connect_timeout_sec)
{
$this->modbusSocket->socket_connect_timeout_sec = $socket_connect_timeout_sec;
return $this;
}
}

@ -0,0 +1,111 @@
<?php
namespace PHPModbus\Network;
class ModbusConnectionBuilder extends ModbusConnectionProperties
{
/**
* Return built instance of ModbusConnection
*
* @return ModbusConnection built instance
* @throws \LogicException
*/
public function build()
{
if (empty($this->host)) {
throw new \LogicException('host property can not be left null or empty!');
}
return new ModbusConnection($this);
}
/**
* @param string $client
* @return ModbusConnectionBuilder
*/
public function setClient($client)
{
$this->client = $client;
return $this;
}
/**
* @param string $clientPort
* @return ModbusConnectionBuilder
*/
public function setClientPort($clientPort)
{
$this->clientPort = $clientPort;
return $this;
}
/**
* @param float $timeoutSec
* @return ModbusConnectionBuilder
*/
public function setTimeoutSec($timeoutSec)
{
$this->timeoutSec = $timeoutSec;
return $this;
}
/**
* @param float $connectTimeoutSec
* @return ModbusConnectionBuilder
*/
public function setConnectTimeoutSec($connectTimeoutSec)
{
$this->connectTimeoutSec = $connectTimeoutSec;
return $this;
}
/**
* @param float $readTimeoutSec
* @return ModbusConnectionBuilder
*/
public function setReadTimeoutSec($readTimeoutSec)
{
$this->readTimeoutSec = $readTimeoutSec;
return $this;
}
/**
* @param float $writeTimeoutSec
* @return ModbusConnectionBuilder
*/
public function setWriteTimeoutSec($writeTimeoutSec)
{
$this->writeTimeoutSec = $writeTimeoutSec;
return $this;
}
/**
* @param string $protocol
* @return ModbusConnectionBuilder
*/
public function setProtocol($protocol)
{
$this->protocol = $protocol;
return $this;
}
/**
* @param string $host
* @return ModbusConnectionBuilder
*/
public function setHost($host)
{
$this->host = $host;
return $this;
}
/**
* @param string $port
* @return ModbusConnectionBuilder
*/
public function setPort($port)
{
$this->port = $port;
return $this;
}
}

@ -0,0 +1,120 @@
<?php
namespace PHPModbus\Network;
/**
* ModbusConnection immutable properties base class
*/
abstract class ModbusConnectionProperties
{
/**
* @var string (optional) client IP address when binding client
*/
protected $client = '';
/**
* @var string client port set when binding client to local ip&port
*/
protected $clientPort = 502;
/**
* @var float Total response timeout (seconds, decimals allowed)
*/
protected $timeoutSec = 5;
/**
* @var float maximum timeout when establishing connection (seconds, decimals allowed)
*/
protected $connectTimeoutSec = 1;
/**
* @var float read timeout (seconds, decimals allowed)
*/
protected $readTimeoutSec = 0.3; // 300 ms
/**
* @var float maximum timeout for write operation on connection (seconds, decimals allowed)
*/
protected $writeTimeoutSec = 1;
/**
* @var string network protocol (TCP, UDP)
*/
protected $protocol = 'UDP';
/**
* @var string|null Modbus device IP address
*/
protected $host;
/**
* @var string gateway port
*/
protected $port = 502;
/**
* @return string (optional) client IP address when binding client
*/
public function getClient()
{
return $this->client;
}
/**
* @return string client port set when binding client to local ip&port
*/
public function getClientPort()
{
return $this->clientPort;
}
/**
* @return float Total response timeout (seconds, decimals allowed)
*/
public function getTimeoutSec()
{
return $this->timeoutSec;
}
/**
* @return float maximum timeout when establishing connection (seconds, decimals allowed)
*/
public function getConnectTimeoutSec()
{
return $this->connectTimeoutSec;
}
/**
* @return float read timeout (seconds, decimals allowed)
*/
public function getReadTimeoutSec()
{
return $this->readTimeoutSec;
}
/**
* @return float maximum timeout for write operation on connection (seconds, decimals allowed)
*/
public function getWriteTimeoutSec()
{
return $this->writeTimeoutSec;
}
/**
* @return string network protocol (TCP, UDP)
*/
public function getProtocol()
{
return $this->protocol;
}
/**
* @return string Modbus device IP address
*/
public function getHost()
{
return $this->host;
}
/**
* @return string gateway port
*/
public function getPort()
{
return $this->port;
}
}

@ -2,40 +2,41 @@
namespace Tests\ModbusMaster; namespace Tests\ModbusMaster;
use InvalidArgumentException; use InvalidArgumentException;
use PHPModbus\IOException;
use PHPModbus\ModbusException;
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
use PHPModbus\ModbusMasterTcp; use PHPModbus\ModbusMasterTcp;
class ModbusExceptionTest extends MockServerTestCase class ModbusExceptionTest extends MockServerTestCase
{ {
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unknown socket protocol, should be 'TCP' or 'UDP'
*/
public function testThrowProtocolMismatchException() public function testThrowProtocolMismatchException()
{ {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Unknown socket protocol, should be 'TCP' or 'UDP'");
$modbus = new ModbusMaster('127.0.0.1', 'Mismatch'); $modbus = new ModbusMaster('127.0.0.1', 'Mismatch');
$modbus->readCoils(0, 256, 1); $modbus->readCoils(0, 256, 1);
} }
/**
* @expectedException \PHPModbus\Network\IOException
* @expectedExceptionMessage Unable to create client socket to
*/
public function testPortClosedException() public function testPortClosedException()
{ {
$this->expectException(IOException::class);
$this->expectExceptionMessage('Unable to create client socket to');
$modbus = new ModbusMasterTcp('127.0.0.1'); $modbus = new ModbusMasterTcp('127.0.0.1');
$modbus->setSocketTimeout(0.2, 0.2); $modbus->setSocketTimeout(0.2, 0.2);
$modbus->readCoils(0, 256, 1); $modbus->readCoils(0, 256, 1);
} }
/**
* @expectedException \RuntimeException
* @expectedExceptionMessageRegExp /Watchdog time expired \[ 0\.5 sec \]!!! Connection to 127\.0\.0\.1:.* is not established/
*/
public function testTimeoutException() public function testTimeoutException()
{ {
$this->expectException(\RuntimeException::class);
$mockResponse = '89130000000400010101'; // respond with 1 byte (00000001 bits set) [1] $mockResponse = '89130000000400010101'; // respond with 1 byte (00000001 bits set) [1]
static::executeWithMockServer($mockResponse, function ($port) { static::executeWithMockServer($mockResponse, function ($port) {
$this->expectExceptionMessage("Watchdog time expired [ 0.5 sec ]!!! Connection to 127.0.0.1:{$port} is not established.");
$modbus = new ModbusMaster('127.0.0.1', 'UDP'); $modbus = new ModbusMaster('127.0.0.1', 'UDP');
$modbus->port = $port; $modbus->port = $port;
$modbus->setTimeout(0.5); $modbus->setTimeout(0.5);
@ -45,12 +46,12 @@ class ModbusExceptionTest extends MockServerTestCase
}, 'UDP', 1); }, 'UDP', 1);
} }
/**
* @expectedException \PHPModbus\ModbusException
* @expectedExceptionMessage Modbus response error code: 3 (ILLEGAL DATA VALUE)
*/
public function testThrowIllegalDataValueException() public function testThrowIllegalDataValueException()
{ {
$this->expectException(ModbusException::class);
$this->expectExceptionMessage('Modbus response error code: 3 (ILLEGAL DATA VALUE)');
$mockResponse = 'da8700000003008303'; // respond with 1 WORD (2 bytes) [0, 3] $mockResponse = 'da8700000003008303'; // respond with 1 WORD (2 bytes) [0, 3]
$clientData = static::executeWithMockServer($mockResponse, function ($port) { $clientData = static::executeWithMockServer($mockResponse, function ($port) {
$modbus = new ModbusMaster('127.0.0.1', 'TCP'); $modbus = new ModbusMaster('127.0.0.1', 'TCP');

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
class PhpTypeBytes2Mixed extends TestCase class PhpTypeBytes2Mixed extends TestCase
{ {
const DATA = [ private $data = [
"0" => 125, // 32098 (DINT) "0" => 125, // 32098 (DINT)
"1" => 98, "1" => 98,
"2" => 0, "2" => 0,
@ -31,15 +31,15 @@ class PhpTypeBytes2Mixed extends TestCase
public function testUnsignedInt() public function testUnsignedInt()
{ {
$this->assertEquals(32098, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4))); $this->assertEquals(32098, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4)));
} }
public function testSignedInt() public function testSignedInt()
{ {
$this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 4, 4))); $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 4, 4)));
$this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 8, 4))); $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 8, 4)));
$this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 12, 4))); $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 12, 4)));
$this->assertEquals(-25000, PhpType::bytes2signedInt(array_slice(self::DATA, 16, 2))); $this->assertEquals(-25000, PhpType::bytes2signedInt(array_slice($this->data, 16, 2)));
$this->assertEquals(25000, PhpType::bytes2signedInt(array_slice(self::DATA, 18, 2))); $this->assertEquals(25000, PhpType::bytes2signedInt(array_slice($this->data, 18, 2)));
} }
} }

@ -7,7 +7,7 @@ use PHPUnit_Framework_TestCase;
class Bytes2Real extends PHPUnit_Framework_TestCase class Bytes2Real extends PHPUnit_Framework_TestCase
{ {
const DATA = [ private $data = [
0 => 0, 0 => 0,
1 => 0, 1 => 0,
2 => 68, 2 => 68,
@ -24,8 +24,8 @@ class Bytes2Real extends PHPUnit_Framework_TestCase
public function testByte2Real() public function testByte2Real()
{ {
$this->assertEquals(1000, PhpType::bytes2float(array_slice(self::DATA, 0, 4))); $this->assertEquals(1000, PhpType::bytes2float(array_slice($this->data, 0, 4)));
$this->assertEquals(2000, PhpType::bytes2float(array_slice(self::DATA, 4, 4))); $this->assertEquals(2000, PhpType::bytes2float(array_slice($this->data, 4, 4)));
$this->assertEquals(1.25, PhpType::bytes2float(array_slice(self::DATA, 8, 4))); $this->assertEquals(1.25, PhpType::bytes2float(array_slice($this->data, 8, 4)));
} }
} }

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
class Bytes2SignedInt extends TestCase class Bytes2SignedInt extends TestCase
{ {
const DATA = [ private $data = [
0xFF, // -1 0xFF, // -1
0xFF, 0xFF,
0xFF, 0xFF,
@ -31,12 +31,12 @@ class Bytes2SignedInt extends TestCase
public function testByte2SignedInt() public function testByte2SignedInt()
{ {
$this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 0, 2))); $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 0, 2)));
$this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 0, 4))); $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 0, 4)));
$this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 4, 4))); $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 4, 4)));
$this->assertEquals(1, PhpType::bytes2signedInt(array_slice(self::DATA, 8, 4))); $this->assertEquals(1, PhpType::bytes2signedInt(array_slice($this->data, 8, 4)));
$this->assertEquals(-2147483648, PhpType::bytes2signedInt(array_slice(self::DATA, 12, 4))); $this->assertEquals(-2147483648, PhpType::bytes2signedInt(array_slice($this->data, 12, 4)));
$this->assertEquals(2147483647, PhpType::bytes2signedInt(array_slice(self::DATA, 16, 4))); $this->assertEquals(2147483647, PhpType::bytes2signedInt(array_slice($this->data, 16, 4)));
} }
} }

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
class Bytes2String extends TestCase class Bytes2String extends TestCase
{ {
const DATA = [ // String "Hello word!" private $data = [ // String "Hello word!"
0x48, //H 0x48, //H
0x65, //e 0x65, //e
0x6c, //l 0x6c, //l
@ -26,7 +26,7 @@ class Bytes2String extends TestCase
public function testBytesToString() public function testBytesToString()
{ {
$this->assertEquals('eHll oowlr!da', PhpType::bytes2string(self::DATA)); $this->assertEquals('eHll oowlr!da', PhpType::bytes2string($this->data));
$this->assertEquals('Hello world!', PhpType::bytes2string(self::DATA, true)); $this->assertEquals('Hello world!', PhpType::bytes2string($this->data, true));
} }
} }

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
class Bytes2UnSignedIntTest extends TestCase class Bytes2UnSignedIntTest extends TestCase
{ {
const DATA = [ private $data = [
0xFF, // -1 0xFF, // -1
0xFF, 0xFF,
0xFF, 0xFF,
@ -31,12 +31,12 @@ class Bytes2UnSignedIntTest extends TestCase
public function testByte2SignedInt() public function testByte2SignedInt()
{ {
$this->assertEquals(65535, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 2))); $this->assertEquals(65535, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 2)));
$this->assertEquals(4294967295, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4))); $this->assertEquals(4294967295, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4)));
$this->assertEquals(0, PhpType::bytes2unsignedInt(array_slice(self::DATA, 4, 4))); $this->assertEquals(0, PhpType::bytes2unsignedInt(array_slice($this->data, 4, 4)));
$this->assertEquals(1, PhpType::bytes2unsignedInt(array_slice(self::DATA, 8, 4))); $this->assertEquals(1, PhpType::bytes2unsignedInt(array_slice($this->data, 8, 4)));
$this->assertEquals(2147483648, PhpType::bytes2unsignedInt(array_slice(self::DATA, 12, 4))); $this->assertEquals(2147483648, PhpType::bytes2unsignedInt(array_slice($this->data, 12, 4)));
$this->assertEquals(2147483647, PhpType::bytes2unsignedInt(array_slice(self::DATA, 16, 4))); $this->assertEquals(2147483647, PhpType::bytes2unsignedInt(array_slice($this->data, 16, 4)));
} }
} }

@ -6,22 +6,26 @@ use PHPUnit\Framework\TestCase;
class PhpTypeArrayExceptionWithTextArrayTest extends TestCase class PhpTypeArrayExceptionWithTextArrayTest extends TestCase
{ {
const DATA = [ private $data = [
"0" => 100, // 32098 (DINT) "0" => 100, // 32098 (DINT)
"1" => "e", "1" => "e",
"2" => 0, "2" => 0,
"3" => 0 "3" => 0
]; ];
/**
* @expectedException \Exception
*/
public function testExceptionWhenSize2ContainsString() public function testExceptionWhenSize2ContainsString()
{ {
$this->expectException(\Exception::class); PhpType::bytes2unsignedInt(array_slice($this->data, 0, 2));
PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 2));
} }
/**
* @expectedException \Exception
*/
public function testExceptionWhenSize4ContainsString() public function testExceptionWhenSize4ContainsString()
{ {
$this->expectException(\Exception::class); PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4));
PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4));
} }
} }

@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
class PhpTypeArraySizeExceptionsTest extends TestCase class PhpTypeArraySizeExceptionsTest extends TestCase
{ {
const DATA = [ private $data = [
"0" => 100, // 32098 (DINT) "0" => 100, // 32098 (DINT)
"1" => 2, "1" => 2,
"2" => 0, "2" => 0,
@ -15,32 +15,38 @@ class PhpTypeArraySizeExceptionsTest extends TestCase
"5" => 2 "5" => 2
]; ];
/**
* @expectedException \Exception
*/
public function testExceptionWhenSizeShort() public function testExceptionWhenSizeShort()
{ {
$this->expectException(\Exception::class); PhpType::bytes2unsignedInt(array_slice($this->data, 0, 1));
PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 1));
} }
/**
* @expectedException \Exception
*/
public function testExceptionWhenSizeShort3() public function testExceptionWhenSizeShort3()
{ {
$this->expectException(\Exception::class); PhpType::bytes2unsignedInt(array_slice($this->data, 0, 3));
PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 3));
} }
/**
* @expectedException \Exception
*/
public function testExceptionWhenSizeLong() public function testExceptionWhenSizeLong()
{ {
$this->expectException(\Exception::class); PhpType::bytes2unsignedInt(array_slice($this->data, 0, 5));
PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 5));
} }
public function testNoExceptionWhenSize2() public function testNoExceptionWhenSize2()
{ {
$this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 2))); $this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 2)));
} }
public function testNoExceptionWhenSize4() public function testNoExceptionWhenSize4()
{ {
$this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4))); $this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4)));
} }
} }
Loading…
Cancel
Save