Skip to content

Commit f573423

Browse files
committed
Added Util::newRequest(), with a test for it;
CS fix in Response.
1 parent 6352a83 commit f573423

File tree

5 files changed

+117
-3
lines changed

5 files changed

+117
-3
lines changed

docs/wiki

Submodule wiki updated from 6c3a27a to 282d4f9

src/PEAR2/Net/RouterOS/NotSupportedException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class NotSupportedException extends E implements Exception
3939
{
4040

4141
const CODE_CONTROL_BYTE = 1601;
42+
43+
const CODE_MENU_MISMATCH = 60000;
4244

4345
/**
4446
* @var mixed The unsupported value.

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function getType()
276276
* @return string|resource|null The value of the specified argument.
277277
* Returns NULL if such an argument is not set.
278278
*
279-
* @deprecated 1.0.0b5 Use {@link static::getProperty()} instead.
279+
* @deprecated 1.0.0b5 Use {@link static::getProperty()} instead.
280280
* This method will be removed upon final release, and is currently
281281
* left standing merely because it can't be easily search&replaced in
282282
* existing code, due to the fact the name "getArgument()" is shared

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public function __construct(Client $client)
417417
/**
418418
* Gets the current menu.
419419
*
420-
* @return string The current menu.
420+
* @return string The absolute path to current menu, using API syntax.
421421
*/
422422
public function getMenu()
423423
{
@@ -457,6 +457,50 @@ public function setMenu($newMenu)
457457
return $this;
458458
}
459459

460+
/**
461+
* Creates a Request object.
462+
*
463+
* Creates a {@link Request} object, with a command that's relative to the
464+
* current menu. The request can then be sent using {@link Client}.
465+
*
466+
* @param string $command The command of the request, not including
467+
* the menu. The request will have that command at the current menu.
468+
* @param array $args Arguments of the request.
469+
* Follows the same rules as with {@link Util::getAll()}.
470+
* @param Query|null $query The {@link Query} of the request.
471+
* @param string|null $tag The tag of the request.
472+
*
473+
* @return Request The {@link Request} object.
474+
*
475+
* @throws NotSupportedException On an attempt to call a command in a
476+
* different menu.
477+
*/
478+
public function newRequest(
479+
$command,
480+
array $args = array(),
481+
Query $query = null,
482+
$tag = null
483+
) {
484+
if (false !== strpos($command, '/')) {
485+
throw new NotSupportedException(
486+
'Command tried to go to a different menu',
487+
NotSupportedException::CODE_MENU_MISMATCH,
488+
null,
489+
$command
490+
);
491+
}
492+
$request = new Request('/menu', $query, $tag);
493+
$request->setCommand("{$this->menu}/{$command}");
494+
foreach ($args as $name => $value) {
495+
if (is_int($name)) {
496+
$request->setArgument($value);
497+
} else {
498+
$request->setArgument($name, $value);
499+
}
500+
}
501+
return $request;
502+
}
503+
460504
/**
461505
* Executes a RouterOS script.
462506
*

tests/Util/Safe.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PEAR2\Net\RouterOS\Test\Util;
44

55
use PEAR2\Net\RouterOS\Client;
6+
use PEAR2\Net\RouterOS\NotSupportedException;
7+
use PEAR2\Net\RouterOS\InvalidArgumentException;
68
use PEAR2\Net\RouterOS\Query;
79
use PEAR2\Net\RouterOS\Request;
810
use PEAR2\Net\RouterOS\Response;
@@ -57,6 +59,72 @@ public function testSetGetMenu()
5759
);
5860
}
5961

62+
public function testNewRequest()
63+
{
64+
//Simple request
65+
$request = $this->util->setMenu('/queue simple')->newRequest('print');
66+
$this->assertInstanceOf(ROS_NAMESPACE . '\Request', $request);
67+
$this->assertSame('/queue/simple/print', $request->getCommand());
68+
$this->assertEmpty($request->getIterator()->getArrayCopy());
69+
$this->assertNull($request->getQuery());
70+
$this->assertNull($request->getTag());
71+
72+
//Complex request
73+
$request = $this->util->setMenu('/queue simple')->newRequest(
74+
'print',
75+
array('detail', 'from'=> TEST_QUEUE_NAME),
76+
Query::where('target', HOSTNAME_INVALID . '/32'),
77+
'test'
78+
);
79+
$this->assertInstanceOf(ROS_NAMESPACE . '\Request', $request);
80+
$this->assertSame('/queue/simple/print', $request->getCommand());
81+
$this->assertSame(
82+
array(
83+
'detail' => '',
84+
'from'=> TEST_QUEUE_NAME
85+
),
86+
$request->getIterator()->getArrayCopy()
87+
);
88+
$this->assertInstanceOf(ROS_NAMESPACE . '\Query', $request->getQuery());
89+
$this->assertSame('test', $request->getTag());
90+
91+
//Failed request (API syntax)
92+
try {
93+
$request = $this->util->setMenu('/queue simple')->newRequest(
94+
'../tree/print',
95+
array('detail'),
96+
Query::where('target', HOSTNAME_INVALID . '/32'),
97+
'test'
98+
);
99+
$this->fail('Exception for invalid request not thrown');
100+
} catch (NotSupportedException $e) {
101+
$this->assertSame(
102+
NotSupportedException::CODE_MENU_MISMATCH,
103+
$e->getCode()
104+
);
105+
$this->assertSame(
106+
'../tree/print',
107+
$e->getValue()
108+
);
109+
}
110+
111+
//Failed request (CLI syntax)
112+
try {
113+
$request = $this->util->setMenu('/queue simple')->newRequest(
114+
'.. tree print',
115+
array('detail'),
116+
Query::where('target', HOSTNAME_INVALID . '/32'),
117+
'test'
118+
);
119+
$this->fail('Exception for invalid request not thrown');
120+
} catch (InvalidArgumentException $e) {
121+
$this->assertSame(
122+
InvalidArgumentException::CODE_CMD_INVALID,
123+
$e->getCode()
124+
);
125+
}
126+
}
127+
60128
public function testFindByQuery()
61129
{
62130
$this->util->setMenu('/queue/simple');

0 commit comments

Comments
 (0)