Skip to content

Commit cf953e9

Browse files
committed
Added unit tests for some of the new functionality;
Util::prepareScript() now works properly with stream arguments; Doc and CS fixes.
1 parent b42f324 commit cf953e9

File tree

9 files changed

+185
-64
lines changed

9 files changed

+185
-64
lines changed

RELEASE-1.0.0b5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Console and some overall improvements.
88
* Util::get() now uses RouterOS' "get" command, unless it returns an empty !done response (as it does for RouterOS versions prior to 6.0), in which case it automatically fallbacks to a print with a query.
99
* Util::escapeValue() and Util::parseValue() now support associative arrays (introduced in RouterOS 6.2).
1010
* Util::add() and Util::set()/Util::edit() now support flags as values with a numeric key.
11+
* Util::changeMenu() now returns the Util object itself, except when you supply an empty string, when the current menu is returned (as before).
1112
* ResponseCollection can now be searched by argument values, if you first designate an argument name with the new ResponseCollection::setIndex() method.
1213
* Doc fixes (Notably: Clarified the acceptability of seekable streams as argument values, which has been present for a long time, but never documented).
1314
* CS fixes.

package.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<email>[email protected]</email>
1212
<active>yes</active>
1313
</lead>
14-
<date>2013-10-24</date>
15-
<time>18:18:13</time>
14+
<date>2013-10-26</date>
15+
<time>19:56:36</time>
1616
<version>
1717
<release>1.0.0b5</release>
1818
<api>1.0.0</api>
@@ -31,6 +31,8 @@
3131
- prepareScript()
3232
* Util::get() now uses RouterOS' &quot;get&quot; command, unless it returns an empty !done response (as it does for RouterOS versions prior to 6.0), in which case it automatically fallbacks to a print with a query.
3333
* Util::escapeValue() and Util::parseValue() now support associative arrays (introduced in RouterOS 6.2).
34+
* Util::add() and Util::set()/Util::edit() now support flags as values with a numeric key.
35+
* Util::changeMenu() now returns the Util object itself, except when you supply an empty string, when the current menu is returned (as before).
3436
* ResponseCollection can now be searched by argument values, if you first designate an argument name with the new ResponseCollection::setIndex() method.
3537
* Doc fixes (Notably: Clarified the acceptability of seekable streams as argument values, which has been present for a long time, but never documented).
3638
* CS fixes.</notes>
@@ -67,6 +69,8 @@
6769
</file>
6870
<file role="script" name="roscon.php">
6971
<tasks:replace type="pear-config" to="php_dir" from="../src"/>
72+
<tasks:replace type="pear-config" to="php_dir" from="../../Net_Transmitter.git/src"/>
73+
<tasks:replace type="pear-config" to="php_dir" from="../../Console_Color.git/src"/>
7074
<tasks:replace type="pear-config" to="data_dir" from="@PEAR2_DATA_DIR@"/>
7175
<tasks:replace type="package-info" to="channel" from="@PACKAGE_CHANNEL@"/>
7276
<tasks:replace type="package-info" to="name" from="@PACKAGE_NAME@"/>
@@ -222,6 +226,8 @@
222226
</dir>
223227
<file role="test" name="bootstrap.php">
224228
<tasks:replace type="pear-config" to="php_dir" from="../src"/>
229+
<tasks:replace type="pear-config" to="php_dir" from="../../Net_Transmitter.git/src"/>
230+
<tasks:replace type="pear-config" to="php_dir" from="../../Cache_SHM.git/src"/>
225231
<tasks:replace type="package-info" to="version" from="GIT: $Id$"/>
226232
</file>
227233
<file role="test" name="ConnectionTest.php"/>

packagexmlsetup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343
'type' => 'pear-config',
4444
'to' => 'php_dir'
4545
),
46-
'../../PEAR2_Net_Transmitter.git/src' => array(
46+
'../../Net_Transmitter.git/src' => array(
4747
'type' => 'pear-config',
4848
'to' => 'php_dir'
4949
),
50-
'../../PEAR2_Cache_SHM.git/src' => array(
50+
'../../Cache_SHM.git/src' => array(
5151
'type' => 'pear-config',
5252
'to' => 'php_dir'
5353
),
54-
'../../PEAR2_Console_Color.git/src' => array(
54+
'../../Console_Color.git/src' => array(
5555
'type' => 'pear-config',
5656
'to' => 'php_dir'
5757
),

src/PEAR2/Net/RouterOS/ResponseCollection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ public function __construct(array $responses)
113113
* function. Depending on the argument given, one of the other functions in
114114
* the class is invoked and its returned value is returned by this function.
115115
*
116-
* @param int $offset The offset of the response to seek to. Setting NULL
117-
* will seek to the last response.
116+
* @param int|string $offset The offset of the response to seek to.
117+
* If the collection is indexed, you can also supply a value to seek to.
118+
* Setting NULL will seek to the last response.
118119
*
119120
* @return Response The {@link Response} at the specified index, last
120121
* reponse if no index is provided or FALSE if the index is invalid or the
@@ -129,9 +130,12 @@ public function __invoke($offset = null)
129130
* Sets an argument to be usable as a key in the collection.
130131
*
131132
* @param string|null $name The name of the argument to use. Future calls
132-
* that accept a position will then also be able to search that argument for
133-
* a matching value. Specifying NULL will disable such lookups (as is by
134-
* default).
133+
* that accept a position will then also be able to search that argument
134+
* for a matching value.
135+
* Specifying NULL will disable such lookups (as is by default).
136+
* Note that in case this value occures multiple times within the
137+
* collection, only the last matching response will be accessible by
138+
* that value.
135139
*
136140
* @return $this The object itself.
137141
*/

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ public static function prepareScript(
223223
array $params = array(),
224224
$stream = null
225225
) {
226-
$resultStream = $stream ?: fopen('php://temp', 'r+b');
226+
$isStreamGiven = Stream::isStream($stream);
227+
$resultStream = $isStreamGiven ? $stream : fopen('php://temp', 'r+b');
227228
$writer = new Stream($resultStream, false);
228229
$bytes = 0;
229230

@@ -234,7 +235,7 @@ public static function prepareScript(
234235
$reader = new Stream($pvalue, false);
235236
$chunkSize = $reader->getChunk(Stream::DIRECTION_RECEIVE);
236237
$bytes += $writer->send('"');
237-
while ($reader->isDataAwaiting()) {
238+
while ($reader->isAvailable() && $reader->isDataAwaiting()) {
238239
$bytes += $writer->send(
239240
static::escapeString(fread($pvalue, $chunkSize))
240241
);
@@ -246,7 +247,7 @@ public static function prepareScript(
246247
}
247248

248249
$bytes += $writer->send($source);
249-
if ($stream) {
250+
if ($isStreamGiven) {
250251
return $bytes;
251252
}
252253

@@ -396,16 +397,16 @@ public function __construct(Client $client)
396397
* or CLI syntax and can be either absolute or relative. If relative,
397398
* it's relative to the current menu, which by default is the root.
398399
*
399-
* @return string The old menu. If an empty string is given for a new menu,
400-
* no change is performed, and the current menu is returned.
400+
* @return $this|string The object itself. If an empty string is given for
401+
* a new menu, no change is performed, and the current menu is returned.
401402
*/
402403
public function changeMenu($newMenu = '')
403404
{
404405
$newMenu = (string)$newMenu;
405406
if ('' === $newMenu) {
406407
return $this->menu;
407408
}
408-
$oldMenu = $this->menu;
409+
409410
$menuRequest = new Request('/menu');
410411
if ('/' === $newMenu[0]) {
411412
$this->menu = $menuRequest->setCommand($newMenu)->getCommand();
@@ -416,7 +417,7 @@ public function changeMenu($newMenu = '')
416417
)->getCommand();
417418
}
418419
$this->clearIdCache();
419-
return $oldMenu;
420+
return $this;
420421
}
421422

422423
/**

tests/Client/Unsafe.php

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public function testSendSyncReturningResponseLargeDataException()
305305
//Clearing out for other tests.
306306
ini_set('memory_limit', $memoryLimit);
307307
}
308-
308+
309309
public function testResponseCollectionGetArgumentMap()
310310
{
311311
$addRequest = new Request('/queue/simple/add');
@@ -332,9 +332,54 @@ public function testResponseCollectionGetArgumentMap()
332332
$removeRequest = new Request('/queue/simple/remove');
333333
$removeRequest->setArgument('numbers', TEST_QUEUE_NAME);
334334
$response = $this->object->sendSync($removeRequest);
335+
} else {
336+
$this->fail('Failed to add test queue.');
335337
}
336338
}
337-
339+
340+
public function testResponseCollectionIndex()
341+
{
342+
$queueComment = 'API_TEST';
343+
$addRequest = new Request('/queue/simple/add');
344+
$addRequest->setArgument('name', TEST_QUEUE_NAME)
345+
->setArgument('target', '0.0.0.0/0')
346+
->setArgument('comment', $queueComment);
347+
$responses = $this->object->sendSync($addRequest);
348+
if (count($responses) === 1
349+
&& $responses->getLast()->getType() === Response::TYPE_FINAL
350+
) {
351+
$printRequest = new Request('/queue/simple/print');
352+
$printRequest->setArgument('.proplist', 'name,comment');
353+
$responses = $this->object->sendSync($printRequest)
354+
->setIndex('name');
355+
$this->assertSame('name', $responses->getIndex());
356+
357+
$this->assertSame(
358+
$queueComment,
359+
$responses[TEST_QUEUE_NAME]('comment')
360+
);
361+
$this->assertSame(
362+
count($responses->toArray(true)),
363+
count($responses->toArray(false)) - 1//!done
364+
);
365+
366+
$this->assertNotSame(
367+
$responses->current(),
368+
$responses->seek(TEST_QUEUE_NAME)
369+
);
370+
$this->assertSame(
371+
$responses->current(),
372+
$responses[TEST_QUEUE_NAME]
373+
);
374+
375+
$removeRequest = new Request('/queue/simple/remove');
376+
$removeRequest->setArgument('numbers', TEST_QUEUE_NAME);
377+
$response = $this->object->sendSync($removeRequest);
378+
} else {
379+
$this->fail('Failed to add test queue.');
380+
}
381+
}
382+
338383
public function testSetCharset()
339384
{
340385
if (!extension_loaded('iconv') || !function_exists('iconv')) {
@@ -352,8 +397,7 @@ public function testSetCharset()
352397
)
353398
)
354399
);
355-
356-
400+
357401
$addRequest = new Request('/queue/simple/add');
358402
$addRequest->setArgument('name', TEST_QUEUE_NAME)
359403
->setArgument('target', '0.0.0.0/0');
@@ -376,7 +420,7 @@ public function testSetCharset()
376420
$printRequest = new Request('/queue/simple/print');
377421
$printRequest->setQuery(Query::where('name', TEST_QUEUE_NAME));
378422
$responses = $this->object->sendSync($printRequest);
379-
423+
380424
$this->assertEquals(
381425
TEST_QUEUE_NAME,
382426
$responses[0]->getArgument('name')
@@ -385,15 +429,15 @@ public function testSetCharset()
385429
'ПРИМЕР',
386430
$responses[0]->getArgument('comment')
387431
);
388-
432+
389433
$this->object->setCharset($appropriateCharsets);
390434
$this->assertNotEquals(
391435
'ПРИМЕР',
392436
$responses[0]->getArgument('comment')
393437
);
394-
438+
395439
$responses = $this->object->sendSync($printRequest);
396-
440+
397441
$this->assertEquals(
398442
TEST_QUEUE_NAME,
399443
$responses[0]->getArgument('name')
@@ -402,7 +446,7 @@ public function testSetCharset()
402446
'ПРИМЕР',
403447
$responses[0]->getArgument('comment')
404448
);
405-
449+
406450
$this->object->setCharset(
407451
'ISO-8859-1',
408452
Communicator::CHARSET_REMOTE
@@ -412,7 +456,7 @@ public function testSetCharset()
412456
'ПРИМЕР',
413457
$responses[0]->getArgument('comment')
414458
);
415-
459+
416460
$this->object->setCharset(
417461
'ISO-8859-1',
418462
Communicator::CHARSET_LOCAL
@@ -422,14 +466,14 @@ public function testSetCharset()
422466
'ПРИМЕР',
423467
$responses[0]->getArgument('comment')
424468
);
425-
469+
426470
$this->object->setCharset($appropriateCharsets);
427471
$responses = $this->object->sendSync($printRequest);
428472
$this->assertEquals(
429473
'ПРИМЕР',
430474
$responses[0]->getArgument('comment')
431475
);
432-
476+
433477
$this->object->setStreamingResponses(true);
434478
$responses = $this->object->sendSync($printRequest);
435479
$this->assertEquals(
@@ -457,7 +501,7 @@ public function testSetCharset()
457501
$responses[0]->getArgument('comment')
458502
)
459503
);
460-
504+
461505
$testQueueNameStream = fopen('php://temp', 'r+b');
462506
fwrite($testQueueNameStream, TEST_QUEUE_NAME);
463507
rewind($testQueueNameStream);
@@ -479,8 +523,7 @@ public function testSetCharset()
479523
$responses[0]->getArgument('comment')
480524
)
481525
);
482-
483-
526+
484527
$removeRequest = new Request('/queue/simple/remove');
485528
$removeRequest->setArgument('numbers', TEST_QUEUE_NAME);
486529
$responses = $this->object->sendSync($removeRequest);

tests/HandlingTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,33 @@ public function testTaglessModePassing()
11641164
$reg1->close();
11651165
$this->assertStringStartsWith('-1_', $reg2->getOwnershipTag());
11661166
}
1167-
1167+
1168+
public function testPrepareScript()
1169+
{
1170+
$msg = 'testing';
1171+
$result = Util::prepareScript(
1172+
'/log print $msg',
1173+
array('msg' => $msg)
1174+
);
1175+
$this->assertSame(
1176+
":local \"msg\" \"{$msg}\";\n/log print \$msg",
1177+
stream_get_contents($result)
1178+
);
1179+
1180+
$testParam = fopen('php://temp', 'r+b');
1181+
fwrite($testParam, $msg);
1182+
rewind($testParam);
1183+
$result = Util::prepareScript(
1184+
'/log print $msg',
1185+
array('msg' => $testParam)
1186+
);
1187+
$this->assertSame(
1188+
":local \"msg\" \"{$msg}\";\n/log print \$msg",
1189+
stream_get_contents($result)
1190+
);
1191+
$this->assertSame(strlen($msg), ftell($testParam));
1192+
}
1193+
11681194
/**
11691195
* @param string $value
11701196
* @param mixed $expected

tests/Util/Safe.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,34 @@ abstract class Safe extends PHPUnit_Framework_TestCase
2727
public function testChangeMenu()
2828
{
2929
$this->assertSame('/', $this->util->changeMenu());
30-
$this->assertSame('/', $this->util->changeMenu('queue'));
31-
$this->assertSame('/queue', $this->util->changeMenu('simple'));
32-
$this->assertSame('/queue/simple', $this->util->changeMenu('.. tree'));
33-
$this->assertSame('/queue/tree', $this->util->changeMenu('../type'));
34-
$this->assertSame('/queue/type', $this->util->changeMenu('/interface'));
35-
$this->assertSame('/interface', $this->util->changeMenu('/ip/arp'));
36-
$this->assertSame('/ip/arp', $this->util->changeMenu('/ip hotspot'));
37-
$this->assertSame('/ip/hotspot', $this->util->changeMenu());
30+
$this->assertSame(
31+
'/queue',
32+
$this->util->changeMenu('queue')->changeMenu()
33+
);
34+
$this->assertSame(
35+
'/queue/simple',
36+
$this->util->changeMenu('simple')->changeMenu()
37+
);
38+
$this->assertSame(
39+
'/queue/tree',
40+
$this->util->changeMenu('.. tree')->changeMenu()
41+
);
42+
$this->assertSame(
43+
'/queue/type',
44+
$this->util->changeMenu('../type')->changeMenu()
45+
);
46+
$this->assertSame(
47+
'/interface',
48+
$this->util->changeMenu('/interface')->changeMenu()
49+
);
50+
$this->assertSame(
51+
'/ip/arp',
52+
$this->util->changeMenu('/ip/arp')->changeMenu()
53+
);
54+
$this->assertSame(
55+
'/ip/hotspot',
56+
$this->util->changeMenu('/ip hotspot')->changeMenu()
57+
);
3858
}
3959

4060
public function testFindByQuery()

0 commit comments

Comments
 (0)