Skip to content

Commit 3a259ae

Browse files
committed
Adding some more tests and reducing sleep time for consul agent
startup / shutdown.
1 parent 3e4dcfe commit 3a259ae

18 files changed

+270
-186
lines changed

phpunit.local.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
<file>./tests/Usage/ConfigUsageTest.php</file>
2828
</testsuite>
2929

30-
<testsuite name="usage-consul">
31-
<file>./tests/Usage/ConsulUsageTest.php</file>
32-
</testsuite>
33-
34-
<testsuite name="usage-kv">
35-
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
30+
<testsuite name="usage-request">
31+
<file>./tests/Usage/RequestUsageTest.php</file>
3632
</testsuite>
3733

3834
<testsuite name="usage-agent">
3935
<file>./tests/Usage/Agent/AgentClientUsageTests.php</file>
4036
</testsuite>
37+
38+
<testsuite name="usage-kv">
39+
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
40+
</testsuite>
4141
</testsuites>
4242

4343
<filter>

phpunit.xml.dist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
<file>./tests/Usage/ConfigUsageTest.php</file>
2828
</testsuite>
2929

30-
<testsuite name="usage-consul">
31-
<file>./tests/Usage/ConsulUsageTest.php</file>
32-
</testsuite>
33-
34-
<testsuite name="usage-kv">
35-
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
30+
<testsuite name="usage-request">
31+
<file>./tests/Usage/RequestUsageTest.php</file>
3632
</testsuite>
3733

3834
<testsuite name="usage-agent">
3935
<file>./tests/Usage/Agent/AgentClientUsageTests.php</file>
4036
</testsuite>
37+
38+
<testsuite name="usage-kv">
39+
<file>./tests/usage/KV/KVClientCRUDTests.php</file>
40+
</testsuite>
4141
</testsuites>
4242

4343
<filter>

src/Agent/AgentClient.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public function checkDeregister($checkID) {
299299
public function join($addr, $wan = false) {
300300
$r = new Request('PUT', sprintf('v1/agent/join/%s', $addr), $this->config);
301301
if ($wan) {
302-
$r->params->set('wan', '1');
302+
$r->Params->set('wan', '1');
303303
}
304304

305305
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
@@ -326,8 +326,8 @@ public function forceLeave($node) {
326326
*/
327327
public function enableServiceMaintenance($serviceID, $reason = '') {
328328
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->config);
329-
$r->params->set('enable', 'true');
330-
$r->params->set('reason', $reason);
329+
$r->Params->set('enable', 'true');
330+
$r->Params->set('reason', $reason);
331331

332332
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
333333

@@ -340,7 +340,7 @@ public function enableServiceMaintenance($serviceID, $reason = '') {
340340
*/
341341
public function disableServiceMaintenance($serviceID) {
342342
$r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->config);
343-
$r->params->set('enable', 'false');
343+
$r->Params->set('enable', 'false');
344344

345345
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
346346

@@ -353,8 +353,8 @@ public function disableServiceMaintenance($serviceID) {
353353
*/
354354
public function enableNodeMaintenance($reason = '') {
355355
$r = new Request('PUT', 'v1/agent/maintenance', $this->config);
356-
$r->params->set('enable', 'true');
357-
$r->params->set('reason', $reason);
356+
$r->Params->set('enable', 'true');
357+
$r->Params->set('reason', $reason);
358358

359359
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
360360

@@ -366,7 +366,7 @@ public function enableNodeMaintenance($reason = '') {
366366
*/
367367
public function disableNodeMaintenance() {
368368
$r = new Request('PUT', 'v1/agent/maintenance', $this->config);
369-
$r->params->set('enable', 'false');
369+
$r->Params->set('enable', 'false');
370370

371371
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
372372

src/Catalog/CatalogClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function service($service, $tag = '', QueryOptions $options = null) {
159159
$r = new Request('GET', sprintf('v1/catalog/service/%s', $service), $this->config);
160160
$r->setQueryOptions($options);
161161
if ('' !== $tag) {
162-
$r->params->set('tag', $tag);
162+
$r->Params->set('tag', $tag);
163163
}
164164

165165
/** @var \Psr\Http\Message\ResponseInterface $response */

src/Event/EventClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public function fire(UserEvent $event, WriteOptions $options = null) {
4545
$r->setWriteOptions($options);
4646

4747
if ('' !== ($nf = $event->NodeFilter)) {
48-
$r->params->set('node', $nf);
48+
$r->Params->set('node', $nf);
4949
}
5050
if ('' !== ($sf = $event->ServiceFilter)) {
51-
$r->params->set('service', $sf);
51+
$r->Params->set('service', $sf);
5252
}
5353
if ('' !== ($tf = $event->TagFilter)) {
54-
$r->params->set('tag', $tf);
54+
$r->Params->set('tag', $tf);
5555
}
5656

5757
/** @var \Psr\Http\Message\ResponseInterface $response */
@@ -82,7 +82,7 @@ public function fire(UserEvent $event, WriteOptions $options = null) {
8282
public function eventList($name = '', QueryOptions $options = null) {
8383
$r = new Request('GET', 'v1/event/list', $this->config);
8484
if ('' !== (string)$name) {
85-
$r->params->set('name', $name);
85+
$r->Params->set('name', $name);
8686
}
8787
$r->setQueryOptions($options);
8888

src/Health/HealthClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ public function service($service, $tag = '', $passingOnly = false, QueryOptions
141141
$r = new Request('GET', sprintf('v1/health/service/%s', $service), $this->config);
142142
$r->setQueryOptions($options);
143143
if ('' !== $tag) {
144-
$r->params->set('tag', $tag);
144+
$r->Params->set('tag', $tag);
145145
}
146146
if ($passingOnly) {
147-
$r->params->set('passing', '1');
147+
$r->Params->set('passing', '1');
148148
}
149149

150150
/** @var \Psr\Http\Message\ResponseInterface $response */

src/KV/KVClient.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function put(KVPair $p, WriteOptions $options = null) {
8989
$r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config, $p->Value);
9090
$r->setWriteOptions($options);
9191
if (0 !== $p->Flags) {
92-
$r->params->set('flags', (string)$p->Flags);
92+
$r->Params->set('flags', (string)$p->Flags);
9393
}
9494

9595
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
@@ -145,7 +145,7 @@ public function valueList($prefix = '', QueryOptions $options = null) {
145145
}
146146

147147
$r->setQueryOptions($options);
148-
$r->params->set('recurse', '');
148+
$r->Params->set('recurse', '');
149149

150150
/** @var \Psr\Http\Message\ResponseInterface $response */
151151
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
@@ -195,7 +195,7 @@ public function keys($prefix = null, QueryOptions $options = null) {
195195
}
196196

197197
$r->setQueryOptions($options);
198-
$r->params->set('keys', 'true');
198+
$r->Params->set('keys', 'true');
199199

200200
/** @var \Psr\Http\Message\ResponseInterface $response */
201201
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
@@ -221,9 +221,9 @@ public function keys($prefix = null, QueryOptions $options = null) {
221221
public function cas(KVPair $p, WriteOptions $options = null) {
222222
$r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config);
223223
$r->setWriteOptions($options);
224-
$r->params->set('cas', (string)$p->ModifyIndex);
224+
$r->Params->set('cas', (string)$p->ModifyIndex);
225225
if (0 !== $p->Flags) {
226-
$r->params->set('flags', (string)$p->Flags);
226+
$r->Params->set('flags', (string)$p->Flags);
227227
}
228228

229229
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
@@ -245,9 +245,9 @@ public function cas(KVPair $p, WriteOptions $options = null) {
245245
public function acquire(KVPair $p, WriteOptions $options = null) {
246246
$r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config);
247247
$r->setWriteOptions($options);
248-
$r->params->set('acquire', $p->Session);
248+
$r->Params->set('acquire', $p->Session);
249249
if (0 !== $p->Flags) {
250-
$r->params->set('flags', (string)$p->Flags);
250+
$r->Params->set('flags', (string)$p->Flags);
251251
}
252252

253253
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
@@ -269,9 +269,9 @@ public function acquire(KVPair $p, WriteOptions $options = null) {
269269
public function release(KVPair $p, WriteOptions $options = null) {
270270
$r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config);
271271
$r->setWriteOptions($options);
272-
$r->params->set('release', $p->Session);
272+
$r->Params->set('release', $p->Session);
273273
if (0 !== $p->Flags) {
274-
$r->params->set('flags', (string)$p->Flags);
274+
$r->Params->set('flags', (string)$p->Flags);
275275
}
276276

277277
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));

src/Operator/OperatorClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function raftGetConfiguration(QueryOptions $options = null) {
6363
public function raftRemovePeerByAddress($address, WriteOptions $options = null) {
6464
$r = new Request('DELETE', 'v1/operator/raft/peer', $this->config);
6565
$r->setWriteOptions($options);
66-
$r->params->set('address', (string)$address);
66+
$r->Params->set('address', (string)$address);
6767

6868
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
6969

src/Request.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
*/
2727
class Request {
2828
/** @var \DCarbone\PHPConsulAPI\Values */
29-
public $headers;
29+
public $Headers;
3030
/** @var \DCarbone\PHPConsulAPI\Params */
31-
public $params;
31+
public $Params;
3232

3333
/** @var \DCarbone\PHPConsulAPI\Config */
3434
private $config;
@@ -56,24 +56,24 @@ public function __construct($method, $path, Config $config, $body = null) {
5656
$this->config = $config;
5757

5858
$this->method = strtoupper($method);
59-
$this->path = $path;
59+
$this->path = $path; // TODO: perform some kind of path input validation?
6060
61-
$this->headers = new Values();
62-
$this->params = new Params();
61+
$this->Headers = new Values();
62+
$this->Params = new Params();
6363

6464
if ('' !== $config->Datacenter) {
65-
$this->params->set('dc', $config->Datacenter);
65+
$this->Params->set('dc', $config->Datacenter);
6666
}
6767

6868
if (0 !== $config->WaitTime) {
69-
$this->params->set('wait', $config->intToMillisecond($config->WaitTime));
69+
$this->Params->set('wait', $config->intToMillisecond($config->WaitTime));
7070
}
7171

7272
if ('' !== $config->Token) {
7373
if ($config->TokenInHeader) {
74-
$this->headers->set('X-Consul-Token', $config->Token);
74+
$this->Headers->set('X-Consul-Token', $config->Token);
7575
} else {
76-
$this->params->set('token', $config->Token);
76+
$this->Params->set('token', $config->Token);
7777
}
7878
}
7979

@@ -121,40 +121,40 @@ public function setQueryOptions(QueryOptions $options = null) {
121121
}
122122

123123
if ('' !== $options->Datacenter) {
124-
$this->params->set('dc', $options->Datacenter);
124+
$this->Params->set('dc', $options->Datacenter);
125125
}
126126
if ($options->AllowStale) {
127-
$this->params->set('stale', '');
127+
$this->Params->set('stale', '');
128128
}
129129
if ($options->RequireConsistent) {
130-
$this->params->set('consistent', '');
130+
$this->Params->set('consistent', '');
131131
}
132132
if (0 !== $options->WaitIndex) {
133-
$this->params->set('index', (string)$options->WaitIndex);
133+
$this->Params->set('index', (string)$options->WaitIndex);
134134
}
135135
if (0 !== $options->WaitTime) {
136-
$this->params->set('wait', $this->config->intToMillisecond($options->WaitTime));
136+
$this->Params->set('wait', $this->config->intToMillisecond($options->WaitTime));
137137
}
138138
if ('' !== $options->Token) {
139139
if ($this->config->TokenInHeader) {
140-
$this->headers->set('X-Consul-Token', $options->Token);
140+
$this->Headers->set('X-Consul-Token', $options->Token);
141141
} else {
142-
$this->params->set('token', $options->Token);
142+
$this->Params->set('token', $options->Token);
143143
}
144144
}
145145
if ('' !== $options->Near) {
146-
$this->params->set('near', $options->Near);
146+
$this->Params->set('near', $options->Near);
147147
}
148148
if (isset($options->NodeMeta) && 0 < count($options->NodeMeta)) {
149149
foreach ($options->NodeMeta as $k => $v) {
150-
$this->params->add('node-meta', "{$k}:{$v}");
150+
$this->Params->add('node-meta', "{$k}:{$v}");
151151
}
152152
}
153-
if ('' !== $options->RelayFactor) {
154-
$this->params->set('relay-factor', (string)$options->RelayFactor);
153+
if (0 !== $options->RelayFactor) {
154+
$this->Params->set('relay-factor', (string)$options->RelayFactor);
155155
}
156156
if ($options->Pretty) {
157-
$this->params->set('pretty', '');
157+
$this->Params->set('pretty', '');
158158
}
159159

160160
$this->uri = null;
@@ -169,17 +169,17 @@ public function setWriteOptions(WriteOptions $options = null) {
169169
}
170170

171171
if ('' !== $options->Datacenter) {
172-
$this->params->set('dc', $options->Datacenter);
172+
$this->Params->set('dc', $options->Datacenter);
173173
}
174174
if ('' !== $options->Token) {
175175
if ($this->config->TokenInHeader) {
176-
$this->headers->set('X-Consul-Token', $options->Token);
176+
$this->Headers->set('X-Consul-Token', $options->Token);
177177
} else {
178-
$this->headers->set('token', $options->Token);
178+
$this->Headers->set('token', $options->Token);
179179
}
180180
}
181181
if (0 !== $options->RelayFactor) {
182-
$this->params->set('relay-factor', (string)$options->RelayFactor);
182+
$this->Params->set('relay-factor', (string)$options->RelayFactor);
183183
}
184184

185185
$this->uri = null;
@@ -190,9 +190,15 @@ public function setWriteOptions(WriteOptions $options = null) {
190190
*/
191191
public function getUri() {
192192
if (!isset($this->uri)) {
193-
$uri = sprintf('%s://%s/%s', $this->config->getScheme(), $this->config->Address, $this->path);
194-
if (0 < count($this->params)) {
195-
$uri = sprintf('%s?%s', $uri, (string)$this->params);
193+
$uri = sprintf(
194+
'%s://%s/%s',
195+
$this->config->getScheme(),
196+
$this->config->Address,
197+
ltrim(rtrim($this->path, " \t\n\r\0\x0B&?"),
198+
" \t\n\r\0\x0B/") // TODO: Lessen # of things being looked for?
199+
);
200+
if (0 < count($this->Params)) {
201+
$uri = sprintf('%s?%s', $uri, (string)$this->Params);
196202
}
197203
$this->uri = new Uri($uri);
198204
}
@@ -209,7 +215,7 @@ public function toPsrRequest() {
209215
return new Psr7Request(
210216
$this->method,
211217
$this->getUri(),
212-
$this->headers->toPsr7Array(),
218+
$this->Headers->toPsr7Array(),
213219
isset($this->body) ? new Psr7Stream($this->body) : null
214220
);
215221
}

src/Values.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public function set($key, $value) {
6565
throw new \InvalidArgumentException(sprintf('$key must be string, saw "%s".', gettype($key)));
6666
}
6767

68-
if (!is_string($value)) {
69-
throw new \InvalidArgumentException(sprintf('$value must be string, saw "%s".', gettype($value)));
68+
if (settype($value, 'string')) {
69+
$this->values[$key] = [$value];
70+
} else {
71+
throw new \InvalidArgumentException(sprintf('$value must be castable to string, saw "%s".',
72+
gettype($value)));
7073
}
71-
72-
$this->values[$key] = [$value];
7374
}
7475

7576
/**

0 commit comments

Comments
 (0)