Skip to content

Commit 7dd1aaa

Browse files
committed
Changed Util::edit() to be its own method for modifying a single property;
Added "count-only" as a prohibited argument at Util::getAll(), and made the thrown NotSupportedException have the prohibited value in it.
1 parent c80f632 commit 7dd1aaa

File tree

4 files changed

+132
-128
lines changed

4 files changed

+132
-128
lines changed

RELEASE-1.0.0b6

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
Bug fixes, mostly.
1+
Util stuff, mostly.
22

3+
* __BREAKING CHANGES:__
4+
- Util::edit() is no longer an alias of Util::set(). It's now its own method that can modify or unset a single specified property.
35
* New Util methods:
46
- comment()
57
- getCurrentTime()
68
- newRequest()
79
* Util::parseValue() now supports letter notation for time (1h2m3s), not just double colon notation (01:02:03), modeled after RouterOS. Related to that is also that leading zeroes, and zero minutes and seconds are now optional (e.g. "1:" is a valid way of saying 1 hour). Sub-second information is rounded up to the nearest second on current PHP versions (future versions are expected to support sub-second information in DateInterval by allowing seconds to be a double; The code currently attempts to give DateInterval a double, falling back to rounding to a second).
10+
* Util::parseValue() now recognizes dates in the "M/j/Y" format (used across RouterOS), and translates them to a DateTime object that is midnight UTC time at the specified date.
11+
* 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).
12+
* Util::setMenu() can now go back to the root menu.
813
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.
9-
* Util::setMenu() can now go back to the root menu.
14+
* 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: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -985,20 +985,21 @@ public function set($numbers, array $newValues)
985985
/**
986986
* Alias of {@link static::set()}
987987
*
988-
* @param mixed $numbers Items
989-
* to be modified.
988+
* @param mixed $numbers Items to be modified.
990989
* Can be any criteria accepted by {@link static::find()} or NULL
991990
* in case the menu is one without items (e.g. "/system identity").
992-
* @param array<string,string|resource>|array<int,string> $newValues An
993-
* array with the names of each property to set as an array key, and the
994-
* new value as an array value.
991+
* @param string $valueName Name of property to be modified.
992+
* @param string|resource|null $newValue The new value to set.
993+
* If set to NULL, the property is unset.
995994
*
996995
* @return ResponseCollection Returns the response collection, allowing you
997996
* to inspect errors, if any.
998997
*/
999-
public function edit($numbers, array $newValues)
998+
public function edit($numbers, $valueName, $newValue)
1000999
{
1001-
return $this->set($numbers, $newValues);
1000+
return null === $newValue
1001+
? $this->unsetValue($numbers, $valueName)
1002+
: $this->set($numbers, array($valueName => $newValue));
10021003
}
10031004

10041005
/**
@@ -1169,13 +1170,16 @@ public function getAll(array $args = array(), Query $query = null)
11691170
$printRequest->setArgument($name, $value);
11701171
}
11711172
}
1172-
if ($printRequest->getArgument('follow') !== null
1173-
|| $printRequest->getArgument('follow-only') !== null
1174-
) {
1175-
throw new NotSupportedException(
1176-
'The "follow" and "follow-only" arguments are prohibited',
1177-
NotSupportedException::CODE_ARG_PROHIBITED
1178-
);
1173+
1174+
foreach (array('follow', 'follow-only', 'count-only') as $arg) {
1175+
if ($printRequest->getArgument($arg) !== null) {
1176+
throw new NotSupportedException(
1177+
"The argument '{$arg}' was specified, but is prohibited",
1178+
NotSupportedException::CODE_ARG_PROHIBITED,
1179+
null,
1180+
$arg
1181+
);
1182+
}
11791183
}
11801184
$responses = $this->client->sendSync($printRequest);
11811185

tests/Util/Safe.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ public function testInvalidGetallAndCount()
251251
public function providerProhibitedArgs()
252252
{
253253
return array(
254-
0 => array(array('follow')),
255-
1 => array(array('follow-only'))
254+
'follow' => array(array('follow'), 0),
255+
'follow-only' => array(array('follow-only'), 0),
256+
'count-only' => array(array('count-only'), 0)
256257
);
257258
}
258259

@@ -263,13 +264,14 @@ public function providerProhibitedArgs()
263264
*
264265
* @dataProvider providerProhibitedArgs
265266
*/
266-
public function testGetallArgExceptions(array $args)
267+
public function testGetallArgExceptions(array $args, $argKey)
267268
{
268269
$this->util->setMenu('/queue simple');
269270
try {
270271
$this->util->getAll($args);
271272
$this->fail('Supplying these arguments should result in an exception');
272273
} catch (NotSupportedException $e) {
274+
$this->assertSame($args[$argKey], $e->getValue());
273275
$this->assertSame(
274276
NotSupportedException::CODE_ARG_PROHIBITED,
275277
$e->getCode()

tests/Util/Unsafe.php

Lines changed: 102 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -194,115 +194,6 @@ public function testAddMultiple()
194194
$this->assertSame($beforeCount, $postCount);
195195
}
196196

197-
/**
198-
* @depends testRemove
199-
*
200-
* @return void
201-
*/
202-
public function testSetAndEdit()
203-
{
204-
$this->util->setMenu('/queue/simple');
205-
$id = $this->util->add(
206-
array(
207-
'name' => TEST_QUEUE_NAME,
208-
'target' => HOSTNAME_SILENT . '/32'
209-
)
210-
);
211-
212-
$printRequest = new Request(
213-
'/queue/simple/print',
214-
Query::where('.id', $id)
215-
);
216-
217-
$responses = $this->client->sendSync($printRequest);
218-
$this->assertSame(
219-
HOSTNAME_SILENT . '/32',
220-
$responses->getProperty('target')
221-
);
222-
$this->assertNotSame(
223-
'true',
224-
$responses->getProperty('disabled')
225-
);
226-
227-
$this->util->set(
228-
$id,
229-
array(
230-
'target' => HOSTNAME_INVALID . '/32',
231-
'disabled'
232-
)
233-
);
234-
235-
$responses = $this->client->sendSync($printRequest);
236-
$this->assertSame(
237-
HOSTNAME_INVALID . '/32',
238-
$responses->getProperty('target')
239-
);
240-
$this->assertSame(
241-
'true',
242-
$responses->getProperty('disabled')
243-
);
244-
245-
$this->util->edit(
246-
$id,
247-
array(
248-
'target' => HOSTNAME_SILENT . '/32'
249-
)
250-
);
251-
252-
$responses = $this->client->sendSync($printRequest);
253-
$this->assertSame(
254-
HOSTNAME_SILENT . '/32',
255-
$responses->getProperty('target')
256-
);
257-
$this->assertSame(
258-
'true',
259-
$responses->getProperty('disabled')
260-
);
261-
262-
$this->util->remove($id);
263-
}
264-
265-
/**
266-
* @depends testSetAndEdit
267-
*/
268-
public function testGetCurrentTime()
269-
{
270-
$originalTimezone = $this->util->setMenu('/system clock')
271-
->get(null, 'time-zone-name');
272-
$originalTimezoneAutodetect = $this->util
273-
->get(null, 'time-zone-autodetect');
274-
275-
$this->util->set(
276-
null,
277-
array(
278-
'time-zone-autodetect' => 'false',
279-
'time-zone-name' => TEST_TIMEZONE
280-
)
281-
);
282-
$curTime = $this->util->setMenu('/')->getCurrentTime();
283-
$this->assertInstanceOf(
284-
'DateTime',
285-
$curTime
286-
);
287-
$this->assertSame(TEST_TIMEZONE, $curTime->getTimezone()->getName());
288-
289-
$this->util->setMenu('/system clock')
290-
->set(null, array('time-zone-name' => 'manual'));
291-
$curTimeInManual = $this->util->setMenu('/')->getCurrentTime();
292-
$this->assertNotSame(
293-
TEST_TIMEZONE,
294-
$curTimeInManual->getTimezone()->getName()
295-
);
296-
297-
$this->util->setMenu('/system clock')->set(
298-
null,
299-
array(
300-
'time-zone-name' => $originalTimezone,
301-
'time-zone-autodetect' => $originalTimezoneAutodetect
302-
)
303-
);
304-
}
305-
306197
/**
307198
* @depends testRemove
308199
*
@@ -444,6 +335,108 @@ public function testUnsetValue()
444335
$this->assertNull($targetAfter);
445336
}
446337

338+
/**
339+
* @depends testRemove
340+
* @depends testUnsetValue
341+
*
342+
* @return void
343+
*/
344+
public function testSetAndEdit()
345+
{
346+
$this->util->setMenu('/queue/simple');
347+
$id = $this->util->add(
348+
array(
349+
'name' => TEST_QUEUE_NAME,
350+
'target' => HOSTNAME_SILENT . '/32'
351+
)
352+
);
353+
354+
$printRequest = new Request(
355+
'/queue/simple/print',
356+
Query::where('.id', $id)
357+
);
358+
359+
$responses = $this->client->sendSync($printRequest);
360+
$this->assertNotSame('true', $responses->getProperty('disabled'));
361+
$this->assertSame(
362+
HOSTNAME_SILENT . '/32',
363+
$responses->getProperty('target')
364+
);
365+
366+
$this->util->set(
367+
$id,
368+
array(
369+
'target' => HOSTNAME_INVALID . '/32',
370+
'disabled'
371+
)
372+
);
373+
374+
$responses = $this->client->sendSync($printRequest);
375+
$this->assertSame('true', $responses->getProperty('disabled'));
376+
$this->assertSame(
377+
HOSTNAME_INVALID . '/32',
378+
$responses->getProperty('target')
379+
);
380+
381+
$this->util->edit($id, 'target', HOSTNAME_SILENT . '/32');
382+
383+
$responses = $this->client->sendSync($printRequest);
384+
$this->assertSame('true', $responses->getProperty('disabled'));
385+
$this->assertSame(
386+
HOSTNAME_SILENT . '/32',
387+
$responses->getProperty('target')
388+
);
389+
390+
$this->util->edit($id, 'target', null);
391+
392+
$responses = $this->client->sendSync($printRequest);
393+
$this->assertSame('true', $responses->getProperty('disabled'));
394+
$this->assertSame(null, $responses->getProperty('target'));
395+
396+
$this->util->remove($id);
397+
}
398+
399+
/**
400+
* @depends testSetAndEdit
401+
*/
402+
public function testGetCurrentTime()
403+
{
404+
$originalTimezone = $this->util->setMenu('/system clock')
405+
->get(null, 'time-zone-name');
406+
$originalTimezoneAutodetect = $this->util
407+
->get(null, 'time-zone-autodetect');
408+
409+
$this->util->set(
410+
null,
411+
array(
412+
'time-zone-autodetect' => 'false',
413+
'time-zone-name' => TEST_TIMEZONE
414+
)
415+
);
416+
$curTime = $this->util->setMenu('/')->getCurrentTime();
417+
$this->assertInstanceOf(
418+
'DateTime',
419+
$curTime
420+
);
421+
$this->assertSame(TEST_TIMEZONE, $curTime->getTimezone()->getName());
422+
423+
$this->util->setMenu('/system clock')
424+
->set(null, array('time-zone-name' => 'manual'));
425+
$curTimeInManual = $this->util->setMenu('/')->getCurrentTime();
426+
$this->assertNotSame(
427+
TEST_TIMEZONE,
428+
$curTimeInManual->getTimezone()->getName()
429+
);
430+
431+
$this->util->setMenu('/system clock')->set(
432+
null,
433+
array(
434+
'time-zone-name' => $originalTimezone,
435+
'time-zone-autodetect' => $originalTimezoneAutodetect
436+
)
437+
);
438+
}
439+
447440
/**
448441
* @depends testRemove
449442
*

0 commit comments

Comments
 (0)