Skip to content

Commit 23a684c

Browse files
committed
long way to go yet.
1 parent f5c842f commit 23a684c

12 files changed

Lines changed: 1792 additions & 3 deletions

.github/copilot-instructions.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@
1818
* Do not do this in PHPUnit test class files, as PHPUnit does not support strict types.
1919
* **Always** ensure that constructor parameters for enum fields also accept the enum value type
2020
(e.g. `public function __construct(string $foo, string|MyEnum $bar)`).
21-
* Write tests for both cases.
21+
* Write tests for both cases.
22+
23+
# When generting code for concrete implementations of AbstractType:
24+
* **Always** ensure that class fields have an associated getter and setter method.
25+
* **Always** ensure the constructor has parameters for all class fields, and that the constructor parameters are
26+
assigned to the class fields.
27+
* **Always** write unit tests that use both the constructor and the setter methods to set class fields,
28+
and that use the getter methods and fields directly to verify that the fields were set correctly.

tests/Unit/Agent/AgentMemberTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testConstructorDefaults(): void
1616
self::assertSame('', $m->getName());
1717
self::assertSame('', $m->getAddr());
1818
self::assertSame(0, $m->getPort());
19-
self::assertSame([], $m->getTags());
19+
self::assertSame([], $m->Tags);
2020
self::assertSame(0, $m->getStatus());
2121
self::assertSame(0, $m->getProtocolMin());
2222
self::assertSame(0, $m->getProtocolMax());
@@ -32,7 +32,7 @@ public function testConstructorWithParams(): void
3232
self::assertSame('node1', $m->getName());
3333
self::assertSame('127.0.0.1', $m->getAddr());
3434
self::assertSame(8301, $m->getPort());
35-
self::assertSame(['role' => 'consul'], $m->getTags());
35+
self::assertSame(['role' => 'consul'], $m->Tags);
3636
self::assertSame(1, $m->getStatus());
3737
}
3838

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace DCarbone\PHPConsulAPITests\Unit\Agent;
4+
5+
use DCarbone\PHPConsulAPI\Agent\AgentServiceCheck;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @internal
10+
*/
11+
final class AgentServiceCheckTest extends TestCase
12+
{
13+
public function testConstructorDefaults(): void
14+
{
15+
$c = new AgentServiceCheck();
16+
self::assertSame('', $c->getCheckID());
17+
self::assertSame('', $c->getName());
18+
self::assertSame([], $c->getArgs());
19+
self::assertSame('', $c->getDockerContainerID());
20+
self::assertSame('', $c->getInterval());
21+
self::assertSame('', $c->getTimeout());
22+
self::assertSame('', $c->getTTL());
23+
self::assertSame('', $c->getHTTP());
24+
self::assertSame('', $c->getMethod());
25+
self::assertSame('', $c->getTCP());
26+
self::assertSame('', $c->getStatus());
27+
self::assertSame('', $c->getNotes());
28+
self::assertFalse($c->isTLSSkipVerify());
29+
self::assertSame('', $c->getGRPC());
30+
self::assertFalse($c->isGRPCUseTLS());
31+
self::assertSame(0, $c->getSuccessBeforePassing());
32+
self::assertSame(0, $c->getFailuresBeforeCritical());
33+
self::assertSame('', $c->getDeregisterCriticalServiceAfter());
34+
}
35+
36+
public function testConstructorWithParams(): void
37+
{
38+
$c = new AgentServiceCheck(
39+
CheckID: 'chk-1',
40+
Name: 'http-check',
41+
HTTP: 'http://localhost:8080/health',
42+
Interval: '10s',
43+
Timeout: '5s',
44+
Status: 'critical',
45+
);
46+
self::assertSame('chk-1', $c->getCheckID());
47+
self::assertSame('http-check', $c->getName());
48+
self::assertSame('http://localhost:8080/health', $c->getHTTP());
49+
self::assertSame('10s', $c->getInterval());
50+
self::assertSame('5s', $c->getTimeout());
51+
self::assertSame('critical', $c->getStatus());
52+
}
53+
54+
public function testFluentSetters(): void
55+
{
56+
$c = new AgentServiceCheck();
57+
$result = $c->setCheckID('c')->setName('n')->setHTTP('http://example.com')
58+
->setInterval('5s')->setTimeout('2s')->setStatus('passing');
59+
self::assertSame($c, $result);
60+
self::assertSame('c', $c->getCheckID());
61+
self::assertSame('http://example.com', $c->getHTTP());
62+
}
63+
64+
public function testJsonSerialize(): void
65+
{
66+
$c = new AgentServiceCheck(CheckID: 'chk', Name: 'test', TTL: '30s');
67+
$out = $c->jsonSerialize();
68+
self::assertSame('chk', $out->CheckID);
69+
self::assertSame('test', $out->Name);
70+
self::assertSame('30s', $out->TTL);
71+
}
72+
73+
public function testJsonUnserialize(): void
74+
{
75+
$d = new \stdClass();
76+
$d->CheckID = 'c1';
77+
$d->Name = 'check';
78+
$d->HTTP = 'http://localhost';
79+
$d->Interval = '10s';
80+
$c = AgentServiceCheck::jsonUnserialize($d);
81+
self::assertSame('c1', $c->getCheckID());
82+
self::assertSame('http://localhost', $c->getHTTP());
83+
}
84+
}
85+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace DCarbone\PHPConsulAPITests\Unit\Agent;
4+
5+
use DCarbone\PHPConsulAPI\Agent\AgentServiceChecksInfo;
6+
use DCarbone\PHPConsulAPI\Agent\AgentService;
7+
use DCarbone\PHPConsulAPI\Health\HealthChecks;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class AgentServiceChecksInfoTest extends TestCase
14+
{
15+
public function testConstructorDefaults(): void
16+
{
17+
$i = new AgentServiceChecksInfo();
18+
self::assertSame('', $i->getAggregatedStatus());
19+
self::assertNull($i->getService());
20+
self::assertInstanceOf(HealthChecks::class, $i->getChecks());
21+
}
22+
23+
public function testConstructorWithParams(): void
24+
{
25+
$svc = new AgentService(ID: 'svc-1', Service: 'web');
26+
$i = new AgentServiceChecksInfo(AggregatedStatus: 'passing', Service: $svc);
27+
self::assertSame('passing', $i->getAggregatedStatus());
28+
self::assertSame($svc, $i->getService());
29+
}
30+
31+
public function testFluentSetters(): void
32+
{
33+
$i = new AgentServiceChecksInfo();
34+
$svc = new AgentService(ID: 's');
35+
$result = $i->setAggregatedStatus('critical')->setService($svc);
36+
self::assertSame($i, $result);
37+
self::assertSame('critical', $i->getAggregatedStatus());
38+
self::assertSame($svc, $i->getService());
39+
}
40+
}
41+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace DCarbone\PHPConsulAPITests\Unit\Agent;
4+
5+
use DCarbone\PHPConsulAPI\Agent\AgentServiceConnectProxyConfig;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @internal
10+
*/
11+
final class AgentServiceConnectProxyConfigTest extends TestCase
12+
{
13+
public function testConstructorDefaults(): void
14+
{
15+
$p = new AgentServiceConnectProxyConfig();
16+
self::assertSame('', $p->getDestinationServiceName());
17+
self::assertSame('', $p->getDestinationServiceID());
18+
self::assertSame('', $p->getLocalServiceAddress());
19+
self::assertSame(0, $p->getLocalServicePort());
20+
self::assertSame([], $p->getEnvoyExtensions());
21+
self::assertSame([], $p->getUpstreams());
22+
}
23+
24+
public function testConstructorWithParams(): void
25+
{
26+
$p = new AgentServiceConnectProxyConfig(
27+
DestinationServiceName: 'web',
28+
DestinationServiceID: 'web-1',
29+
LocalServiceAddress: '127.0.0.1',
30+
LocalServicePort: 8080,
31+
);
32+
self::assertSame('web', $p->getDestinationServiceName());
33+
self::assertSame('web-1', $p->getDestinationServiceID());
34+
self::assertSame('127.0.0.1', $p->getLocalServiceAddress());
35+
self::assertSame(8080, $p->getLocalServicePort());
36+
}
37+
38+
public function testFluentSetters(): void
39+
{
40+
$p = new AgentServiceConnectProxyConfig();
41+
$result = $p->setDestinationServiceName('svc')->setDestinationServiceID('svc-1')
42+
->setLocalServiceAddress('10.0.0.1')->setLocalServicePort(9090);
43+
self::assertSame($p, $result);
44+
self::assertSame('svc', $p->getDestinationServiceName());
45+
}
46+
47+
public function testJsonSerialize(): void
48+
{
49+
$p = new AgentServiceConnectProxyConfig(DestinationServiceName: 'web', LocalServicePort: 80);
50+
$out = $p->jsonSerialize();
51+
self::assertSame('web', $out->DestinationServiceName);
52+
self::assertSame(80, $out->LocalServicePort);
53+
}
54+
}
55+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace DCarbone\PHPConsulAPITests\Unit\Agent;
4+
5+
use DCarbone\PHPConsulAPI\Agent\ConnectProxyConfig;
6+
use DCarbone\PHPConsulAPI\Agent\Upstream;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ConnectProxyConfigTest extends TestCase
13+
{
14+
public function testConstructorDefaults(): void
15+
{
16+
$c = new ConnectProxyConfig();
17+
self::assertSame('', $c->getProxyServiceID());
18+
self::assertSame('', $c->getTargetServiceID());
19+
self::assertSame('', $c->getTargetServiceName());
20+
self::assertSame('', $c->getContentHash());
21+
self::assertSame([], $c->getConfig());
22+
self::assertSame([], $c->getUpstreams());
23+
}
24+
25+
public function testConstructorWithParams(): void
26+
{
27+
$u = new Upstream(DestinationName: 'db', LocalBindPort: 5432);
28+
$c = new ConnectProxyConfig(
29+
ProxyServiceID: 'proxy-1',
30+
TargetServiceID: 'web-1',
31+
TargetServiceName: 'web',
32+
ContentHash: 'hash',
33+
Config: ['protocol' => 'http'],
34+
Upstreams: [$u],
35+
);
36+
self::assertSame('proxy-1', $c->getProxyServiceID());
37+
self::assertSame('web', $c->getTargetServiceName());
38+
self::assertSame(['protocol' => 'http'], $c->getConfig());
39+
self::assertCount(1, $c->getUpstreams());
40+
}
41+
42+
public function testFluentSetters(): void
43+
{
44+
$c = new ConnectProxyConfig();
45+
$result = $c->setProxyServiceID('p')->setTargetServiceID('t')
46+
->setTargetServiceName('n')->setContentHash('h')
47+
->setConfig(['k' => 'v'])->setUpstreams();
48+
self::assertSame($c, $result);
49+
self::assertSame('p', $c->getProxyServiceID());
50+
}
51+
}
52+

tests/Unit/Agent/UpstreamTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace DCarbone\PHPConsulAPITests\Unit\Agent;
4+
5+
use DCarbone\PHPConsulAPI\Agent\Upstream;
6+
use DCarbone\PHPConsulAPI\Agent\UpstreamDestType;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class UpstreamTest extends TestCase
13+
{
14+
public function testConstructorDefaults(): void
15+
{
16+
$u = new Upstream();
17+
self::assertSame(UpstreamDestType::UNDEFINED, $u->getDestinationType());
18+
self::assertSame('', $u->getDestinationPartition());
19+
self::assertSame('', $u->getDestinationNamespace());
20+
self::assertSame('', $u->getDestinationPeer());
21+
self::assertSame('', $u->getDestinationName());
22+
self::assertSame('', $u->getDatacenter());
23+
self::assertSame('', $u->getLocalBindAddress());
24+
self::assertSame(0, $u->getLocalBindPort());
25+
self::assertFalse($u->isCentrallyConfigured());
26+
}
27+
28+
public function testConstructorWithParams(): void
29+
{
30+
$u = new Upstream(
31+
DestinationType: UpstreamDestType::Service,
32+
DestinationName: 'db',
33+
LocalBindAddress: '127.0.0.1',
34+
LocalBindPort: 5432,
35+
);
36+
self::assertSame(UpstreamDestType::Service, $u->getDestinationType());
37+
self::assertSame('db', $u->getDestinationName());
38+
self::assertSame(5432, $u->getLocalBindPort());
39+
}
40+
41+
public function testConstructorWithStringDestType(): void
42+
{
43+
$u = new Upstream(DestinationType: 'service', DestinationName: 'db');
44+
self::assertSame(UpstreamDestType::Service, $u->getDestinationType());
45+
}
46+
47+
public function testFluentSetters(): void
48+
{
49+
$u = new Upstream();
50+
$result = $u->setDestinationType(UpstreamDestType::PreparedQuery)
51+
->setDestinationName('query')
52+
->setLocalBindPort(9999);
53+
self::assertSame($u, $result);
54+
self::assertSame(UpstreamDestType::PreparedQuery, $u->getDestinationType());
55+
}
56+
57+
public function testJsonSerialize(): void
58+
{
59+
$u = new Upstream(DestinationType: UpstreamDestType::Service, DestinationName: 'db', LocalBindPort: 5432);
60+
$out = $u->jsonSerialize();
61+
self::assertSame('db', $out->DestinationName);
62+
self::assertSame(5432, $out->LocalBindPort);
63+
}
64+
}
65+

0 commit comments

Comments
 (0)