Skip to content

Commit

Permalink
Added unit tests for Util::comment();
Browse files Browse the repository at this point in the history
Marked the test for Util::count()'s mode be incomplete, due to it being absent in the final PHP 5.6 builds;
Altered Client examples according to the Wiki;
Added Util example files, matching those in the Wiki;
Fixed the suggested firewall rule in Roscon's connection error message.
  • Loading branch information
boenrobot committed Mar 17, 2015
1 parent 2a0d4a0 commit e5705b1
Showing 26 changed files with 262 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

//Custom function, defined specifically for the example
$responseHandler = function ($response) {
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$addRequest = new RouterOS\Request('/ip/arp/add');

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$addRequest = new RouterOS\Request('/ip/arp/add');

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$addRequest = new RouterOS\Request('/ip/arp/add');

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$addRequest = new RouterOS\Request('/ip/arp/add');

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

require_once 'PEAR2/Autoload.php';

$client = new RouterOS\Client('192.168.0.1', 'admin');
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$responses = $client->sendSync(new RouterOS\Request('/ip/arp/print'));

16 changes: 16 additions & 0 deletions examples/Util/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
$util->add(
array(
'address' => '192.168.88.100',
'mac-address' => '00:00:00:00:00:01'
),
array(
'address' => '192.168.88.101',
'mac-address' => '00:00:00:00:00:02'
)
);
15 changes: 15 additions & 0 deletions examples/Util/count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');

//With function
echo count($util) . "\n";

//With method call
echo $util->count() . "\n";

//Count only disabled ARP items
echo $util->count(COUNT_NORMAL, RouterOS\Query::where('disabled', 'true')) . "\n";
9 changes: 9 additions & 0 deletions examples/Util/enable_disable_remove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
$util->remove(0);
$util->disable(Query::where('comment', 'DISABLE ME'));
$util->enable(1);
14 changes: 14 additions & 0 deletions examples/Util/exec-basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');

$util->exec('
add address=192.168.88.100 mac-address=00:00:00:00:00:01 comment=customer_1
add address=192.168.88.101 mac-address=00:00:00:00:00:02 comment=customer_2
/tool
fetch url="http://example.com/?name=customer_1"
fetch url="http://example.com/?name=customer_2"
');
28 changes: 28 additions & 0 deletions examples/Util/exec-params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');

$source = '
add address="192.168.88.$ip" mac-address="00:00:00:00:00:$mac" comment=$name
/tool
fetch url="http://example.com/?name=$name"
';
$util->exec(
$source,
array(
'ip' => 100,
'mac' => '01',
'name' => 'customer_1'
)
);
$util->exec(
$source,
array(
'ip' => 101,
'mac' => '02',
'name' => 'customer_2'
)
);
23 changes: 23 additions & 0 deletions examples/Util/exec-policy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/tool');

$url = $_GET['url'];//assume $_GET['url'] equals something akin to "http://example.com/geoip.rsc?filter=all"...

$source = '
fetch url=$db keep-result=yes dst-path=$filename
# Give the script time to be written onto disk
:delay 2
/import file=$filename
';
$util->exec(
$source,
array(
'db' => $url,
'filename' => pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME) //... then this would be equal to "geoip.rsc"
),
'read,write'
);
8 changes: 8 additions & 0 deletions examples/Util/file-get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$filename = 'backup.rsc';
file_put_contents($filename, $util->fileGetContents($filename));
8 changes: 8 additions & 0 deletions examples/Util/file-put.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$filename = 'backup.auto.rsc';
$util->filePutContents($filename, file_get_contents($filename));
9 changes: 9 additions & 0 deletions examples/Util/find-callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->changeMenu('/ip arp');
echo $util->find(function ($response) {
return preg_match('/^\d\d/', $response->getArgument('comment'));//Matches any entry who's comment starts with two digits
});
7 changes: 7 additions & 0 deletions examples/Util/find-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
echo $util->find(0, 1);//Outputs something similar to "*4de,*16a", since we targeted two entries - the one in position 0 and position 1.
7 changes: 7 additions & 0 deletions examples/Util/get-null.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/system identity');
echo $util->get(null, 'name');//echoes "MikroTik", assuming you've never altered your router's identity.
7 changes: 7 additions & 0 deletions examples/Util/get-number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
echo $util->get(0, 'address');//echoes "192.168.88.1", assuming we had the previous example executed under an empty ARP list
12 changes: 12 additions & 0 deletions examples/Util/getAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');

foreach($util->getAll() as $item) {
echo 'IP: ', $item->getProperty('address'),
' MAC: ', $item->getProperty('mac-address'),
"\n";
}
8 changes: 8 additions & 0 deletions examples/Util/move.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/queue simple');
$util->move(2, 0);//Place the queue at position 2 above the queue at position 0
$util->move($util->find(3,4), 0);//Place the queues at positions 3 and 4 above the queue at position 0 (the same one that a line ago was at position 2)
13 changes: 13 additions & 0 deletions examples/Util/parseValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$util->setMenu('/system resource');
$uptime = RouterOS\Util::parseValue($util->get(null, 'uptime'));

$now = new DateTime;

//Will output something akin to 'The router has been in operation since Sunday, 18 Aug 2013 14:03:01'
echo 'The router has been in operation since ' . $now->sub($uptime)->format(DateTime::COOKIE);
16 changes: 16 additions & 0 deletions examples/Util/set-multiple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
$util->set(
$util->find(
function ($response) {
return preg_match('/^\d\d/', $response->getArgument('comment'));//Matches any entry who's comment starts with two digits
}
),
array(
'address' => '192.168.88.103'
)
);
9 changes: 9 additions & 0 deletions examples/Util/set-single.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/ip arp');
$util->set(0, array(
'address' => '192.168.88.103'
));
2 changes: 1 addition & 1 deletion scripts/roscon.php
Original file line number Diff line number Diff line change
@@ -314,7 +314,7 @@
following command from a terminal:
```
/ip firewall filter
add place-before=[find where chain="input" && action="drop"] \
add place-before=[:pick [find where chain="input"] 0] \
chain="input" action="accept" \
dst-port=[/ip service get "api" "port"]
```
3 changes: 3 additions & 0 deletions tests/Client/Safe.php
Original file line number Diff line number Diff line change
@@ -1140,6 +1140,9 @@ public function testSendSyncWithQueryBetween()
*/
public function testResponseCollectionWordCount()
{
$this->markTestIncomplete(
'PHP reverted Countable::count($mode) support before the final 5.6'
);
$this->assertEquals(
3/*!re + .id*/+ 2/*!done*/,
count(
43 changes: 43 additions & 0 deletions tests/Util/Unsafe.php
Original file line number Diff line number Diff line change
@@ -262,6 +262,49 @@ public function testSetAndEdit()
$this->util->remove($id);
}

/**
* @depends testRemove
*
* @return void
*/
public function testComment()
{
$this->util->setMenu('/queue/simple');
$id = $this->util->add(
array(
'name' => TEST_QUEUE_NAME,
'target' => HOSTNAME_SILENT . '/32'
)
);

$printRequest = new Request(
'/queue/simple/print',
Query::where('.id', $id)
);

$responses = $this->client->sendSync($printRequest);
$this->assertSame(
HOSTNAME_SILENT . '/32',
$responses->getProperty('target')
);
$this->assertNull(
$responses->getProperty('comment')
);
$this->util->comment(TEST_QUEUE_NAME, 'test comment');

$responses = $this->client->sendSync($printRequest);
$this->assertSame(
HOSTNAME_SILENT . '/32',
$responses->getProperty('target')
);
$this->assertSame(
'test comment',
$responses->getProperty('comment')
);

$this->util->remove($id);
}

/**
* @depends testAdd
* @depends testRemove

0 comments on commit e5705b1

Please sign in to comment.