Skip to content

Commit

Permalink
Support of PHP 8.0 implemented, tests fixed, for #52
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilFreelancer committed Jan 30, 2021
1 parent 8149731 commit dd68789
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 77 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ php:
- '7.2'
- '7.3'
- '7.4'
- '8.0'

before_script:
- sudo apt-get update
Expand Down
9 changes: 2 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@
}
},
"require": {
"php": "^7.2",
"php": "^7.2|^8.0",
"ext-sockets": "*",
"divineomega/php-ssh-connection": "^2.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"limedeck/phpunit-detailed-printer": "^5.0",
"orchestra/testbench": "^4.0|^5.0",
"phpstan/phpstan": "^0.12.32",
"phpstan/phpstan-strict-rules": "^0.12.2",
"phpunit/phpunit": "^8.0",
"rector/rector": "^0.7.41",
"rector/rector": "^0.7|^0.8|^0.9",
"roave/security-advisories": "dev-master",
"thecodingmachine/phpstan-strict-rules": "^0.12.0",
"squizlabs/php_codesniffer": "^3.5",
"larapack/dd": "^1.1"
},
Expand All @@ -66,11 +63,9 @@
"lint": "rector process src && php-cs-fixer fix -v",
"test:lint": "php-cs-fixer fix -v --dry-run",
"test:rector": "rector process src --dry-run",
"test:types": "phpstan analyse --ansi --memory-limit=0",
"test:unit": "phpunit --coverage-clover clover.xml",
"test": [
"@test:lint",
"@test:types",
"@test:unit"
]
}
Expand Down
37 changes: 37 additions & 0 deletions phpunit.local.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
printerClass="LimeDeck\Testing\Printer"
processIsolation="false"
stopOnFailure="true">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix="Test.php">./tests</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
</logging>
<testsuites>
<testsuite name="RouterOS API on PHP tests">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="ROS_HOST_LEGACY" value="routeros-6-42"/>
<env name="ROS_HOST_MODERN" value="routeros-6-48"/>
<env name="ROS_PORT_LEGACY" value="8728"/>
<env name="ROS_PORT_MODERN" value="8728"/>
<env name="ROS_USER" value="admin"/>
<env name="ROS_PASS" value="admin"/>
<env name="ROS_SSH_PORT" value="22"/>
</php>
</phpunit>
7 changes: 4 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
</testsuite>
</testsuites>
<php>
<env name="ROS_HOST" value="127.0.0.1"/>
<env name="ROS_USER" value="admin"/>
<env name="ROS_PASS" value="admin"/>
<env name="ROS_HOST_LEGACY" value="127.0.0.1"/>
<env name="ROS_HOST_MODERN" value="127.0.0.1"/>
<env name="ROS_PORT_MODERN" value="8728"/>
<env name="ROS_PORT_LEGACY" value="18728"/>
<env name="ROS_USER" value="admin"/>
<env name="ROS_PASS" value="admin"/>
<env name="ROS_SSH_PORT" value="22222"/>
</php>
</phpunit>
27 changes: 14 additions & 13 deletions src/Streams/ResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ class ResourceStream implements StreamInterface
public function __construct($stream)
{
if (!is_resource($stream)) {
throw new \InvalidArgumentException(
sprintf(
'Argument must be a valid resource type. %s given.',
gettype($stream)
)
);
throw new \InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.', gettype($stream)));
}

// TODO: Should we verify the resource type?
$this->stream = $stream;
}

/**
* @inheritDoc
* {@inheritDoc}
*
* @throws \RouterOS\Exceptions\StreamException when length parameter is invalid
* @throws \InvalidArgumentException when the stream have been totally read and read method is called again
Expand All @@ -49,8 +44,11 @@ public function read(int $length): string
throw new \InvalidArgumentException('Cannot read zero ot negative count of bytes from a stream');
}

// TODO: Ignore errors here, but why?
$result = @fread($this->stream, $length);
if (!is_resource($this->stream)) {
throw new StreamException('Stream is not writable');
}

$result = fread($this->stream, $length);

if (false === $result) {
throw new StreamException("Error reading $length bytes");
Expand All @@ -60,7 +58,7 @@ public function read(int $length): string
}

/**
* @inheritDoc
* {@inheritDoc}
*
* @throws \RouterOS\Exceptions\StreamException when not possible to write bytes
*/
Expand All @@ -70,8 +68,11 @@ public function write(string $string, int $length = null): int
$length = strlen($string);
}

// TODO: Ignore errors here, but why?
$result = @fwrite($this->stream, $string, $length);
if (!is_resource($this->stream)) {
throw new StreamException('Stream is not writable');
}

$result = fwrite($this->stream, $string, $length);

if (false === $result) {
throw new StreamException("Error writing $length bytes");
Expand All @@ -81,7 +82,7 @@ public function write(string $string, int $length = null): int
}

/**
* @inheritDoc
* {@inheritDoc}
*
* @throws \RouterOS\Exceptions\StreamException when not possible to close the stream
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/APIConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function constructProvider(): array
{
return [
[new ResourceStream(fopen(__FILE__, 'rb'))], // Myself, sure I exists
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST'), getenv('ROS_PORT_MODERN')))], // Socket
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST_MODERN'), getenv('ROS_PORT_MODERN')))], // Socket
[new ResourceStream(STDIN), false], // Try it, but do not close STDIN please !!!
[new StringStream('Hello World !!!')], // Try it, but do not close STDIN please !!!
[new StringStream('')], // Try it, but do not close STDIN please !!!
Expand Down
73 changes: 36 additions & 37 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use RouterOS\Client;
use RouterOS\Exceptions\ConfigException;
use RouterOS\Exceptions\QueryException;
use RouterOS\Query;
use RouterOS\Config;
use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConnectException;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function setUp(): void
$this->config = [
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
'host' => getenv('ROS_HOST_MODERN'),
'ssh_port' => (int) getenv('ROS_SSH_PORT'),
];

Expand All @@ -66,11 +65,11 @@ public function testConstruct(): void
->set('host', $this->config['host']);

$obj = new Client($config);
$this->assertIsObject($obj);
self::assertIsObject($obj);
$socket = $obj->getSocket();
$this->assertIsResource($socket);
self::assertIsResource($socket);
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
self::assertStringContainsString('Must be initialized ', $e->getMessage());
}
}

Expand All @@ -79,23 +78,23 @@ public function testConstruct2(): void
try {
$config = new Config($this->config);
$obj = new Client($config);
$this->assertIsObject($obj);
self::assertIsObject($obj);
$socket = $obj->getSocket();
$this->assertIsResource($socket);
self::assertIsResource($socket);
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
self::assertStringContainsString('Must be initialized ', $e->getMessage());
}
}

public function testConstruct3(): void
{
try {
$obj = new Client($this->config);
$this->assertIsObject($obj);
self::assertIsObject($obj);
$socket = $obj->getSocket();
$this->assertIsResource($socket);
self::assertIsResource($socket);
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
self::assertStringContainsString('Must be initialized ', $e->getMessage());
}
}

Expand Down Expand Up @@ -128,13 +127,13 @@ public function testConstructLegacy(): void
$obj = new Client([
'user' => $this->config['user'],
'pass' => $this->config['pass'],
'host' => $this->config['host'],
'host' => getenv('ROS_HOST_LEGACY'),
'port' => $this->port_legacy,
'legacy' => true,
]);
$this->assertIsObject($obj);
self::assertIsObject($obj);
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
self::assertStringContainsString('Must be initialized ', $e->getMessage());
}
}

Expand All @@ -153,9 +152,9 @@ public function testConstructLegacy2(): void
'port' => $this->port_legacy,
'legacy' => false,
]);
$this->assertIsObject($obj);
self::assertIsObject($obj);
} catch (Exception $e) {
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
self::assertStringContainsString('Must be initialized ', $e->getMessage());
}
}

Expand Down Expand Up @@ -205,7 +204,7 @@ public function testPregResponse(string $line, array $result): void
{
$matches = [];
$this->client->pregResponse($line, $matches);
$this->assertEquals($matches, $result);
self::assertEquals($matches, $result);
}

public function testQueryRead(): void
Expand All @@ -215,22 +214,22 @@ public function testQueryRead(): void
*/

$read = $this->client->query('/system/package/print', ['name'])->read();
$this->assertNotEmpty($read);
self::assertNotEmpty($read);

$read = $this->client->query('/system/package/print', ['.id', '*1'])->read();
$this->assertCount(1, $read);
self::assertCount(1, $read);

$read = $this->client->query('/system/package/print', ['.id', '=', '*1'])->read();
$this->assertCount(1, $read);
self::assertCount(1, $read);

$read = $this->client->query('/system/package/print', [['name']])->read();
$this->assertNotEmpty($read);
self::assertNotEmpty($read);

$read = $this->client->query('/system/package/print', [['.id', '*1']])->read();
$this->assertCount(1, $read);
self::assertCount(1, $read);

$read = $this->client->query('/system/package/print', [['.id', '=', '*1']])->read();
$this->assertCount(1, $read);
self::assertCount(1, $read);

/*
* Build query with operations
Expand All @@ -240,43 +239,43 @@ public function testQueryRead(): void
['type', 'ether'],
['type', 'vlan'],
], '|')->read();
$this->assertCount(1, $read);
$this->assertEquals('*1', $read[0]['.id']);
self::assertCount(1, $read);
self::assertEquals('*1', $read[0]['.id']);

/*
* Build query with tag
*/

$read = $this->client->query('/system/package/print', null, null, 'zzzz')->read();

// $this->assertCount(13, $read);
$this->assertEquals('zzzz', $read[0]['tag']);
// self::assertCount(13, $read);
self::assertEquals('zzzz', $read[0]['tag']);

/*
* Build query with option count
*/
$read = $this->client->query('/interface/monitor-traffic')->read(true, ['count' => 3]);
$this->assertCount(3, $read);
self::assertCount(3, $read);
}

public function testReadAsIterator(): void
{
$result = $this->client->query('/system/package/print')->readAsIterator();
$this->assertIsObject($result);
self::assertIsObject($result);
}

public function testWriteReadString(): void
{
$readTrap = $this->client->query('/interface')->read(false);
$this->assertCount(3, $readTrap);
$this->assertEquals('!trap', $readTrap[0]);
self::assertCount(3, $readTrap);
self::assertEquals('!trap', $readTrap[0]);
}

public function testFatal(): void
{
$readTrap = $this->client->query('/quit')->read();
$this->assertCount(2, $readTrap);
$this->assertEquals('!fatal', $readTrap[0]);
self::assertCount(2, $readTrap);
self::assertEquals('!fatal', $readTrap[0]);
}

public function queryExceptionDataProvider(): array
Expand Down Expand Up @@ -313,20 +312,20 @@ public function testQueryException(string $exception, $endpoint, $attributes): v
public function testExportMethod(): void
{
if (!in_array(gethostname(), ['pasha-lt', 'pasha-pc'])) {
$this->markTestSkipped('Travis does not allow to use SSH protocol on testing stage');
self::markTestSkipped('Travis does not allow to use SSH protocol on testing stage');
}

$result = $this->client->export();
$this->assertNotEmpty($result);
self::assertNotEmpty($result);
}

public function testExportQuery(): void
{
if (!in_array(gethostname(), ['pasha-lt', 'pasha-pc'])) {
$this->markTestSkipped('Travis does not allow to use SSH protocol on testing stage');
self::markTestSkipped('Travis does not allow to use SSH protocol on testing stage');
}

$result = $this->client->query('/export');
$this->assertNotEmpty($result);
self::assertNotEmpty($result);
}
}
2 changes: 1 addition & 1 deletion tests/ResponseIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp(): void
$this->client = new Client([
'user' => getenv('ROS_USER'),
'pass' => getenv('ROS_PASS'),
'host' => getenv('ROS_HOST'),
'host' => getenv('ROS_HOST_MODERN'),
]);
}

Expand Down
Loading

0 comments on commit dd68789

Please sign in to comment.