From 95ecf4bf5b7e1f04f85de19fa240e4e3490bd8b0 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Wed, 7 Dec 2016 15:01:12 +0200 Subject: [PATCH] Rename ModbusSocket to ModbusConnection, support php5.4 --- composer.json | 2 +- src/IOException.php | 11 - src/ModbusMaster.php | 45 ++-- src/Network/IOException.php | 12 ++ .../ModbusConnection.php} | 193 +++--------------- src/Network/ModbusConnectionBuilder.php | 111 ++++++++++ src/Network/ModbusConnectionProperties.php | 120 +++++++++++ tests/ModbusMaster/ModbusExceptionTest.php | 31 +-- tests/PhpType/Bytes2MixedTest.php | 14 +- tests/PhpType/Bytes2RealTest.php | 8 +- tests/PhpType/Bytes2SignedIntTest.php | 14 +- tests/PhpType/Bytes2StringTest.php | 6 +- tests/PhpType/Bytes2UnSignedIntTest.php | 14 +- ...PhpTypeArrayExceptionWithTextArrayTest.php | 14 +- .../PhpTypeArraySizeExceptionsTest.php | 24 ++- 15 files changed, 365 insertions(+), 254 deletions(-) delete mode 100644 src/IOException.php create mode 100644 src/Network/IOException.php rename src/{ModbusSocket.php => Network/ModbusConnection.php} (54%) create mode 100644 src/Network/ModbusConnectionBuilder.php create mode 100644 src/Network/ModbusConnectionProperties.php diff --git a/composer.json b/composer.json index 5730ece..5558ddf 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "PhpModbus with namespaces and updated to PHP 7", "license": "LGPL", "require": { - "php": "^5.5 || ^7.0" + "php": "^5.4 || ^7.0" }, "require-dev": { "react/socket": "~0.4.0", diff --git a/src/IOException.php b/src/IOException.php deleted file mode 100644 index 53e73a9..0000000 --- a/src/IOException.php +++ /dev/null @@ -1,11 +0,0 @@ -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 { - $socket = ModbusSocket::getBuilder() - ->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(); + $connection->connect(); $packet = $buildRequest(); $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->validateResponseCode($data); + + $this->closeConnection($connection); return $parseResponse($data); - } finally { - $this->status .= implode("\n", $socket->getStatusMessages()); - $socket->close(); + } catch (Exception $e) { + $this->closeConnection($connection); + throw $e; } } + private function closeConnection(ModbusConnection $connection) + { + $this->status .= implode("\n", $connection->getStatusMessages()); + $connection->close(); + } + /** * fc15 * diff --git a/src/Network/IOException.php b/src/Network/IOException.php new file mode 100644 index 0000000..f8e70b3 --- /dev/null +++ b/src/Network/IOException.php @@ -0,0 +1,12 @@ +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() { - return new ModbusSocketBuilder(); + return new ModbusConnectionBuilder(); } /** @@ -78,15 +53,15 @@ class ModbusSocket * * @return bool * @throws \InvalidArgumentException - * @throws \PHPModbus\IOException + * @throws \PHPModbus\Network\IOException */ public function connect() { $protocol = null; - switch ($this->socket_protocol) { + switch ($this->protocol) { case 'TCP': case 'UDP': - $protocol = strtolower($this->socket_protocol); + $protocol = strtolower($this->protocol); break; default: 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 $opts = 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", $errno, $errstr, - $this->socket_connect_timeout_sec, + $this->connectTimeoutSec, STREAM_CLIENT_CONNECT, $context ); @@ -124,7 +99,7 @@ class ModbusSocket 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 // this call will only affect our fwrite parts (send data method) stream_set_timeout($this->streamSocket, $writeTimeoutParts['sec'], $writeTimeoutParts['usec']); @@ -142,10 +117,10 @@ class ModbusSocket */ public function receive() { - $totalReadTimeout = $this->timeout_sec; + $totalReadTimeout = $this->timeoutSec; $lastAccess = microtime(true); - $readTimeout = $this->secsToSecUsecArray($this->socket_read_timeout_sec); + $readTimeout = $this->secsToSecUsecArray($this->readTimeoutSec); while (true) { $read = array($this->streamSocket); $write = null; @@ -234,120 +209,4 @@ class ModbusSocket { 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; - } - -} +} \ No newline at end of file diff --git a/src/Network/ModbusConnectionBuilder.php b/src/Network/ModbusConnectionBuilder.php new file mode 100644 index 0000000..166436f --- /dev/null +++ b/src/Network/ModbusConnectionBuilder.php @@ -0,0 +1,111 @@ +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; + } +} diff --git a/src/Network/ModbusConnectionProperties.php b/src/Network/ModbusConnectionProperties.php new file mode 100644 index 0000000..05066f1 --- /dev/null +++ b/src/Network/ModbusConnectionProperties.php @@ -0,0 +1,120 @@ +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; + } + +} \ No newline at end of file diff --git a/tests/ModbusMaster/ModbusExceptionTest.php b/tests/ModbusMaster/ModbusExceptionTest.php index ac7e3a4..1e83df9 100644 --- a/tests/ModbusMaster/ModbusExceptionTest.php +++ b/tests/ModbusMaster/ModbusExceptionTest.php @@ -2,40 +2,41 @@ namespace Tests\ModbusMaster; use InvalidArgumentException; -use PHPModbus\IOException; -use PHPModbus\ModbusException; use PHPModbus\ModbusMaster; use PHPModbus\ModbusMasterTcp; class ModbusExceptionTest extends MockServerTestCase { + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Unknown socket protocol, should be 'TCP' or 'UDP' + */ 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->readCoils(0, 256, 1); } + /** + * @expectedException \PHPModbus\Network\IOException + * @expectedExceptionMessage Unable to create client socket to + */ public function testPortClosedException() { - $this->expectException(IOException::class); - $this->expectExceptionMessage('Unable to create client socket to'); - $modbus = new ModbusMasterTcp('127.0.0.1'); $modbus->setSocketTimeout(0.2, 0.2); $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() { - $this->expectException(\RuntimeException::class); $mockResponse = '89130000000400010101'; // respond with 1 byte (00000001 bits set) [1] 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->port = $port; $modbus->setTimeout(0.5); @@ -45,12 +46,12 @@ class ModbusExceptionTest extends MockServerTestCase }, 'UDP', 1); } - + /** + * @expectedException \PHPModbus\ModbusException + * @expectedExceptionMessage Modbus response error code: 3 (ILLEGAL DATA VALUE) + */ 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] $clientData = static::executeWithMockServer($mockResponse, function ($port) { $modbus = new ModbusMaster('127.0.0.1', 'TCP'); diff --git a/tests/PhpType/Bytes2MixedTest.php b/tests/PhpType/Bytes2MixedTest.php index fcaa25b..3692910 100644 --- a/tests/PhpType/Bytes2MixedTest.php +++ b/tests/PhpType/Bytes2MixedTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class PhpTypeBytes2Mixed extends TestCase { - const DATA = [ + private $data = [ "0" => 125, // 32098 (DINT) "1" => 98, "2" => 0, @@ -31,15 +31,15 @@ class PhpTypeBytes2Mixed extends TestCase 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() { - $this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 4, 4))); - $this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 8, 4))); - $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 12, 4))); - $this->assertEquals(-25000, PhpType::bytes2signedInt(array_slice(self::DATA, 16, 2))); - $this->assertEquals(25000, PhpType::bytes2signedInt(array_slice(self::DATA, 18, 2))); + $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 4, 4))); + $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 8, 4))); + $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 12, 4))); + $this->assertEquals(-25000, PhpType::bytes2signedInt(array_slice($this->data, 16, 2))); + $this->assertEquals(25000, PhpType::bytes2signedInt(array_slice($this->data, 18, 2))); } } diff --git a/tests/PhpType/Bytes2RealTest.php b/tests/PhpType/Bytes2RealTest.php index 2fd7b80..1bb7458 100644 --- a/tests/PhpType/Bytes2RealTest.php +++ b/tests/PhpType/Bytes2RealTest.php @@ -7,7 +7,7 @@ use PHPUnit_Framework_TestCase; class Bytes2Real extends PHPUnit_Framework_TestCase { - const DATA = [ + private $data = [ 0 => 0, 1 => 0, 2 => 68, @@ -24,8 +24,8 @@ class Bytes2Real extends PHPUnit_Framework_TestCase public function testByte2Real() { - $this->assertEquals(1000, PhpType::bytes2float(array_slice(self::DATA, 0, 4))); - $this->assertEquals(2000, PhpType::bytes2float(array_slice(self::DATA, 4, 4))); - $this->assertEquals(1.25, PhpType::bytes2float(array_slice(self::DATA, 8, 4))); + $this->assertEquals(1000, PhpType::bytes2float(array_slice($this->data, 0, 4))); + $this->assertEquals(2000, PhpType::bytes2float(array_slice($this->data, 4, 4))); + $this->assertEquals(1.25, PhpType::bytes2float(array_slice($this->data, 8, 4))); } } diff --git a/tests/PhpType/Bytes2SignedIntTest.php b/tests/PhpType/Bytes2SignedIntTest.php index 362a30d..dcf4467 100644 --- a/tests/PhpType/Bytes2SignedIntTest.php +++ b/tests/PhpType/Bytes2SignedIntTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class Bytes2SignedInt extends TestCase { - const DATA = [ + private $data = [ 0xFF, // -1 0xFF, 0xFF, @@ -31,12 +31,12 @@ class Bytes2SignedInt extends TestCase public function testByte2SignedInt() { - $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 0, 2))); - $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice(self::DATA, 0, 4))); + $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 0, 2))); + $this->assertEquals(-1, PhpType::bytes2signedInt(array_slice($this->data, 0, 4))); - $this->assertEquals(0, PhpType::bytes2signedInt(array_slice(self::DATA, 4, 4))); - $this->assertEquals(1, PhpType::bytes2signedInt(array_slice(self::DATA, 8, 4))); - $this->assertEquals(-2147483648, PhpType::bytes2signedInt(array_slice(self::DATA, 12, 4))); - $this->assertEquals(2147483647, PhpType::bytes2signedInt(array_slice(self::DATA, 16, 4))); + $this->assertEquals(0, PhpType::bytes2signedInt(array_slice($this->data, 4, 4))); + $this->assertEquals(1, PhpType::bytes2signedInt(array_slice($this->data, 8, 4))); + $this->assertEquals(-2147483648, PhpType::bytes2signedInt(array_slice($this->data, 12, 4))); + $this->assertEquals(2147483647, PhpType::bytes2signedInt(array_slice($this->data, 16, 4))); } } diff --git a/tests/PhpType/Bytes2StringTest.php b/tests/PhpType/Bytes2StringTest.php index 8b5d788..851c331 100644 --- a/tests/PhpType/Bytes2StringTest.php +++ b/tests/PhpType/Bytes2StringTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class Bytes2String extends TestCase { - const DATA = [ // String "Hello word!" + private $data = [ // String "Hello word!" 0x48, //H 0x65, //e 0x6c, //l @@ -26,7 +26,7 @@ class Bytes2String extends TestCase public function testBytesToString() { - $this->assertEquals('eHll oowlr!da', PhpType::bytes2string(self::DATA)); - $this->assertEquals('Hello world!', PhpType::bytes2string(self::DATA, true)); + $this->assertEquals('eHll oowlr!da', PhpType::bytes2string($this->data)); + $this->assertEquals('Hello world!', PhpType::bytes2string($this->data, true)); } } diff --git a/tests/PhpType/Bytes2UnSignedIntTest.php b/tests/PhpType/Bytes2UnSignedIntTest.php index 2c424fc..14e2698 100644 --- a/tests/PhpType/Bytes2UnSignedIntTest.php +++ b/tests/PhpType/Bytes2UnSignedIntTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class Bytes2UnSignedIntTest extends TestCase { - const DATA = [ + private $data = [ 0xFF, // -1 0xFF, 0xFF, @@ -31,12 +31,12 @@ class Bytes2UnSignedIntTest extends TestCase public function testByte2SignedInt() { - $this->assertEquals(65535, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 2))); - $this->assertEquals(4294967295, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4))); + $this->assertEquals(65535, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 2))); + $this->assertEquals(4294967295, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4))); - $this->assertEquals(0, PhpType::bytes2unsignedInt(array_slice(self::DATA, 4, 4))); - $this->assertEquals(1, PhpType::bytes2unsignedInt(array_slice(self::DATA, 8, 4))); - $this->assertEquals(2147483648, PhpType::bytes2unsignedInt(array_slice(self::DATA, 12, 4))); - $this->assertEquals(2147483647, PhpType::bytes2unsignedInt(array_slice(self::DATA, 16, 4))); + $this->assertEquals(0, PhpType::bytes2unsignedInt(array_slice($this->data, 4, 4))); + $this->assertEquals(1, PhpType::bytes2unsignedInt(array_slice($this->data, 8, 4))); + $this->assertEquals(2147483648, PhpType::bytes2unsignedInt(array_slice($this->data, 12, 4))); + $this->assertEquals(2147483647, PhpType::bytes2unsignedInt(array_slice($this->data, 16, 4))); } } diff --git a/tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php b/tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php index 0565a13..62efe58 100644 --- a/tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php +++ b/tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php @@ -6,22 +6,26 @@ use PHPUnit\Framework\TestCase; class PhpTypeArrayExceptionWithTextArrayTest extends TestCase { - const DATA = [ + private $data = [ "0" => 100, // 32098 (DINT) "1" => "e", "2" => 0, "3" => 0 ]; + /** + * @expectedException \Exception + */ public function testExceptionWhenSize2ContainsString() { - $this->expectException(\Exception::class); - PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 2)); + PhpType::bytes2unsignedInt(array_slice($this->data, 0, 2)); } + /** + * @expectedException \Exception + */ public function testExceptionWhenSize4ContainsString() { - $this->expectException(\Exception::class); - PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4)); + PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4)); } } diff --git a/tests/PhpType/PhpTypeArraySizeExceptionsTest.php b/tests/PhpType/PhpTypeArraySizeExceptionsTest.php index 6003ad4..1782fc2 100644 --- a/tests/PhpType/PhpTypeArraySizeExceptionsTest.php +++ b/tests/PhpType/PhpTypeArraySizeExceptionsTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; class PhpTypeArraySizeExceptionsTest extends TestCase { - const DATA = [ + private $data = [ "0" => 100, // 32098 (DINT) "1" => 2, "2" => 0, @@ -15,32 +15,38 @@ class PhpTypeArraySizeExceptionsTest extends TestCase "5" => 2 ]; + /** + * @expectedException \Exception + */ public function testExceptionWhenSizeShort() { - $this->expectException(\Exception::class); - PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 1)); + PhpType::bytes2unsignedInt(array_slice($this->data, 0, 1)); } + /** + * @expectedException \Exception + */ public function testExceptionWhenSizeShort3() { - $this->expectException(\Exception::class); - PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 3)); + PhpType::bytes2unsignedInt(array_slice($this->data, 0, 3)); } + /** + * @expectedException \Exception + */ public function testExceptionWhenSizeLong() { - $this->expectException(\Exception::class); - PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 5)); + PhpType::bytes2unsignedInt(array_slice($this->data, 0, 5)); } 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() { - $this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice(self::DATA, 0, 4))); + $this->assertEquals(25602, PhpType::bytes2unsignedInt(array_slice($this->data, 0, 4))); } } \ No newline at end of file