Skip to content

Commit b1eecfb

Browse files
committed
Fixed Util::getCurrentTime() and Util::find() to work even when the underlying Client is streaming responses, and added tests for that;
Switched the date parsing in Util::parseValue() to use "new DateTime" instead of "DateTime::createFromFormat()" for the purpose of easier validation checking (an exception being thrown...).
1 parent ec48174 commit b1eecfb

File tree

6 files changed

+158
-22
lines changed

6 files changed

+158
-22
lines changed

RELEASE-1.0.0b6

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ Util stuff, mostly.
1010
* Util::parseValue() now recognizes dates in the "M/j/Y H:i:s" format (used across RouterOS), and turns that into a DateTime object for that time (or midnight if the time part is omitted) in UTC.
1111
* Util::getAll() now throws a NotSupportedException if the arguments "follow", "follow-only" or "count-only" are used. The first two, because PHP would hang (since Client::sendSync() is used under the hood), and the last one because it's unredable in the returned output (use Util::count() instead).
1212
* Util::setMenu() can now go back to the root menu.
13+
* Util::find() now works even when the underlying Client streams responses.
1314
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.
1415
* The console now checks whether PEAR2_CommandLine is installed, ensuring better error messages when this package is installed without its optional dependencies.

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ public static function parseValue($value)
208208
$value,
209209
$date
210210
)) {
211+
if (!isset($date['time'])) {
212+
$date['time'] = '00:00:00';
213+
}
211214
try {
212-
if (!isset($date['time'])) {
213-
$date['time'] = '00:00:00';
214-
}
215-
return DateTime::createFromFormat(
216-
'M/j/Y H:i:s',
217-
ucfirst($date['mon']) . '/' . (int)$date['day'] .
218-
"/{$date['year']} {$date['time']}",
215+
return new DateTime(
216+
$date['year'] .
217+
'-' . ucfirst($date['mon']) .
218+
"-{$date['day']} {$date['time']}",
219219
new DateTimeZone('UTC')
220220
);
221221
} catch (E $e) {
@@ -693,20 +693,37 @@ public function clearIdCache()
693693
public function getCurrentTime()
694694
{
695695
$clock = $this->client->sendSync(
696-
new Request('/system/clock/print')
696+
new Request(
697+
'/system/clock/print
698+
.proplist=date,time,time-zone-name,gmt-offset'
699+
)
697700
)->current();
698-
$datetime = ucfirst(strtolower($clock->getProperty('date'))) . ' ' .
699-
$clock->getProperty('time');
701+
$clockParts = array();
702+
foreach (array(
703+
'date',
704+
'time',
705+
'time-zone-name',
706+
'gmt-offset'
707+
) as $clockPart) {
708+
$clockParts[$clockPart] = $clock->getProperty($clockPart);
709+
if (is_resource($clockParts[$clockPart])) {
710+
$clockParts[$clockPart] = stream_get_contents(
711+
$clockParts[$clockPart]
712+
);
713+
}
714+
}
715+
$datetime = ucfirst(strtolower($clockParts['date'])) . ' ' .
716+
$clockParts['time'];
700717
try {
701718
$result = DateTime::createFromFormat(
702719
'M/j/Y H:i:s',
703720
$datetime,
704-
new DateTimeZone($clock->getProperty('time-zone-name'))
721+
new DateTimeZone($clockParts['time-zone-name'])
705722
);
706723
} catch (E $e) {
707724
$result = DateTime::createFromFormat(
708725
'M/j/Y H:i:s P',
709-
$datetime . ' ' . $clock->getProperty('gmt-offset'),
726+
$datetime . ' ' . $clockParts['gmt-offset'],
710727
new DateTimeZone('UTC')
711728
);
712729
}
@@ -762,18 +779,25 @@ public function find()
762779
if ($criteria instanceof Query) {
763780
foreach ($this->client->sendSync(
764781
new Request($this->menu . '/print .proplist=.id', $criteria)
765-
) as $response) {
766-
$idList .= $response->getProperty('.id') . ',';
782+
)->getAllOfType(Response::TYPE_DATA) as $response) {
783+
$newId = $response->getProperty('.id');
784+
$idList .= is_string($newId)
785+
? $newId . ','
786+
: stream_get_contents($newId) . ',';
767787
}
768788
} elseif (is_callable($criteria)) {
769789
$idCache = array();
770790
foreach ($this->client->sendSync(
771791
new Request($this->menu . '/print')
772-
) as $response) {
792+
)->getAllOfType(Response::TYPE_DATA) as $response) {
793+
$newId = $response->getProperty('.id');
794+
$newId = is_string($newId)
795+
? $newId
796+
: stream_get_contents($newId);
773797
if ($criteria($response)) {
774-
$idList .= $response->getProperty('.id') . ',';
798+
$idList .= $newId . ',';
775799
}
776-
$idCache[] = $response->getProperty('.id');
800+
$idCache[] = $newId;
777801
}
778802
$this->idCache = $idCache;
779803
} else {

tests/Extra/isHostnameInvalid.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
*/
3131
function isHostnameInvalid(Response $item)
3232
{
33-
return $item->getProperty(
34-
'target'
35-
) === Test\HOSTNAME_INVALID . '/32';
33+
$target = $item->getProperty('target');
34+
if (!is_string($target)) {
35+
$target = stream_get_contents($target);
36+
}
37+
return $target === Test\HOSTNAME_INVALID . '/32';
3638
}

tests/Misc/ConnectionlessTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace PEAR2\Net\RouterOS\Test\Misc;
44

55
use DateInterval;
6-
use PEAR2\Net\RouterOS\Client;
6+
use DateTime;
7+
use DateTimeZone;
78
use PEAR2\Net\RouterOS\Communicator;
89
use PEAR2\Net\RouterOS\InvalidArgumentException;
910
use PEAR2\Net\RouterOS\LengthException;
@@ -1035,6 +1036,9 @@ public function providerUtilParseValue()
10351036
'1w2m3s' => array('1w2m3s', new DateInterval('P7DT2M3S')),
10361037
'1w2h3m4s' => array('1w2h3m4s', new DateInterval('P7DT2H3M4S')),
10371038
'1w2d3h4m5s' => array('1w2d3h4m5s', new DateInterval('P9DT3H4M5S')),
1039+
'Dec/21/2012' => array('Dec/21/2012', new DateTime('2012-12-21 00:00:00', new DateTimeZone('UTC'))),
1040+
'Dec/21/2012 12:34:56' => array('Dec/21/2012 12:34:56', new DateTime('2012-12-21 12:34:56', new DateTimeZone('UTC'))),
1041+
'Dec/99/9999 99:99:99' => array('Dec/99/9999 99:99:99', 'Dec/99/9999 99:99:99'),
10381042
'{}' => array('{}', array()),
10391043
'{a}' => array('{a}', array('a')),
10401044
'{1;2}' => array('{1;2}', array(1, 2)),

tests/Util/Safe.php

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ public function testFindByQuery()
135135
Query::where('target', HOSTNAME_INVALID . '/32')
136136
)
137137
);
138+
139+
$this->client->setStreamingResponses(true);
140+
$this->util->setMenu('/queue/simple');
141+
$this->assertRegExp(
142+
'/^' . self::REGEX_ID . '$/',
143+
$this->util->find(
144+
Query::where('target', HOSTNAME_INVALID . '/32')
145+
)
146+
);
138147
}
139148

140149
public function testFindNoCriteria()
@@ -153,6 +162,22 @@ public function testFindNoCriteria()
153162
)->getAllOfType(Response::TYPE_DATA)
154163
)
155164
);
165+
166+
$this->client->setStreamingResponses(true);
167+
$this->util->setMenu('/queue/simple');
168+
$findResults = $this->util->find();
169+
$this->assertRegExp(
170+
self::REGEX_IDLIST,
171+
$findResults
172+
);
173+
$this->assertSame(
174+
count(explode(',', $findResults)),
175+
count(
176+
$this->client->sendSync(
177+
new Request('/queue/simple/print')
178+
)->getAllOfType(Response::TYPE_DATA)
179+
)
180+
);
156181
}
157182

158183
public function testFindCallback()
@@ -178,9 +203,34 @@ function ($entry) {
178203
)
179204
)->getProperty('.id')
180205
);
206+
207+
$this->client->setStreamingResponses(true);
208+
$this->util->setMenu('/queue/simple');
209+
$findResults = $this->util->find(
210+
function ($entry) {
211+
return stream_get_contents(
212+
$entry->getProperty('target')
213+
) === HOSTNAME_INVALID . '/32';
214+
}
215+
);
216+
$this->assertRegExp(
217+
'/^' . self::REGEX_ID . '$/',
218+
$findResults
219+
);
220+
$this->assertSame(
221+
$findResults,
222+
stream_get_contents(
223+
$this->client->sendSync(
224+
new Request(
225+
'/queue/simple/print',
226+
Query::where('target', HOSTNAME_INVALID . '/32')
227+
)
228+
)->getProperty('.id')
229+
)
230+
);
181231
}
182232

183-
public function testByCallbackName()
233+
public function testFindByCallbackName()
184234
{
185235
include_once __DIR__ . '/../Extra/isHostnameInvalid.php';
186236

@@ -199,6 +249,25 @@ public function testByCallbackName()
199249
)
200250
)->getProperty('.id')
201251
);
252+
253+
$this->client->setStreamingResponses(true);
254+
$this->util->setMenu('/queue/simple');
255+
$findResults = $this->util->find('isHostnameInvalid');
256+
$this->assertRegExp(
257+
'/^' . self::REGEX_ID . '$/',
258+
$findResults
259+
);
260+
$this->assertSame(
261+
$findResults,
262+
stream_get_contents(
263+
$this->client->sendSync(
264+
new Request(
265+
'/queue/simple/print',
266+
Query::where('target', HOSTNAME_INVALID . '/32')
267+
)
268+
)->getProperty('.id')
269+
)
270+
);
202271
}
203272

204273
public function testFindById()
@@ -209,6 +278,13 @@ public function testFindById()
209278
Query::where('target', HOSTNAME_INVALID . '/32')
210279
)
211280
);
281+
282+
$this->assertSame(
283+
$originalResult->getProperty('.id'),
284+
$this->util->find($originalResult->getProperty('.id'))
285+
);
286+
287+
$this->client->setStreamingResponses(true);
212288
$this->assertSame(
213289
$originalResult->getProperty('.id'),
214290
$this->util->find($originalResult->getProperty('.id'))
@@ -231,6 +307,23 @@ public function testFindByCommaSeparatedValue()
231307
$findResults
232308
);
233309
$this->assertCount(2, explode(',', $findResults));
310+
311+
312+
$this->client->setStreamingResponses(true);
313+
$this->util->setMenu('/queue/simple');
314+
$findResults = $this->util->find('0,1');
315+
$this->assertRegExp(
316+
self::REGEX_IDLIST,
317+
$findResults
318+
);
319+
$this->assertCount(2, explode(',', $findResults));
320+
321+
$findResults = $this->util->find('0,,1');
322+
$this->assertRegExp(
323+
self::REGEX_IDLIST,
324+
$findResults
325+
);
326+
$this->assertCount(2, explode(',', $findResults));
234327
}
235328

236329
public function testGetallAndCount()
@@ -239,6 +332,12 @@ public function testGetallAndCount()
239332
$queues = $this->util->getAll();
240333
$this->assertInstanceOf(ROS_NAMESPACE . '\ResponseCollection', $queues);
241334
$this->assertSameSize($queues, $this->util);
335+
336+
$this->client->setStreamingResponses(true);
337+
$this->util->setMenu('/queue/simple');
338+
$queues = $this->util->getAll();
339+
$this->assertInstanceOf(ROS_NAMESPACE . '\ResponseCollection', $queues);
340+
$this->assertSameSize($queues, $this->util);
242341
}
243342

244343
public function testInvalidGetallAndCount()

tests/Util/Unsafe.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ public function testGetCurrentTime()
436436
)
437437
);
438438
}
439+
440+
public function testGetCurrentTimeFromStreamClient()
441+
{
442+
$this->client->setStreamingResponses(true);
443+
$this->testGetCurrentTime();
444+
}
439445

440446
/**
441447
* @depends testRemove

0 commit comments

Comments
 (0)