Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reworked Client exception codes;

Made sendAsync() detect invalid callbacks;
Minor performance tweak in isActiveRequest();
Doc fixes;
  • Loading branch information...
commit 6e6d5d3838c86428d6c801d74e76a4a6acb62b15 1 parent c9bf408
@boenrobot boenrobot authored
View
23 src/PEAR2/Net/RouterOS/Client.php
@@ -106,7 +106,7 @@ public function __construct($host, $username, $password = '', $port = 8728,
if (!static::login($this->com, $username, $password)) {
$this->com->close();
throw new DataFlowException(
- 'Invalid username or password supplied.', 100
+ 'Invalid username or password supplied.', 10000
);
}
}
@@ -180,7 +180,7 @@ public static function login(Communicator $com, $username, $password = '')
throw ($e instanceof NotSupportedException
|| $e instanceof UnexpectedValueException
|| !$com->getTransmitter()->isDataAwaiting()) ? new SocketException(
- 'This is not a compatible RouterOS service', 101, $e
+ 'This is not a compatible RouterOS service', 10200, $e
) : $e;
}
}
@@ -255,12 +255,13 @@ public function sendAsync(Request $request, $callback = null)
$tag = $request->getTag();
if ('' == $tag) {
throw new DataFlowException(
- 'Asynchonous commands must have a tag.', 102
+ 'Asynchonous commands must have a tag.', 10500
);
}
if ($this->isRequestActive($tag)) {
throw new DataFlowException(
- 'There must not be multiple active requests sharing a tag.', 103
+ 'There must not be multiple active requests sharing a tag.',
+ 10501
);
}
@@ -269,9 +270,13 @@ public function sendAsync(Request $request, $callback = null)
if (null === $callback) {
//Register the request at the buffer
$this->responseBuffer[$tag] = array();
- } else {
+ } elseif (is_callable($callback, true)) {
//Prepare the callback
$this->callbacks[$tag] = $callback;
+ } else {
+ throw new UnexpectedValueException(
+ 'Invalid callback provided.', 10502
+ );
}
return $this;
}
@@ -293,10 +298,10 @@ public function sendAsync(Request $request, $callback = null)
public function isRequestActive($tag, $filter = self::FILTER_ALL)
{
$result = 0;
- if (self::FILTER_CALLBACK === ($filter & self::FILTER_CALLBACK)) {
+ if ($filter & self::FILTER_CALLBACK) {
$result |= (int) array_key_exists($tag, $this->callbacks);
}
- if (self::FILTER_BUFFER === ($filter & self::FILTER_BUFFER)) {
+ if ($filter & self::FILTER_BUFFER) {
$result |= (int) array_key_exists($tag, $this->responseBuffer);
}
return 0 !== $result;
@@ -402,7 +407,7 @@ public function extractNewResponses($tag = null)
return new ResponseCollection($result);
} else {
throw new DataFlowException(
- 'No such request, or the request has already finished.', 104
+ 'No such request, or the request has already finished.', 10900
);
}
}
@@ -471,7 +476,7 @@ public function cancelRequest($tag = null)
$cancelRequest->setArgument('tag', $tag);
} else {
throw new DataFlowException(
- 'No such request. Canceling aborted.', 105
+ 'No such request. Canceling aborted.', 11200
);
}
}
View
2  src/PEAR2/Net/RouterOS/InvalidArgumentException.php
@@ -21,7 +21,7 @@
namespace PEAR2\Net\RouterOS;
/**
- * Exception thrown when there's something wrong with message parts.
+ * Exception thrown when there's something wrong with message arguments.
*
* @category Net
* @package PEAR2_Net_RouterOS
View
3  src/PEAR2/Net/RouterOS/UnexpectedValueException.php
@@ -21,8 +21,7 @@
namespace PEAR2\Net\RouterOS;
/**
- * Exception thrown when encountering something not supported by RouterOS or
- * this package.
+ * Exception thrown when encountering an invalid value in a function argument.
*
* @category Net
* @package PEAR2_Net_RouterOS
View
36 tests/ClientFeaturesTest.php
@@ -99,7 +99,9 @@ public function testSendAsyncTagRequirement()
$this->fail('The call had to fail.');
} catch (DataFlowException $e) {
- $this->assertEquals(102, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10500, $e->getCode(), 'Improper exception code.'
+ );
}
try {
$ping->setTag('');
@@ -107,7 +109,9 @@ public function testSendAsyncTagRequirement()
$this->fail('The call had to fail.');
} catch (DataFlowException $e) {
- $this->assertEquals(102, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10500, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -125,7 +129,9 @@ public function testSendAsyncUniqueTagRequirement()
$this->fail('The call had to fail.');
} catch (DataFlowException $e) {
- $this->assertEquals(103, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10501, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -178,13 +184,17 @@ public function testSendAsyncAndFullCancel()
$this->object->extractNewResponses('ping1');
$this->fail('The call had to fail.');
} catch (DataFlowException $e) {
- $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10900, $e->getCode(), 'Improper exception code.'
+ );
}
try {
$this->object->extractNewResponses('ping2');
$this->fail('The call had to fail.');
} catch (DataFlowException $e) {
- $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10900, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -197,7 +207,9 @@ public function testInvalidCancel()
try {
$this->object->cancelRequest('ping1');
} catch (DataFlowException $e) {
- $this->assertEquals(105, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 11200, $e->getCode(), 'Improper exception code.'
+ );
}
$this->assertEquals(
0, $this->object->getPendingRequestsCount(),
@@ -223,7 +235,9 @@ public function testSendAsyncAndInvalidCancel()
try {
$this->object->cancelRequest('ping3');
} catch (DataFlowException $e) {
- $this->assertEquals(105, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 11200, $e->getCode(), 'Improper exception code.'
+ );
}
$this->assertEquals(
2, $this->object->getPendingRequestsCount(),
@@ -515,7 +529,9 @@ public function testCompleteRequestEmptyQueue()
$this->fail('No exception was thrown.');
} catch (DataFlowException $e) {
- $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10900, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -529,7 +545,9 @@ public function testCompleteRequestInvalid()
$this->fail('No exception was thrown.');
} catch (DataFlowException $e) {
- $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10900, $e->getCode(), 'Improper exception code.'
+ );
}
}
View
24 tests/ConnectionTest.php
@@ -140,7 +140,9 @@ public function testInvalidUsername()
. '" should be available.'
);
} catch (DataFlowException $e) {
- $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -155,7 +157,9 @@ public function testInvalidPassword()
. '" should be available.'
);
} catch (DataFlowException $e) {
- $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -174,7 +178,9 @@ public function testInvalidUsernameAndPassword()
. '" should be available.'
);
} catch (DataFlowException $e) {
- $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10000, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -189,7 +195,9 @@ public function testInvalidHost()
. '" should be available.'
);
} catch (SocketException $e) {
- $this->assertEquals(101, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10200, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -220,7 +228,9 @@ public function testInvalidPort()
. '" should be available.'
);
} catch (SocketException $e) {
- $this->assertEquals(101, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 10200, $e->getCode(), 'Improper exception code.'
+ );
}
}
@@ -291,7 +301,9 @@ public function testInvalidSocketOnClose()
new Response($com);
$this->fail('Receiving had to fail.');
} catch (SocketException $e) {
- $this->assertEquals(206, $e->getCode(), 'Improper exception code.');
+ $this->assertEquals(
+ 206, $e->getCode(), 'Improper exception code.'
+ );
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.