Skip to content

Commit 00c0fcf

Browse files
committed
More Util tests;
Added persistent variant of the state altering client tests; More efficient comma separated list filtering at Util::find().
1 parent 3a895da commit 00c0fcf

File tree

6 files changed

+222
-16
lines changed

6 files changed

+222
-16
lines changed

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,18 @@ public function find()
194194
$idList .= $criteria . ',';
195195
} else {
196196
$criteriaArr = explode(',', $criteria);
197-
array_filter(
198-
$criteriaArr,
199-
function ($value) use (&$idList) {
200-
if ('' === $value) {
201-
return false;
202-
}
203-
if ('*' === $value[0]) {
204-
$idList .= $value . ',';
205-
return false;
206-
}
207-
return true;
197+
for ($i = count($criteriaArr) - 1; $i >= 0; --$i) {
198+
if ('' === $criteriaArr[$i]) {
199+
unset($criteriaArr[$i]);
200+
} elseif ('*' === $criteriaArr[$i][0]) {
201+
$idList .= $criteriaArr[$i] . ',';
202+
unset($criteriaArr[$i]);
208203
}
209-
);
204+
}
210205
$idList .= call_user_func_array(
211206
array($this, 'find'),
212207
$criteriaArr
213-
);
208+
) . ',';
214209
}
215210
}
216211
}

tests/ClientStateAlteringFeaturesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace PEAR2\Net\RouterOS;
33

4-
class StateAlteringFeaturesTest extends \PHPUnit_Framework_TestCase
4+
class ClientStateAlteringFeaturesTest extends \PHPUnit_Framework_TestCase
55
{
66

77
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace PEAR2\Net\RouterOS;
3+
4+
require_once 'ClientStateAlteringFeaturesTest.php';
5+
6+
class ClientStateAlteringPersistentFeaturesTest extends ClientStateAlteringFeaturesTest
7+
{
8+
9+
protected function setUp()
10+
{
11+
$this->object = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT, true);
12+
}
13+
14+
protected function tearDown()
15+
{
16+
$this->object->close();
17+
unset($this->object);
18+
}
19+
}

tests/UtilFeaturesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class UtilFeaturesTest extends \PHPUnit_Framework_TestCase
1414
* @var Client
1515
*/
1616
protected $client;
17-
17+
1818
protected function setUp()
1919
{
2020
$this->util = new Util(
2121
$this->client = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT)
2222
);
2323
}
24-
24+
2525
protected function tearDown()
2626
{
2727
unset($this->util);
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
namespace PEAR2\Net\RouterOS;
3+
4+
class UtilStateAlteringFeaturesTest extends \PHPUnit_Framework_TestCase
5+
{
6+
const REGEX_ID = '\*[A-F0-9]+';
7+
const REGEX_IDLIST = '/^(\*[A-F0-9]+\,)*(\*[A-F0-9]+)?$/';
8+
9+
/**
10+
* @var Util
11+
*/
12+
protected $util;
13+
/**
14+
* @var Client
15+
*/
16+
protected $client;
17+
18+
protected function setUp()
19+
{
20+
$this->util = new Util(
21+
$this->client = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT)
22+
);
23+
}
24+
25+
protected function tearDown()
26+
{
27+
unset($this->util);
28+
unset($this->client);
29+
}
30+
31+
public function testAdd()
32+
{
33+
$printRequest = new Request('/queue/simple/print');
34+
$beforeCount = count($this->client->sendSync($printRequest));
35+
$this->util->changeMenu('/queue/simple');
36+
$this->assertRegExp(
37+
'/^' . self::REGEX_ID . '$/',
38+
$id = $this->util->add(array('name' => TEST_QUEUE_NAME))
39+
);
40+
$afterCount = count($this->client->sendSync($printRequest));
41+
$this->assertSame(1 + $beforeCount, $afterCount);
42+
43+
$removeRequest = new Request('/queue/simple/remove');
44+
$removeRequest->setArgument('numbers', $id);
45+
$this->client->sendSync($removeRequest);
46+
47+
$postCount = count($this->client->sendSync($printRequest));
48+
$this->assertSame($beforeCount, $postCount);
49+
}
50+
51+
/**
52+
* @depends testAdd
53+
*/
54+
public function testDisableAndEnable()
55+
{
56+
$this->util->changeMenu('/queue/simple');
57+
$id = $this->util->add(
58+
array('name' => TEST_QUEUE_NAME, 'disabled' => 'no')
59+
);
60+
$printRequest = new Request(
61+
'/queue/simple/print',
62+
Query::where('.id', $id)
63+
);
64+
65+
$this->assertSame(
66+
'false',
67+
$this->client->sendSync($printRequest)->getArgument('disabled')
68+
);
69+
70+
$this->util->disable($id);
71+
72+
$this->assertSame(
73+
'true',
74+
$this->client->sendSync($printRequest)->getArgument('disabled')
75+
);
76+
77+
$this->util->enable($id);
78+
79+
$this->assertSame(
80+
'false',
81+
$this->client->sendSync($printRequest)->getArgument('disabled')
82+
);
83+
84+
$removeRequest = new Request('/queue/simple/remove');
85+
$removeRequest->setArgument('numbers', $id);
86+
$this->client->sendSync($removeRequest);
87+
}
88+
89+
/**
90+
* @depends testDisableAndEnable
91+
*/
92+
public function testRemove()
93+
{
94+
$printRequest = new Request('/queue/simple/print');
95+
$beforeCount = count($this->client->sendSync($printRequest));
96+
$this->util->changeMenu('/queue/simple');
97+
$this->assertRegExp(
98+
'/^' . self::REGEX_ID . '$/',
99+
$id = $this->util->add(array('name' => TEST_QUEUE_NAME))
100+
);
101+
$afterCount = count($this->client->sendSync($printRequest));
102+
$this->assertSame(1 + $beforeCount, $afterCount);
103+
104+
$this->util->remove($id);
105+
106+
$postCount = count($this->client->sendSync($printRequest));
107+
$this->assertSame($beforeCount, $postCount);
108+
}
109+
110+
/**
111+
* @depends testRemove
112+
*/
113+
public function testSetAndEdit()
114+
{
115+
$this->util->changeMenu('/queue/simple');
116+
$id = $this->util->add(
117+
array(
118+
'name' => TEST_QUEUE_NAME,
119+
'target-addresses' => HOSTNAME_SILENT . '/32'
120+
)
121+
);
122+
123+
$printRequest = new Request(
124+
'/queue/simple/print',
125+
Query::where('.id', $id)
126+
);
127+
128+
$this->assertSame(
129+
HOSTNAME_SILENT . '/32',
130+
$this->client->sendSync(
131+
$printRequest
132+
)->getArgument('target-addresses')
133+
);
134+
135+
$this->util->set(
136+
$id,
137+
array(
138+
'target-addresses' => HOSTNAME_INVALID . '/32'
139+
)
140+
);
141+
142+
$this->assertSame(
143+
HOSTNAME_INVALID . '/32',
144+
$this->client->sendSync(
145+
$printRequest
146+
)->getArgument('target-addresses')
147+
);
148+
149+
$this->util->edit(
150+
$id,
151+
array(
152+
'target-addresses' => HOSTNAME_SILENT . '/32'
153+
)
154+
);
155+
156+
$this->assertSame(
157+
HOSTNAME_SILENT . '/32',
158+
$this->client->sendSync(
159+
$printRequest
160+
)->getArgument('target-addresses')
161+
);
162+
163+
$this->util->remove($id);
164+
}
165+
166+
/**
167+
* @depends testAdd
168+
* @depends testRemove
169+
*/
170+
public function testFindByNumber()
171+
{
172+
$this->util->changeMenu('/queue/simple');
173+
$itemCount = count(explode(',', $this->util->find()));
174+
$id = $this->util->add(
175+
array(
176+
'name' => TEST_QUEUE_NAME,
177+
'target-addresses' => HOSTNAME_SILENT . '/32'
178+
)
179+
);
180+
$this->assertSame(
181+
1 + $itemCount,
182+
count(explode(',', $this->util->find()))
183+
);
184+
$this->assertSame($id, $this->util->find($itemCount));
185+
$this->assertSame($id, $this->util->find((string)$itemCount));
186+
187+
$this->util->remove($id);
188+
}
189+
}

tests/phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<file>ClientFeaturesTest.php</file>
100100
<file>ClientPersistentFeaturesTest.php</file>
101101
<file>ClientStateAlteringFeaturesTest.php</file>
102+
<file>ClientStateAlteringPersistentFeaturesTest.php</file>
103+
<file>UtilFeaturesTest.php</file>
104+
<file>UtilStateAlteringFeaturesTest.php</file>
102105
</testsuite>
103106
</testsuites>
104107
<filter>

0 commit comments

Comments
 (0)