make tests compatible with older php syntax (phpunit still requires 5.6), update readme, update examples to ease testing modbus

pull/3/head
toimtoimtoim 8 years ago
parent 6137326277
commit 7ea3d8007e
  1. 20
      .travis.yml
  2. 22
      README.md
  3. 5
      composer.json
  4. 6
      examples/example_750841_Mmemory.php
  5. 8
      examples/example_datatype.php
  6. 6
      examples/example_fc1.php
  7. 6
      examples/example_fc15.php
  8. 6
      examples/example_fc16.php
  9. 5
      examples/example_fc2.php
  10. 6
      examples/example_fc22.php
  11. 8
      examples/example_fc23.php
  12. 6
      examples/example_fc3.php
  13. 7
      examples/example_fc4.php
  14. 8
      examples/example_fc5.php
  15. 8
      examples/example_fc6.php
  16. 12
      examples/request_input_data.php
  17. 11
      phpunit.xml.dist
  18. 20
      tests/ModbusMaster/Fc15WriteMultipleCoilsTest.php
  19. 5
      tests/ModbusMaster/MockServerTestCase.php
  20. 30
      tests/ModbusMaster/ModbusExceptionTest.php
  21. 2
      tests/ModbusMaster/UdpFc1ReadCoilsTest.php
  22. 14
      tests/PhpType/Bytes2MixedTest.php
  23. 8
      tests/PhpType/Bytes2RealTest.php
  24. 14
      tests/PhpType/Bytes2SignedIntTest.php
  25. 6
      tests/PhpType/Bytes2StringTest.php
  26. 14
      tests/PhpType/Bytes2UnSignedIntTest.php
  27. 14
      tests/PhpType/PhpTypeArrayExceptionWithTextArrayTest.php
  28. 24
      tests/PhpType/PhpTypeArraySizeExceptionsTest.php

@ -0,0 +1,20 @@
language: php
sudo: false
cache:
directories:
- $HOME/.composer/cache
php:
- 5.6
- 7.0
before_install:
- travis_retry composer self-update
install:
- travis_retry composer update --no-interaction
script:
- composer test-ci

@ -2,12 +2,12 @@
Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP. Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
**NOTE: This is a fork to fix & update the library code (and code alone). Notably, the tests are probably all broken.** **NOTE: This is a fork to fix & update the library code (and code alone).**
> **What's new** ##What's new
>
> This fork adds a namespace and fixes issues encountered when porting to PHP 7
* This fork adds a namespace and fixes issues encountered when porting to PHP 7
* Fixes/replaces old MS Windows specific tests
## Implemented features ## Implemented features
@ -26,6 +26,7 @@ Implementation of the basic functionality of the Modbus TCP and UDP based protoc
## Requirements ## Requirements
* The PHP extension php_sockets.dll should be enabled (server php.ini file) * The PHP extension php_sockets.dll should be enabled (server php.ini file)
* PHP 5.3.2 (5.6 for tests)
## Example ## Example
@ -53,6 +54,19 @@ Use the `setTimeout($seconds)` and `setSocketTimeout($read_timeout_sec, $write_t
Most of the code is (to some extent) commented and documented with PhpDoc. You should get useful tooltips in your IDE. Most of the code is (to some extent) commented and documented with PhpDoc. You should get useful tooltips in your IDE.
## Tests
To run the test suite, you need install the dependencies via composer, then
run PHPUnit.
NB: PHP 5.6+ is required for tests
composer install
composer test # or under Windows vendor\bin\phunit.bat
To report test coverage (created inside ./report/html):
composer test-coverage
## GoogleCode legacy docs & downloads ## GoogleCode legacy docs & downloads

@ -32,5 +32,10 @@
"psr-4": { "psr-4": {
"Tests\\": "tests/" "Tests\\": "tests/"
} }
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-clover report/coverage.xml",
"test-coverage": "vendor/bin/phpunit --coverage-html report/html"
} }
} }

@ -1,12 +1,10 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
use PHPModbus\PhpType; use PHPModbus\PhpType;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$quantity = ((int)$_GET['quantity']) ?: 6;
$modbus = new ModbusMaster($ip, 'UDP'); $modbus = new ModbusMaster($ip, 'UDP');

@ -1,12 +1,10 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
use PHPModbus\PhpType; use PHPModbus\PhpType;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$quantity = ((int)$_GET['quantity']) ?: 10;
$modbus = new ModbusMaster($ip, 'UDP'); $modbus = new ModbusMaster($ip, 'UDP');
try { try {

@ -1,11 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$quantity = ((int)$_GET['quantity']) ?: 12;
$modbus = new ModbusMaster($ip, 'UDP'); $modbus = new ModbusMaster($ip, 'UDP');

@ -1,9 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 0;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -1,9 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -2,10 +2,7 @@
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 0;
$quantity = ((int)$_GET['quantity']) ?: 2;
$modbus = new ModbusMaster($ip, 'UDP'); $modbus = new ModbusMaster($ip, 'UDP');

@ -1,9 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -1,11 +1,9 @@
<?php <?php
use PHPModbus\ModbusMasterUdp; require __DIR__ . '/../vendor/autoload.php';
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; use PHPModbus\ModbusMasterUdp;
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$quantity = ((int)$_GET['quantity']) ?: 6;
require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);
// Data to be writen // Data to be writen

@ -1,11 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 12288;
$quantity = ((int)$_GET['quantity']) ?: 6;
$modbus = new ModbusMaster($ip, 'UDP'); $modbus = new ModbusMaster($ip, 'UDP');

@ -1,10 +1,9 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0;
$reference = ((int)$_GET['reference']) ?: 0;
$quantity = ((int)$_GET['quantity']) ?: 2;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -1,10 +1,10 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0; $value = isset($_GET['value']) ? ((bool)$_GET['value']) : false;
$reference = ((int)$_GET['reference']) ?: 12288;
$value = ((bool)$_GET['value']) ?: false;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -1,10 +1,10 @@
<?php <?php
require __DIR__ . '/../vendor/autoload.php';
use PHPModbus\ModbusMasterUdp; use PHPModbus\ModbusMasterUdp;
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_IP) ? $_GET['ip'] : '192.192.15.51'; require_once __DIR__ . '/request_input_data.php'; // 'ip', 'unitid','reference','quantity' are read from $_GET
$unitId = ((int)$_GET['unitid']) ?: 0; $value = isset($_GET['value']) ? ((int)$_GET['value']) : -1000;
$reference = ((int)$_GET['reference']) ?: 12288;
$value = ((int)$_GET['value']) ?: -1000;
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);

@ -0,0 +1,12 @@
<?php
$ipPrefix = '192.192.15.'; // change to '' if you want to allow request to be sent to every possible ip
$ip = '192.192.15.51';
if (isset($_GET['ip']) && filter_var($ipPrefix . $_GET['ip'], FILTER_VALIDATE_IP)) {
$ip = $ipPrefix . $_GET['ip'];
}
$unitId = isset($_GET['unitid']) ? (int)$_GET['unitid'] : 0;
$reference = isset($_GET['reference']) ? (int)$_GET['reference'] : 12288;
$quantity = isset($_GET['quantity']) ? (int)$_GET['quantity'] : 6;

@ -15,17 +15,6 @@
<directory suffix="Test.php">./tests/*</directory> <directory suffix="Test.php">./tests/*</directory>
</testsuite> </testsuite>
<logging>
<log type="coverage-html"
target="report/coverage"
charset="UTF-8"
yui="true"
highlight="true"
lowUpperBound="35"
highLowerBound="70"/>
<log type="junit" target="report/junit.xml" logIncompleteSkipped="false"/>
</logging>
<filter> <filter>
<whitelist processUncoveredFilesFromWhitelist="true"> <whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory> <directory suffix=".php">src</directory>

@ -18,4 +18,24 @@ class Fc15WriteMultipleCoilsTest extends MockServerTestCase
$packetWithoutTransactionId = substr($clientData[0], 4); $packetWithoutTransactionId = substr($clientData[0], 4);
$this->assertEquals('00000008000f300000030105', $packetWithoutTransactionId); $this->assertEquals('00000008000f300000030105', $packetWithoutTransactionId);
} }
public function testFc15WriteMultipleCoilsWithMultiWordPacket()
{
$mockResponse = 'a51100000006000f00000020';
$clientData = static::executeWithMockServer($mockResponse, function ($port) {
$modbus = new ModbusMaster('127.0.0.1', 'TCP');
$modbus->port = $port;
$this->assertTrue($modbus->fc15(0, 0,
[
1, 0, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
]));
});
$packetWithoutTransactionId = substr($clientData[0], 4);
$this->assertEquals('0000000b000f0000002004ed0ff0ff', $packetWithoutTransactionId);
}
} }

@ -23,6 +23,11 @@ abstract class MockServerTestCase extends TestCase
$clientData[] = $output; $clientData[] = $output;
}); });
if (strpos(PHP_OS, 'WIN') === false || getenv('MOCKSERVER_TIMEOUT_USEC') !== false) {
// wait to spin up. needed for linux. unnessecary on Windows 10.
// Ugly but even with 150ms sleep test run faster on Linux
usleep(getenv('MOCKSERVER_TIMEOUT_USEC') ?: 150000);
}
$closure($port); $closure($port);
}); });

@ -1,38 +1,42 @@
<?php <?php
namespace Tests\ModbusMaster; namespace Tests\ModbusMaster;
use InvalidArgumentException;
use PHPModbus\ModbusMaster; use PHPModbus\ModbusMaster;
use PHPModbus\ModbusMasterTcp; use PHPModbus\ModbusMasterTcp;
class ModbusExceptionTest extends MockServerTestCase class ModbusExceptionTest extends MockServerTestCase
{ {
/**
* @expectedException \Exception
* @expectedExceptionMessage Unknown socket protocol, should be 'TCP' or 'UDP'
*/
public function testThrowProtocolMismatchException() public function testThrowProtocolMismatchException()
{ {
$this->expectException(\Exception::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 \Exception
* @expectedExceptionMessage socket_connect() failed. Reason: ()No connection could be made because the target machine actively refused it
*/
public function testPortClosedException() public function testPortClosedException()
{ {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('socket_connect() failed. Reason: ()No connection could be made because the target machine actively refused it.');
$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 \Exception
* @expectedExceptionMessageRegExp /Watchdog time expired \[ 0\.5 sec \]!!! Connection to 127\.0\.0\.1:.* is not established/
*/
public function testTimeoutException() public function testTimeoutException()
{ {
$this->expectException(\Exception::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);
@ -42,12 +46,12 @@ class ModbusExceptionTest extends MockServerTestCase
}, 'UDP', 1); }, 'UDP', 1);
} }
/**
* @expectedException \Exception
* @expectedExceptionMessage Modbus response error code: 3 (ILLEGAL DATA VALUE)
*/
public function testThrowIllegalDataValueException() public function testThrowIllegalDataValueException()
{ {
$this->expectException(\Exception::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');

@ -13,7 +13,7 @@ class UdpFc1ReadCoilsTest extends MockServerTestCase
$modbus = new ModbusMasterUdp('127.0.0.1'); $modbus = new ModbusMasterUdp('127.0.0.1');
$modbus->port = $port; $modbus->port = $port;
usleep(50000); // no idea how to fix this. wait for server to "warm" up or modbus UDP socket will timeout. does not occur with TCP usleep(150000); // no idea how to fix this. wait for server to "warm" up or modbus UDP socket will timeout. does not occur with TCP
$this->assertEquals([1], $modbus->readCoils(0, 256, 1)); $this->assertEquals([1], $modbus->readCoils(0, 256, 1));
}, 'UDP'); }, 'UDP');

@ -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