Skip to content

Commit 78373e7

Browse files
committed
Bugfix and tests
- Was using incorrect Check model with "AgentServiceRegistration" object. Will need to check rest of check objects. - More tests
1 parent 7605578 commit 78373e7

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

src/Agent/AgentServiceRegistration.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class AgentServiceRegistration extends AbstractModel {
3737
public $Address = '';
3838
/** @var bool */
3939
public $EnableTagOverride = false;
40-
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck */
40+
/** @var \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */
4141
public $Check = null;
42-
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck[] */
42+
/** @var \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck[] */
4343
public $Checks = [];
4444

4545
/**
@@ -49,16 +49,16 @@ class AgentServiceRegistration extends AbstractModel {
4949
public function __construct(array $data = []) {
5050
parent::__construct($data);
5151

52-
if (null !== $this->Check && !($this->Check instanceof AgentCheck)) {
53-
$this->Check = new AgentCheck((array)$this->Check);
52+
if (null !== $this->Check && !($this->Check instanceof AgentServiceCheck)) {
53+
$this->Check = new AgentServiceCheck((array)$this->Check);
5454
}
5555

5656
if (0 < count($this->Checks)) {
5757
$this->Checks = array_filter($this->Checks);
5858
if (0 < ($cnt = count($this->Checks))) {
5959
for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++) {
60-
if (!($this->Checks[$i] instanceof AgentCheck)) {
61-
$this->Checks[$i] = new AgentCheck($this->Checks[$i]);
60+
if (!($this->Checks[$i] instanceof AgentServiceCheck)) {
61+
$this->Checks[$i] = new AgentServiceCheck($this->Checks[$i]);
6262
}
6363
}
6464
}
@@ -146,30 +146,30 @@ public function setEnableTagOverride($EnableTagOverride) {
146146
}
147147

148148
/**
149-
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheck
149+
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck
150150
*/
151151
public function getCheck() {
152152
return $this->Check;
153153
}
154154

155155
/**
156-
* @param \DCarbone\PHPConsulAPI\Agent\AgentCheck $Check
156+
* @param \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck $Check
157157
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration
158158
*/
159-
public function setCheck(AgentCheck $Check) {
159+
public function setCheck(AgentServiceCheck $Check) {
160160
$this->Check = $Check;
161161
return $this;
162162
}
163163

164164
/**
165-
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheck[]
165+
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck[]
166166
*/
167167
public function getChecks() {
168168
return $this->Checks;
169169
}
170170

171171
/**
172-
* @param \DCarbone\PHPConsulAPI\Agent\AgentCheck[] $Checks
172+
* @param \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck[] $Checks
173173
* @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration
174174
*/
175175
public function setChecks(array $Checks) {
@@ -180,10 +180,10 @@ public function setChecks(array $Checks) {
180180
}
181181

182182
/**
183-
* @param \DCarbone\PHPConsulAPI\Agent\AgentCheck $Check
183+
* @param \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck $Check
184184
* @return $this
185185
*/
186-
public function addCheck(AgentCheck $Check) {
186+
public function addCheck(AgentServiceCheck $Check) {
187187
$this->Checks[] = $Check;
188188
return $this;
189189
}

tests/Usage/Agent/AgentClientUsageTests.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use DCarbone\PHPConsulAPI\Agent\AgentClient;
2020
use DCarbone\PHPConsulAPI\Agent\AgentMember;
2121
use DCarbone\PHPConsulAPI\Agent\AgentService;
22+
use DCarbone\PHPConsulAPI\Agent\AgentServiceCheck;
2223
use DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration;
2324
use DCarbone\PHPConsulAPI\Config;
2425
use DCarbone\PHPConsulAPITests\ConsulManager;
@@ -174,4 +175,46 @@ public function testCanDeregisterService() {
174175
throw $e;
175176
}
176177
}
178+
179+
/**
180+
* @depends testCanDeregisterService
181+
*/
182+
public function testCanRegisterServiceWithCheck() {
183+
$client = new AgentClient(new Config());
184+
185+
$svc = new AgentServiceRegistration();
186+
$svc
187+
->setName(self::ServiceName)
188+
->setPort(1234)
189+
->setAddress('127.0.0.1')
190+
->setCheck(new AgentServiceCheck([
191+
'TCP' => '127.0.0.1',
192+
'Interval' => '30s',
193+
]));
194+
195+
$err = $client->serviceRegister($svc);
196+
$this->assertNull($err, sprintf('Error registering service with check: %s', $err));
197+
198+
sleep(2);
199+
200+
list($svcs, $err) = $client->services();
201+
202+
try {
203+
$this->assertNull($err, sprintf('AgentClient::services returned error: %s', $err));
204+
$this->assertInternalType('array', $svcs);
205+
$this->assertContainsOnlyInstancesOf(AgentService::class, $svcs);
206+
$this->assertCount(2, $svcs);
207+
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
208+
echo "\nservices list:\n";
209+
var_dump($svcs);
210+
echo "\n";
211+
212+
throw $e;
213+
}
214+
215+
$err = $client->serviceDeregister(self::ServiceName);
216+
$this->assertNull($err, sprintf('Error deregistering service: %s', $err));
217+
}
218+
219+
177220
}

0 commit comments

Comments
 (0)