Skip to content

Commit dd68789

Browse files
Support of PHP 8.0 implemented, tests fixed, for #52
1 parent 8149731 commit dd68789

File tree

9 files changed

+104
-77
lines changed

9 files changed

+104
-77
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ php:
1414
- '7.2'
1515
- '7.3'
1616
- '7.4'
17+
- '8.0'
1718

1819
before_script:
1920
- sudo apt-get update

composer.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,17 @@
4141
}
4242
},
4343
"require": {
44-
"php": "^7.2",
44+
"php": "^7.2|^8.0",
4545
"ext-sockets": "*",
4646
"divineomega/php-ssh-connection": "^2.2"
4747
},
4848
"require-dev": {
4949
"friendsofphp/php-cs-fixer": "^2.16",
5050
"limedeck/phpunit-detailed-printer": "^5.0",
5151
"orchestra/testbench": "^4.0|^5.0",
52-
"phpstan/phpstan": "^0.12.32",
53-
"phpstan/phpstan-strict-rules": "^0.12.2",
5452
"phpunit/phpunit": "^8.0",
55-
"rector/rector": "^0.7.41",
53+
"rector/rector": "^0.7|^0.8|^0.9",
5654
"roave/security-advisories": "dev-master",
57-
"thecodingmachine/phpstan-strict-rules": "^0.12.0",
5855
"squizlabs/php_codesniffer": "^3.5",
5956
"larapack/dd": "^1.1"
6057
},
@@ -66,11 +63,9 @@
6663
"lint": "rector process src && php-cs-fixer fix -v",
6764
"test:lint": "php-cs-fixer fix -v --dry-run",
6865
"test:rector": "rector process src --dry-run",
69-
"test:types": "phpstan analyse --ansi --memory-limit=0",
7066
"test:unit": "phpunit --coverage-clover clover.xml",
7167
"test": [
7268
"@test:lint",
73-
"@test:types",
7469
"@test:unit"
7570
]
7671
}

phpunit.local.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
printerClass="LimeDeck\Testing\Printer"
10+
processIsolation="false"
11+
stopOnFailure="true">
12+
<filter>
13+
<whitelist processUncoveredFilesFromWhitelist="true">
14+
<directory suffix=".php">./src</directory>
15+
<exclude>
16+
<directory suffix="Test.php">./tests</directory>
17+
</exclude>
18+
</whitelist>
19+
</filter>
20+
<logging>
21+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
22+
</logging>
23+
<testsuites>
24+
<testsuite name="RouterOS API on PHP tests">
25+
<directory suffix=".php">./tests/</directory>
26+
</testsuite>
27+
</testsuites>
28+
<php>
29+
<env name="ROS_HOST_LEGACY" value="routeros-6-42"/>
30+
<env name="ROS_HOST_MODERN" value="routeros-6-48"/>
31+
<env name="ROS_PORT_LEGACY" value="8728"/>
32+
<env name="ROS_PORT_MODERN" value="8728"/>
33+
<env name="ROS_USER" value="admin"/>
34+
<env name="ROS_PASS" value="admin"/>
35+
<env name="ROS_SSH_PORT" value="22"/>
36+
</php>
37+
</phpunit>

phpunit.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
</testsuite>
2727
</testsuites>
2828
<php>
29-
<env name="ROS_HOST" value="127.0.0.1"/>
30-
<env name="ROS_USER" value="admin"/>
31-
<env name="ROS_PASS" value="admin"/>
29+
<env name="ROS_HOST_LEGACY" value="127.0.0.1"/>
30+
<env name="ROS_HOST_MODERN" value="127.0.0.1"/>
3231
<env name="ROS_PORT_MODERN" value="8728"/>
3332
<env name="ROS_PORT_LEGACY" value="18728"/>
33+
<env name="ROS_USER" value="admin"/>
34+
<env name="ROS_PASS" value="admin"/>
3435
<env name="ROS_SSH_PORT" value="22222"/>
3536
</php>
3637
</phpunit>

src/Streams/ResourceStream.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,15 @@ class ResourceStream implements StreamInterface
2525
public function __construct($stream)
2626
{
2727
if (!is_resource($stream)) {
28-
throw new \InvalidArgumentException(
29-
sprintf(
30-
'Argument must be a valid resource type. %s given.',
31-
gettype($stream)
32-
)
33-
);
28+
throw new \InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.', gettype($stream)));
3429
}
3530

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

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

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

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

6260
/**
63-
* @inheritDoc
61+
* {@inheritDoc}
6462
*
6563
* @throws \RouterOS\Exceptions\StreamException when not possible to write bytes
6664
*/
@@ -70,8 +68,11 @@ public function write(string $string, int $length = null): int
7068
$length = strlen($string);
7169
}
7270

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

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

8384
/**
84-
* @inheritDoc
85+
* {@inheritDoc}
8586
*
8687
* @throws \RouterOS\Exceptions\StreamException when not possible to close the stream
8788
*/

tests/APIConnectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function constructProvider(): array
3838
{
3939
return [
4040
[new ResourceStream(fopen(__FILE__, 'rb'))], // Myself, sure I exists
41-
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST'), getenv('ROS_PORT_MODERN')))], // Socket
41+
[new ResourceStream(fsockopen('tcp://' . getenv('ROS_HOST_MODERN'), getenv('ROS_PORT_MODERN')))], // Socket
4242
[new ResourceStream(STDIN), false], // Try it, but do not close STDIN please !!!
4343
[new StringStream('Hello World !!!')], // Try it, but do not close STDIN please !!!
4444
[new StringStream('')], // Try it, but do not close STDIN please !!!

tests/ClientTest.php

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use RouterOS\Client;
88
use RouterOS\Exceptions\ConfigException;
99
use RouterOS\Exceptions\QueryException;
10-
use RouterOS\Query;
1110
use RouterOS\Config;
1211
use RouterOS\Exceptions\ClientException;
1312
use RouterOS\Exceptions\ConnectException;
@@ -40,7 +39,7 @@ public function setUp(): void
4039
$this->config = [
4140
'user' => getenv('ROS_USER'),
4241
'pass' => getenv('ROS_PASS'),
43-
'host' => getenv('ROS_HOST'),
42+
'host' => getenv('ROS_HOST_MODERN'),
4443
'ssh_port' => (int) getenv('ROS_SSH_PORT'),
4544
];
4645

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

6867
$obj = new Client($config);
69-
$this->assertIsObject($obj);
68+
self::assertIsObject($obj);
7069
$socket = $obj->getSocket();
71-
$this->assertIsResource($socket);
70+
self::assertIsResource($socket);
7271
} catch (Exception $e) {
73-
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
72+
self::assertStringContainsString('Must be initialized ', $e->getMessage());
7473
}
7574
}
7675

@@ -79,23 +78,23 @@ public function testConstruct2(): void
7978
try {
8079
$config = new Config($this->config);
8180
$obj = new Client($config);
82-
$this->assertIsObject($obj);
81+
self::assertIsObject($obj);
8382
$socket = $obj->getSocket();
84-
$this->assertIsResource($socket);
83+
self::assertIsResource($socket);
8584
} catch (Exception $e) {
86-
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
85+
self::assertStringContainsString('Must be initialized ', $e->getMessage());
8786
}
8887
}
8988

9089
public function testConstruct3(): void
9190
{
9291
try {
9392
$obj = new Client($this->config);
94-
$this->assertIsObject($obj);
93+
self::assertIsObject($obj);
9594
$socket = $obj->getSocket();
96-
$this->assertIsResource($socket);
95+
self::assertIsResource($socket);
9796
} catch (Exception $e) {
98-
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
97+
self::assertStringContainsString('Must be initialized ', $e->getMessage());
9998
}
10099
}
101100

@@ -128,13 +127,13 @@ public function testConstructLegacy(): void
128127
$obj = new Client([
129128
'user' => $this->config['user'],
130129
'pass' => $this->config['pass'],
131-
'host' => $this->config['host'],
130+
'host' => getenv('ROS_HOST_LEGACY'),
132131
'port' => $this->port_legacy,
133132
'legacy' => true,
134133
]);
135-
$this->assertIsObject($obj);
134+
self::assertIsObject($obj);
136135
} catch (Exception $e) {
137-
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
136+
self::assertStringContainsString('Must be initialized ', $e->getMessage());
138137
}
139138
}
140139

@@ -153,9 +152,9 @@ public function testConstructLegacy2(): void
153152
'port' => $this->port_legacy,
154153
'legacy' => false,
155154
]);
156-
$this->assertIsObject($obj);
155+
self::assertIsObject($obj);
157156
} catch (Exception $e) {
158-
$this->assertStringContainsString('Must be initialized ', $e->getMessage());
157+
self::assertStringContainsString('Must be initialized ', $e->getMessage());
159158
}
160159
}
161160

@@ -205,7 +204,7 @@ public function testPregResponse(string $line, array $result): void
205204
{
206205
$matches = [];
207206
$this->client->pregResponse($line, $matches);
208-
$this->assertEquals($matches, $result);
207+
self::assertEquals($matches, $result);
209208
}
210209

211210
public function testQueryRead(): void
@@ -215,22 +214,22 @@ public function testQueryRead(): void
215214
*/
216215

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

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

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

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

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

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

235234
/*
236235
* Build query with operations
@@ -240,43 +239,43 @@ public function testQueryRead(): void
240239
['type', 'ether'],
241240
['type', 'vlan'],
242241
], '|')->read();
243-
$this->assertCount(1, $read);
244-
$this->assertEquals('*1', $read[0]['.id']);
242+
self::assertCount(1, $read);
243+
self::assertEquals('*1', $read[0]['.id']);
245244

246245
/*
247246
* Build query with tag
248247
*/
249248

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

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

255254
/*
256255
* Build query with option count
257256
*/
258257
$read = $this->client->query('/interface/monitor-traffic')->read(true, ['count' => 3]);
259-
$this->assertCount(3, $read);
258+
self::assertCount(3, $read);
260259
}
261260

262261
public function testReadAsIterator(): void
263262
{
264263
$result = $this->client->query('/system/package/print')->readAsIterator();
265-
$this->assertIsObject($result);
264+
self::assertIsObject($result);
266265
}
267266

268267
public function testWriteReadString(): void
269268
{
270269
$readTrap = $this->client->query('/interface')->read(false);
271-
$this->assertCount(3, $readTrap);
272-
$this->assertEquals('!trap', $readTrap[0]);
270+
self::assertCount(3, $readTrap);
271+
self::assertEquals('!trap', $readTrap[0]);
273272
}
274273

275274
public function testFatal(): void
276275
{
277276
$readTrap = $this->client->query('/quit')->read();
278-
$this->assertCount(2, $readTrap);
279-
$this->assertEquals('!fatal', $readTrap[0]);
277+
self::assertCount(2, $readTrap);
278+
self::assertEquals('!fatal', $readTrap[0]);
280279
}
281280

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

319318
$result = $this->client->export();
320-
$this->assertNotEmpty($result);
319+
self::assertNotEmpty($result);
321320
}
322321

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

329328
$result = $this->client->query('/export');
330-
$this->assertNotEmpty($result);
329+
self::assertNotEmpty($result);
331330
}
332331
}

tests/ResponseIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp(): void
1717
$this->client = new Client([
1818
'user' => getenv('ROS_USER'),
1919
'pass' => getenv('ROS_PASS'),
20-
'host' => getenv('ROS_HOST'),
20+
'host' => getenv('ROS_HOST_MODERN'),
2121
]);
2222
}
2323

0 commit comments

Comments
 (0)