|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DCarbone\PHPConsulAPITests\Unit\Agent; |
| 4 | + |
| 5 | +use DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +/** |
| 9 | + * @internal |
| 10 | + */ |
| 11 | +final class AgentCheckRegistrationTest extends TestCase |
| 12 | +{ |
| 13 | + public function testConstructorDefaults(): void |
| 14 | + { |
| 15 | + $r = new AgentCheckRegistration(); |
| 16 | + self::assertSame('', $r->getID()); |
| 17 | + self::assertSame('', $r->ID); |
| 18 | + self::assertSame('', $r->getServiceID()); |
| 19 | + self::assertSame('', $r->ServiceID); |
| 20 | + self::assertSame('', $r->getNamespace()); |
| 21 | + self::assertSame('', $r->Namespace); |
| 22 | + self::assertSame('', $r->getPartition()); |
| 23 | + self::assertSame('', $r->Partition); |
| 24 | + // inherited from AgentServiceCheck |
| 25 | + self::assertSame('', $r->getCheckID()); |
| 26 | + self::assertSame('', $r->getName()); |
| 27 | + self::assertSame([], $r->getArgs()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testConstructorWithParams(): void |
| 31 | + { |
| 32 | + $r = new AgentCheckRegistration( |
| 33 | + ID: 'chk-1', |
| 34 | + ServiceID: 'svc-1', |
| 35 | + CheckID: 'chk-id', |
| 36 | + Name: 'health', |
| 37 | + Interval: '10s', |
| 38 | + HTTP: 'http://localhost:8080/health', |
| 39 | + Namespace: 'ns-1', |
| 40 | + Partition: 'pt-1', |
| 41 | + ); |
| 42 | + self::assertSame('chk-1', $r->getID()); |
| 43 | + self::assertSame('chk-1', $r->ID); |
| 44 | + self::assertSame('svc-1', $r->getServiceID()); |
| 45 | + self::assertSame('ns-1', $r->getNamespace()); |
| 46 | + self::assertSame('pt-1', $r->getPartition()); |
| 47 | + // inherited |
| 48 | + self::assertSame('chk-id', $r->getCheckID()); |
| 49 | + self::assertSame('health', $r->getName()); |
| 50 | + self::assertSame('10s', $r->getInterval()); |
| 51 | + self::assertSame('http://localhost:8080/health', $r->getHTTP()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testFluentSetters(): void |
| 55 | + { |
| 56 | + $r = new AgentCheckRegistration(); |
| 57 | + $result = $r->setID('id-1') |
| 58 | + ->setServiceID('svc-1') |
| 59 | + ->setNamespace('ns') |
| 60 | + ->setPartition('pt'); |
| 61 | + self::assertSame($r, $result); |
| 62 | + self::assertSame('id-1', $r->getID()); |
| 63 | + self::assertSame('id-1', $r->ID); |
| 64 | + self::assertSame('svc-1', $r->getServiceID()); |
| 65 | + self::assertSame('svc-1', $r->ServiceID); |
| 66 | + self::assertSame('ns', $r->getNamespace()); |
| 67 | + self::assertSame('ns', $r->Namespace); |
| 68 | + self::assertSame('pt', $r->getPartition()); |
| 69 | + self::assertSame('pt', $r->Partition); |
| 70 | + } |
| 71 | + |
| 72 | + public function testJsonSerializeOmitsDefaults(): void |
| 73 | + { |
| 74 | + $r = new AgentCheckRegistration(); |
| 75 | + $out = $r->jsonSerialize(); |
| 76 | + self::assertInstanceOf(\stdClass::class, $out); |
| 77 | + self::assertObjectNotHasProperty('ID', $out); |
| 78 | + self::assertObjectNotHasProperty('ServiceID', $out); |
| 79 | + self::assertObjectNotHasProperty('Namespace', $out); |
| 80 | + self::assertObjectNotHasProperty('Partition', $out); |
| 81 | + } |
| 82 | + |
| 83 | + public function testJsonSerializeWithValues(): void |
| 84 | + { |
| 85 | + $r = new AgentCheckRegistration( |
| 86 | + ID: 'chk-1', |
| 87 | + ServiceID: 'svc-1', |
| 88 | + CheckID: 'cid', |
| 89 | + HTTP: 'http://localhost/health', |
| 90 | + Interval: '5s', |
| 91 | + Namespace: 'ns', |
| 92 | + Partition: 'pt', |
| 93 | + ); |
| 94 | + $out = $r->jsonSerialize(); |
| 95 | + self::assertSame('chk-1', $out->ID); |
| 96 | + self::assertSame('svc-1', $out->ServiceID); |
| 97 | + self::assertSame('ns', $out->Namespace); |
| 98 | + self::assertSame('pt', $out->Partition); |
| 99 | + self::assertSame('cid', $out->CheckID); |
| 100 | + self::assertSame('http://localhost/health', $out->HTTP); |
| 101 | + self::assertSame('5s', $out->Interval); |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments