Skip to content

Commit c7b00b5

Browse files
committed
Added Util::unsetValue();
Additional tests and doc fixes.
1 parent b9ca3d7 commit c7b00b5

File tree

5 files changed

+136
-45
lines changed

5 files changed

+136
-45
lines changed

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public static function escapeValue($value)
8989
$value = $value ? 'true' : 'false';
9090
break;
9191
case 'array':
92+
if (0 === count($value)) {
93+
$value = '{}';
94+
break;
95+
}
9296
$result = '';
9397
foreach ($value as $val) {
9498
$result .= ';' . static::escapeValue($val);
@@ -447,8 +451,9 @@ public function set($numbers, array $newValues)
447451
foreach ($newValues as $name => $value) {
448452
$setRequest->setArgument($name, $value);
449453
}
450-
$setRequest->setArgument('numbers', $this->find($numbers));
451-
return $this->client->sendSync($setRequest);
454+
return $this->client->sendSync(
455+
$setRequest->setArgument('numbers', $this->find($numbers))
456+
);
452457
}
453458

454459
/**
@@ -467,6 +472,28 @@ public function edit($numbers, array $newValues)
467472
return $this->set($numbers, $newValues);
468473
}
469474

475+
/**
476+
* Unsets a value of a specified entry at the current menu.
477+
*
478+
* Equivalent of scripting's "unset" command. The "Value" part in the method
479+
* name is added because "unset" is a language construct, and thus a
480+
* reserved word.
481+
*
482+
* @param mixed $numbers Targeted entries. Can be any criteria accepted
483+
* by {@link find()}.
484+
* @param string $value_name The name of the value you want to unset.
485+
*
486+
* @return ResponseCollection
487+
*/
488+
public function unsetValue($numbers, $value_name)
489+
{
490+
$unsetRequest = new Request($this->menu . '/unset');
491+
return $this->client->sendSync(
492+
$unsetRequest->setArgument('numbers', $this->find($numbers))
493+
->setArgument('value-name', $value_name)
494+
);
495+
}
496+
470497
/**
471498
* Adds a new entry at the current menu.
472499
*
@@ -537,58 +564,59 @@ public function move($numbers, $destination)
537564
public function filePutContents($filename, $data, $overwrite = false)
538565
{
539566
$printRequest = new Request(
540-
'/file/print .proplist="size"',
567+
'/file/print .proplist=""',
541568
Query::where('name', $filename)
542569
);
543570
if (!$overwrite && count($this->client->sendSync($printRequest)) > 1) {
544571
return false;
545572
}
546-
$source = <<<'NEWDOC'
547-
/file
548-
print file=$filename where name=""
549-
NEWDOC;
550-
$this->exec(
551-
$source,
552-
array('filename' => $filename)
573+
$result = $this->client->sendSync(
574+
$printRequest->setArgument('file', $filename)
553575
);
576+
if (count($result->getAllOfType(Response::TYPE_ERROR)) > 0) {
577+
return false;
578+
}
579+
//Required for RouterOS to write the initial file.
554580
sleep(2);
555-
$source = <<<'NEWDOC'
556-
/file
557-
set numbers=$filename contents=""
558-
set numbers=$filename contents=$data
559-
NEWDOC;
560-
$this->exec(
561-
$source,
562-
array('filename' => $filename, 'data' => $data)
563-
);
581+
$setRequest = new Request('/file/set contents=""');
582+
$setRequest->setArgument('numbers', $filename);
583+
$this->client->sendSync($setRequest);
584+
$this->client->sendSync($setRequest->setArgument('contents', $data));
585+
//Required for RouterOS to write the file's new contents.
564586
sleep(2);
565587
return strlen($data) == $this->client->sendSync(
566-
$printRequest
588+
$printRequest->setArgument('file', null)
589+
->setArgument('.proplist', 'size')
567590
)->getArgument('size');
568591
}
569592

570593
/**
571594
* Gets the contents of a specified file.
572595
*
573-
* @param string $filename The name of the file to get contents of.
596+
* @param string $filename The name of the file to get the contents of.
597+
* @param string $tmpScriptName In order to get the file's contents, a
598+
* script is created at "/system script" with a random name, the
599+
* source of which is then overwriten with the file's contents, and
600+
* finally retrieved. To eliminate any possibility of name clashes, you
601+
* can specify your own name for the script.
574602
*
575603
* @return string|bool The contents of the file or FALSE if there is no such
576604
* file.
577605
*/
578-
public function fileGetContents($filename)
606+
public function fileGetContents($filename, $tmpScriptName = null)
579607
{
580608
$checkRequest = new Request(
581609
'/file print',
582610
Query::where('name', $filename)
583611
);
584-
if (1 === $this->client->sendSync($checkRequest)) {
612+
if (1 === count($this->client->sendSync($checkRequest))) {
585613
return false;
586614
}
587615
$name = $this->_exec(
588616
'/system script set $"_" source=[/file get $filename contents]',
589617
array('filename' => $filename),
590618
null,
591-
null,
619+
$tmpScriptName,
592620
true
593621
);
594622
$printRequest = new Request(

tests/RequestHandlingTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
class RequestHandlingTest extends \PHPUnit_Framework_TestCase
77
{
8-
98
public function testNonAbsoluteCommand()
109
{
1110
$nonAbsoluteCommands = array(

tests/UtilFeaturesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class UtilFeaturesTest extends \PHPUnit_Framework_TestCase
1010
* @var Util
1111
*/
1212
protected $util;
13+
1314
/**
1415
* @var Client
1516
*/

tests/UtilStateAlteringFeaturesTest.php

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,29 @@ class UtilStateAlteringFeaturesTest extends \PHPUnit_Framework_TestCase
1717
* @var Client
1818
*/
1919
protected $client;
20+
21+
/**
22+
* @var bool Whether connections should be persistent ones.
23+
*/
24+
protected $isPersistent = false;
2025

2126
protected function setUp()
2227
{
2328
$this->util = new Util(
24-
$this->client = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT)
29+
$this->client = new Client(
30+
\HOSTNAME,
31+
USERNAME,
32+
PASSWORD,
33+
PORT,
34+
$this->isPersistent
35+
)
2536
);
2637
}
2738

2839
protected function tearDown()
2940
{
3041
unset($this->util);
42+
$this->client->close();
3143
unset($this->client);
3244
}
3345

@@ -269,6 +281,30 @@ public function testGet()
269281
$this->assertFalse($nameTargetNot);
270282
}
271283

284+
/**
285+
* @depends testGet
286+
*
287+
* @return void
288+
*/
289+
public function testUnsetValue()
290+
{
291+
$value = 'all-p2p';
292+
$this->util->changeMenu('/queue/simple');
293+
$id = $this->util->add(
294+
array(
295+
'name' => TEST_QUEUE_NAME,
296+
'p2p' => $value
297+
)
298+
);
299+
$targetBefore = $this->util->get($id, 'p2p');
300+
$this->util->unsetValue($id, 'p2p');
301+
$targetAfter = $this->util->get($id, 'p2p');
302+
$this->util->remove($id);
303+
304+
$this->assertSame($value, $targetBefore);
305+
$this->assertNull($targetAfter);
306+
}
307+
272308
/**
273309
* @depends testRemove
274310
*
@@ -507,6 +543,34 @@ public function testExecArgValues()
507543
$this->assertCount(1, $results);
508544
$this->assertSame('hello,world', $results->getArgument('comment'));
509545

546+
$this->util->exec(
547+
'add name=$name comment=$comment',
548+
array(
549+
'name' => TEST_QUEUE_NAME,
550+
'comment' => array()
551+
)
552+
);
553+
$results = $this->client->sendSync(
554+
$printRequest
555+
)->getAllOfType(Response::TYPE_DATA);
556+
$this->util->remove(TEST_QUEUE_NAME);
557+
$this->assertCount(1, $results);
558+
$this->assertSame(null, $results->getArgument('comment'));
559+
560+
$this->util->exec(
561+
'add name=$name comment=$comment',
562+
array(
563+
'name' => TEST_QUEUE_NAME,
564+
'comment' => null
565+
)
566+
);
567+
$results = $this->client->sendSync(
568+
$printRequest
569+
)->getAllOfType(Response::TYPE_DATA);
570+
$this->util->remove(TEST_QUEUE_NAME);
571+
$this->assertCount(1, $results);
572+
$this->assertSame(null, $results->getArgument('comment'));
573+
510574
$this->util->exec(
511575
'add name=$name comment=$comment',
512576
array(
@@ -623,4 +687,18 @@ public function testFilePutAndGetContents()
623687
$this->assertSame($data2, $getResult3);
624688
$this->assertFalse($getResult4);
625689
}
690+
691+
public function testFilePutContentsNoPermissions()
692+
{
693+
$this->util = new Util(
694+
$this->client = new Client(
695+
\HOSTNAME,
696+
USERNAME2,
697+
PASSWORD2,
698+
PORT,
699+
$this->isPersistent
700+
)
701+
);
702+
$this->assertFalse($this->util->filePutContents(TEST_FILE_NAME, 'ok'));
703+
}
626704
}

tests/UtilStateAlteringPersistentFeaturesTest.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,8 @@
66
class UtilStateAlteringPersistentFeaturesTest
77
extends UtilStateAlteringFeaturesTest
88
{
9-
protected function setUp()
10-
{
11-
$this->util = new Util(
12-
$this->client = new Client(
13-
\HOSTNAME,
14-
USERNAME,
15-
PASSWORD,
16-
PORT,
17-
true
18-
)
19-
);
20-
}
21-
22-
protected function tearDown()
23-
{
24-
unset($this->util);
25-
$this->client->close();
26-
unset($this->client);
27-
}
9+
/**
10+
* @var bool Whether connections should be persistent ones.
11+
*/
12+
protected $isPersistent = true;
2813
}

0 commit comments

Comments
 (0)