|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Commands; |
| 6 | + |
| 7 | +use DurableWorkflow\Cli\Commands\WorkerCommand\DeregisterCommand; |
| 8 | +use DurableWorkflow\Cli\Commands\WorkerCommand\DescribeCommand; |
| 9 | +use DurableWorkflow\Cli\Commands\WorkerCommand\ListCommand; |
| 10 | +use DurableWorkflow\Cli\Commands\WorkerCommand\RegisterCommand; |
| 11 | +use DurableWorkflow\Cli\Support\ServerClient; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Tester\CommandTester; |
| 15 | + |
| 16 | +final class WorkerParityFixtureTest extends TestCase |
| 17 | +{ |
| 18 | + public function test_worker_register_matches_polyglot_request_fixture(): void |
| 19 | + { |
| 20 | + $fixture = self::fixture('worker-register-parity.json', 'worker.register'); |
| 21 | + $client = new WorkerParityClient($fixture['response_body']); |
| 22 | + $command = new RegisterCommand(); |
| 23 | + $command->setServerClient($client); |
| 24 | + |
| 25 | + $tester = new CommandTester($command); |
| 26 | + |
| 27 | + self::assertSame(Command::SUCCESS, $tester->execute($fixture['cli']['argv'])); |
| 28 | + |
| 29 | + self::assertSame($fixture['request']['method'], $client->lastMethod); |
| 30 | + self::assertSame($fixture['request']['path'], $client->lastPath); |
| 31 | + self::assertSame($fixture['request']['body'], $client->lastBody); |
| 32 | + |
| 33 | + $decoded = json_decode($tester->getDisplay(), true, flags: JSON_THROW_ON_ERROR); |
| 34 | + self::assertSame($fixture['response_body'], $decoded); |
| 35 | + self::assertSame($fixture['semantic_body']['worker_id'], $decoded['worker_id'] ?? null); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_worker_list_matches_polyglot_request_fixture(): void |
| 39 | + { |
| 40 | + $fixture = self::fixture('worker-list-parity.json', 'worker.list'); |
| 41 | + $client = new WorkerParityClient($fixture['response_body']); |
| 42 | + $command = new ListCommand(); |
| 43 | + $command->setServerClient($client); |
| 44 | + |
| 45 | + $tester = new CommandTester($command); |
| 46 | + |
| 47 | + self::assertSame(Command::SUCCESS, $tester->execute($fixture['cli']['argv'])); |
| 48 | + |
| 49 | + self::assertSame($fixture['request']['method'], $client->lastMethod); |
| 50 | + self::assertSame($fixture['request']['path'], $client->lastPath); |
| 51 | + self::assertSame($fixture['request']['query'], $client->lastQuery); |
| 52 | + |
| 53 | + $decoded = json_decode($tester->getDisplay(), true, flags: JSON_THROW_ON_ERROR); |
| 54 | + self::assertSame($fixture['response_body'], $decoded); |
| 55 | + self::assertSame($fixture['semantic_body']['worker_ids'], array_column($decoded['workers'], 'worker_id')); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_worker_describe_matches_polyglot_request_fixture(): void |
| 59 | + { |
| 60 | + $fixture = self::fixture('worker-describe-parity.json', 'worker.describe'); |
| 61 | + $client = new WorkerParityClient($fixture['response_body']); |
| 62 | + $command = new DescribeCommand(); |
| 63 | + $command->setServerClient($client); |
| 64 | + |
| 65 | + $tester = new CommandTester($command); |
| 66 | + |
| 67 | + self::assertSame(Command::SUCCESS, $tester->execute($fixture['cli']['argv'])); |
| 68 | + |
| 69 | + self::assertSame($fixture['request']['method'], $client->lastMethod); |
| 70 | + self::assertSame($fixture['request']['path'], $client->lastPath); |
| 71 | + |
| 72 | + $decoded = json_decode($tester->getDisplay(), true, flags: JSON_THROW_ON_ERROR); |
| 73 | + self::assertSame($fixture['response_body'], $decoded); |
| 74 | + self::assertSame($fixture['semantic_body']['worker_id'], $decoded['worker_id'] ?? null); |
| 75 | + self::assertSame($fixture['semantic_body']['status'], $decoded['status'] ?? null); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_worker_deregister_matches_polyglot_request_fixture(): void |
| 79 | + { |
| 80 | + $fixture = self::fixture('worker-deregister-parity.json', 'worker.deregister'); |
| 81 | + $client = new WorkerParityClient($fixture['response_body']); |
| 82 | + $command = new DeregisterCommand(); |
| 83 | + $command->setServerClient($client); |
| 84 | + |
| 85 | + $tester = new CommandTester($command); |
| 86 | + |
| 87 | + self::assertSame(Command::SUCCESS, $tester->execute($fixture['cli']['argv'])); |
| 88 | + |
| 89 | + self::assertSame($fixture['request']['method'], $client->lastMethod); |
| 90 | + self::assertSame($fixture['request']['path'], $client->lastPath); |
| 91 | + |
| 92 | + $decoded = json_decode($tester->getDisplay(), true, flags: JSON_THROW_ON_ERROR); |
| 93 | + self::assertSame($fixture['response_body'], $decoded); |
| 94 | + self::assertSame($fixture['semantic_body']['outcome'], $decoded['outcome'] ?? null); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<string, mixed> |
| 99 | + */ |
| 100 | + private static function fixture(string $file, string $operation): array |
| 101 | + { |
| 102 | + $contents = file_get_contents(__DIR__.'/../fixtures/control-plane/'.$file); |
| 103 | + self::assertIsString($contents); |
| 104 | + |
| 105 | + $fixture = json_decode($contents, true, flags: JSON_THROW_ON_ERROR); |
| 106 | + self::assertIsArray($fixture); |
| 107 | + self::assertSame('durable-workflow.polyglot.control-plane-request-fixture', $fixture['schema'] ?? null); |
| 108 | + self::assertSame($operation, $fixture['operation'] ?? null); |
| 109 | + |
| 110 | + return $fixture; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +final class WorkerParityClient extends ServerClient |
| 115 | +{ |
| 116 | + public ?string $lastMethod = null; |
| 117 | + |
| 118 | + public ?string $lastPath = null; |
| 119 | + |
| 120 | + /** |
| 121 | + * @var array<string, mixed> |
| 122 | + */ |
| 123 | + public array $lastQuery = []; |
| 124 | + |
| 125 | + /** |
| 126 | + * @var array<string, mixed> |
| 127 | + */ |
| 128 | + public array $lastBody = []; |
| 129 | + |
| 130 | + /** |
| 131 | + * @param array<string, mixed> $response |
| 132 | + */ |
| 133 | + public function __construct(private readonly array $response) |
| 134 | + { |
| 135 | + parent::__construct('http://localhost'); |
| 136 | + } |
| 137 | + |
| 138 | + public function get(string $path, array $query = []): array |
| 139 | + { |
| 140 | + $this->lastMethod = 'GET'; |
| 141 | + $this->lastPath = $path; |
| 142 | + $this->lastQuery = $query; |
| 143 | + |
| 144 | + return $this->response; |
| 145 | + } |
| 146 | + |
| 147 | + public function post(string $path, array $body = []): array |
| 148 | + { |
| 149 | + $this->lastMethod = 'POST'; |
| 150 | + $this->lastPath = $path; |
| 151 | + $this->lastBody = $body; |
| 152 | + |
| 153 | + return $this->response; |
| 154 | + } |
| 155 | + |
| 156 | + public function delete(string $path): array |
| 157 | + { |
| 158 | + $this->lastMethod = 'DELETE'; |
| 159 | + $this->lastPath = $path; |
| 160 | + $this->lastBody = []; |
| 161 | + |
| 162 | + return $this->response; |
| 163 | + } |
| 164 | +} |
0 commit comments