Skip to content
This repository
Browse code

Reworked Client exception codes;

Made sendAsync() detect invalid callbacks;
Minor performance tweak in isActiveRequest();
Doc fixes;
  • Loading branch information...
commit 6e6d5d3838c86428d6c801d74e76a4a6acb62b15 1 parent c9bf408
Vasil Rangelov authored February 03, 2012
23  src/PEAR2/Net/RouterOS/Client.php
@@ -106,7 +106,7 @@ public function __construct($host, $username, $password = '', $port = 8728,
106 106
             if (!static::login($this->com, $username, $password)) {
107 107
                 $this->com->close();
108 108
                 throw new DataFlowException(
109  
-                    'Invalid username or password supplied.', 100
  109
+                    'Invalid username or password supplied.', 10000
110 110
                 );
111 111
             }
112 112
         }
@@ -180,7 +180,7 @@ public static function login(Communicator $com, $username, $password = '')
180 180
             throw ($e instanceof NotSupportedException
181 181
             || $e instanceof UnexpectedValueException
182 182
             || !$com->getTransmitter()->isDataAwaiting()) ? new SocketException(
183  
-                'This is not a compatible RouterOS service', 101, $e
  183
+                'This is not a compatible RouterOS service', 10200, $e
184 184
             ) : $e;
185 185
         }
186 186
     }
@@ -255,12 +255,13 @@ public function sendAsync(Request $request, $callback = null)
255 255
         $tag = $request->getTag();
256 256
         if ('' == $tag) {
257 257
             throw new DataFlowException(
258  
-                'Asynchonous commands must have a tag.', 102
  258
+                'Asynchonous commands must have a tag.', 10500
259 259
             );
260 260
         }
261 261
         if ($this->isRequestActive($tag)) {
262 262
             throw new DataFlowException(
263  
-                'There must not be multiple active requests sharing a tag.', 103
  263
+                'There must not be multiple active requests sharing a tag.',
  264
+                10501
264 265
             );
265 266
         }
266 267
         
@@ -269,9 +270,13 @@ public function sendAsync(Request $request, $callback = null)
269 270
         if (null === $callback) {
270 271
             //Register the request at the buffer
271 272
             $this->responseBuffer[$tag] = array();
272  
-        } else {
  273
+        } elseif (is_callable($callback, true)) {
273 274
             //Prepare the callback
274 275
             $this->callbacks[$tag] = $callback;
  276
+        } else {
  277
+            throw new UnexpectedValueException(
  278
+                'Invalid callback provided.', 10502
  279
+            );
275 280
         }
276 281
         return $this;
277 282
     }
@@ -293,10 +298,10 @@ public function sendAsync(Request $request, $callback = null)
293 298
     public function isRequestActive($tag, $filter = self::FILTER_ALL)
294 299
     {
295 300
         $result = 0;
296  
-        if (self::FILTER_CALLBACK === ($filter & self::FILTER_CALLBACK)) {
  301
+        if ($filter & self::FILTER_CALLBACK) {
297 302
             $result |= (int) array_key_exists($tag, $this->callbacks);
298 303
         }
299  
-        if (self::FILTER_BUFFER === ($filter & self::FILTER_BUFFER)) {
  304
+        if ($filter & self::FILTER_BUFFER) {
300 305
             $result |= (int) array_key_exists($tag, $this->responseBuffer);
301 306
         }
302 307
         return 0 !== $result;
@@ -402,7 +407,7 @@ public function extractNewResponses($tag = null)
402 407
             return new ResponseCollection($result);
403 408
         } else {
404 409
             throw new DataFlowException(
405  
-                'No such request, or the request has already finished.', 104
  410
+                'No such request, or the request has already finished.', 10900
406 411
             );
407 412
         }
408 413
     }
@@ -471,7 +476,7 @@ public function cancelRequest($tag = null)
471 476
                 $cancelRequest->setArgument('tag', $tag);
472 477
             } else {
473 478
                 throw new DataFlowException(
474  
-                    'No such request. Canceling aborted.', 105
  479
+                    'No such request. Canceling aborted.', 11200
475 480
                 );
476 481
             }
477 482
         }
2  src/PEAR2/Net/RouterOS/InvalidArgumentException.php
@@ -21,7 +21,7 @@
21 21
 namespace PEAR2\Net\RouterOS;
22 22
 
23 23
 /**
24  
- * Exception thrown when there's something wrong with message parts.
  24
+ * Exception thrown when there's something wrong with message arguments.
25 25
  * 
26 26
  * @category Net
27 27
  * @package  PEAR2_Net_RouterOS
3  src/PEAR2/Net/RouterOS/UnexpectedValueException.php
@@ -21,8 +21,7 @@
21 21
 namespace PEAR2\Net\RouterOS;
22 22
 
23 23
 /**
24  
- * Exception thrown when encountering something not supported by RouterOS or
25  
- * this package.
  24
+ * Exception thrown when encountering an invalid value in a function argument.
26 25
  * 
27 26
  * @category Net
28 27
  * @package  PEAR2_Net_RouterOS
36  tests/ClientFeaturesTest.php
@@ -99,7 +99,9 @@ public function testSendAsyncTagRequirement()
99 99
 
100 100
             $this->fail('The call had to fail.');
101 101
         } catch (DataFlowException $e) {
102  
-            $this->assertEquals(102, $e->getCode(), 'Improper exception code.');
  102
+            $this->assertEquals(
  103
+                10500, $e->getCode(), 'Improper exception code.'
  104
+            );
103 105
         }
104 106
         try {
105 107
             $ping->setTag('');
@@ -107,7 +109,9 @@ public function testSendAsyncTagRequirement()
107 109
 
108 110
             $this->fail('The call had to fail.');
109 111
         } catch (DataFlowException $e) {
110  
-            $this->assertEquals(102, $e->getCode(), 'Improper exception code.');
  112
+            $this->assertEquals(
  113
+                10500, $e->getCode(), 'Improper exception code.'
  114
+            );
111 115
         }
112 116
     }
113 117
 
@@ -125,7 +129,9 @@ public function testSendAsyncUniqueTagRequirement()
125 129
 
126 130
             $this->fail('The call had to fail.');
127 131
         } catch (DataFlowException $e) {
128  
-            $this->assertEquals(103, $e->getCode(), 'Improper exception code.');
  132
+            $this->assertEquals(
  133
+                10501, $e->getCode(), 'Improper exception code.'
  134
+            );
129 135
         }
130 136
     }
131 137
 
@@ -178,13 +184,17 @@ public function testSendAsyncAndFullCancel()
178 184
             $this->object->extractNewResponses('ping1');
179 185
             $this->fail('The call had to fail.');
180 186
         } catch (DataFlowException $e) {
181  
-            $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
  187
+            $this->assertEquals(
  188
+                10900, $e->getCode(), 'Improper exception code.'
  189
+            );
182 190
         }
183 191
         try {
184 192
             $this->object->extractNewResponses('ping2');
185 193
             $this->fail('The call had to fail.');
186 194
         } catch (DataFlowException $e) {
187  
-            $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
  195
+            $this->assertEquals(
  196
+                10900, $e->getCode(), 'Improper exception code.'
  197
+            );
188 198
         }
189 199
     }
190 200
 
@@ -197,7 +207,9 @@ public function testInvalidCancel()
197 207
         try {
198 208
             $this->object->cancelRequest('ping1');
199 209
         } catch (DataFlowException $e) {
200  
-            $this->assertEquals(105, $e->getCode(), 'Improper exception code.');
  210
+            $this->assertEquals(
  211
+                11200, $e->getCode(), 'Improper exception code.'
  212
+            );
201 213
         }
202 214
         $this->assertEquals(
203 215
             0, $this->object->getPendingRequestsCount(),
@@ -223,7 +235,9 @@ public function testSendAsyncAndInvalidCancel()
223 235
         try {
224 236
             $this->object->cancelRequest('ping3');
225 237
         } catch (DataFlowException $e) {
226  
-            $this->assertEquals(105, $e->getCode(), 'Improper exception code.');
  238
+            $this->assertEquals(
  239
+                11200, $e->getCode(), 'Improper exception code.'
  240
+            );
227 241
         }
228 242
         $this->assertEquals(
229 243
             2, $this->object->getPendingRequestsCount(),
@@ -515,7 +529,9 @@ public function testCompleteRequestEmptyQueue()
515 529
 
516 530
             $this->fail('No exception was thrown.');
517 531
         } catch (DataFlowException $e) {
518  
-            $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
  532
+            $this->assertEquals(
  533
+                10900, $e->getCode(), 'Improper exception code.'
  534
+            );
519 535
         }
520 536
     }
521 537
 
@@ -529,7 +545,9 @@ public function testCompleteRequestInvalid()
529 545
 
530 546
             $this->fail('No exception was thrown.');
531 547
         } catch (DataFlowException $e) {
532  
-            $this->assertEquals(104, $e->getCode(), 'Improper exception code.');
  548
+            $this->assertEquals(
  549
+                10900, $e->getCode(), 'Improper exception code.'
  550
+            );
533 551
         }
534 552
     }
535 553
 
24  tests/ConnectionTest.php
@@ -140,7 +140,9 @@ public function testInvalidUsername()
140 140
                 . '" should be available.'
141 141
             );
142 142
         } catch (DataFlowException $e) {
143  
-            $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
  143
+            $this->assertEquals(
  144
+                10000, $e->getCode(), 'Improper exception code.'
  145
+            );
144 146
         }
145 147
     }
146 148
 
@@ -155,7 +157,9 @@ public function testInvalidPassword()
155 157
                 . '" should be available.'
156 158
             );
157 159
         } catch (DataFlowException $e) {
158  
-            $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
  160
+            $this->assertEquals(
  161
+                10000, $e->getCode(), 'Improper exception code.'
  162
+            );
159 163
         }
160 164
     }
161 165
 
@@ -174,7 +178,9 @@ public function testInvalidUsernameAndPassword()
174 178
                 . '" should be available.'
175 179
             );
176 180
         } catch (DataFlowException $e) {
177  
-            $this->assertEquals(100, $e->getCode(), 'Improper exception code.');
  181
+            $this->assertEquals(
  182
+                10000, $e->getCode(), 'Improper exception code.'
  183
+            );
178 184
         }
179 185
     }
180 186
 
@@ -189,7 +195,9 @@ public function testInvalidHost()
189 195
                 . '" should be available.'
190 196
             );
191 197
         } catch (SocketException $e) {
192  
-            $this->assertEquals(101, $e->getCode(), 'Improper exception code.');
  198
+            $this->assertEquals(
  199
+                10200, $e->getCode(), 'Improper exception code.'
  200
+            );
193 201
         }
194 202
     }
195 203
 
@@ -220,7 +228,9 @@ public function testInvalidPort()
220 228
                 . '" should be available.'
221 229
             );
222 230
         } catch (SocketException $e) {
223  
-            $this->assertEquals(101, $e->getCode(), 'Improper exception code.');
  231
+            $this->assertEquals(
  232
+                10200, $e->getCode(), 'Improper exception code.'
  233
+            );
224 234
         }
225 235
     }
226 236
 
@@ -291,7 +301,9 @@ public function testInvalidSocketOnClose()
291 301
             new Response($com);
292 302
             $this->fail('Receiving had to fail.');
293 303
         } catch (SocketException $e) {
294  
-            $this->assertEquals(206, $e->getCode(), 'Improper exception code.');
  304
+            $this->assertEquals(
  305
+                206, $e->getCode(), 'Improper exception code.'
  306
+            );
295 307
         }
296 308
     }
297 309
 

0 notes on commit 6e6d5d3

Please sign in to comment.
Something went wrong with that request. Please try again.