From 5cbc70b9f33e4e1b72d6a84ccad51c195613502e Mon Sep 17 00:00:00 2001 From: Ferror Date: Thu, 25 Jan 2024 23:58:20 +0100 Subject: [PATCH 1/9] feat: schema V3 --- src/Schema/V3/OperationRenderer.php | 10 ++++++++++ src/Schema/V3/OperationType.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/Schema/V3/OperationRenderer.php create mode 100644 src/Schema/V3/OperationType.php diff --git a/src/Schema/V3/OperationRenderer.php b/src/Schema/V3/OperationRenderer.php new file mode 100644 index 0000000..08b69fb --- /dev/null +++ b/src/Schema/V3/OperationRenderer.php @@ -0,0 +1,10 @@ + Date: Fri, 26 Jan 2024 00:26:26 +0100 Subject: [PATCH 2/9] feat: tests --- src/Schema/V3/OperationRenderer.php | 5 +- .../Unit/Schema/V3/OperationRendererTest.php | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Schema/V3/OperationRendererTest.php diff --git a/src/Schema/V3/OperationRenderer.php b/src/Schema/V3/OperationRenderer.php index 08b69fb..47910fc 100644 --- a/src/Schema/V3/OperationRenderer.php +++ b/src/Schema/V3/OperationRenderer.php @@ -6,5 +6,8 @@ final readonly class OperationRenderer { - + public function render(array $document): array + { + return []; + } } diff --git a/tests/Unit/Schema/V3/OperationRendererTest.php b/tests/Unit/Schema/V3/OperationRendererTest.php new file mode 100644 index 0000000..bd87481 --- /dev/null +++ b/tests/Unit/Schema/V3/OperationRendererTest.php @@ -0,0 +1,54 @@ +render($document); + + $expected = [ + 'UserSignedUpOperation' => [ + 'action' => 'send', + 'messages' => [ + 'UserSignedUp' => [ + '$ref' => '#/components/messages/UserSignedUp', + ] + ] + ] + ]; + + $this->assertEquals($expected, $actual); + } + + public function testItRendersReceiveAction(): void + { + $renderer = new OperationRenderer(); + + $document = []; + + $actual = $renderer->render($document); + + $expected = [ + 'UserSignedUpOperation' => [ + 'action' => 'receive', + 'channel' => [ + '$ref' => '#/channels/UserSignedUpChannel' + ] + ] + ]; + + $this->assertEquals($expected, $actual); + } +} From 9dd0005401e89b1ae60d4473f63f750e3db6a040 Mon Sep 17 00:00:00 2001 From: Ferror Date: Fri, 26 Jan 2024 00:42:30 +0100 Subject: [PATCH 3/9] feat: channel and operation attribute --- src/Attribute/Operation.php | 18 ++++++++++++++++++ src/Schema/V3/InfoRenderer.php | 15 ++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/Attribute/Operation.php diff --git a/src/Attribute/Operation.php b/src/Attribute/Operation.php new file mode 100644 index 0000000..c162007 --- /dev/null +++ b/src/Attribute/Operation.php @@ -0,0 +1,18 @@ + $this->title, - 'version' => $this->version, - 'description' => $this->description, + 'title' => $document['title'], + 'version' => $document['version'], + 'description' => $document['description'], ]; } } From 7ae7cec3a18f1457da040eb9103176aa2e4ac09b Mon Sep 17 00:00:00 2001 From: Ferror Date: Fri, 2 Feb 2024 17:46:10 +0100 Subject: [PATCH 4/9] part --- src/Attribute/Operation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Attribute/Operation.php b/src/Attribute/Operation.php index c162007..ad76fc4 100644 --- a/src/Attribute/Operation.php +++ b/src/Attribute/Operation.php @@ -13,6 +13,7 @@ class Operation public function __construct( public string $name, public OperationType $type = OperationType::SEND, + public array $channels = [], ) { } } From d6d805c64089233746c692fd148c0e7651a9625d Mon Sep 17 00:00:00 2001 From: Ferror Date: Sat, 3 Feb 2024 00:10:40 +0100 Subject: [PATCH 5/9] OperationRenderer --- src/Attribute/Operation.php | 12 ++++++ src/Schema/V3/OperationRenderer.php | 15 ++++++- .../Unit/Schema/V3/OperationRendererTest.php | 42 +++++++++++++++---- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/Attribute/Operation.php b/src/Attribute/Operation.php index ad76fc4..743c3a6 100644 --- a/src/Attribute/Operation.php +++ b/src/Attribute/Operation.php @@ -16,4 +16,16 @@ public function __construct( public array $channels = [], ) { } + + public function toArray(): array + { + return [ + 'name' => $this->name, + 'type' => $this->type->value, + 'channels' => array_map( + static fn (Channel $channel) => $channel->toArray(), + $this->channels + ), + ]; + } } diff --git a/src/Schema/V3/OperationRenderer.php b/src/Schema/V3/OperationRenderer.php index 47910fc..ac86384 100644 --- a/src/Schema/V3/OperationRenderer.php +++ b/src/Schema/V3/OperationRenderer.php @@ -8,6 +8,19 @@ { public function render(array $document): array { - return []; + $operations = []; + + foreach ($document['operations'] as $operation) { + foreach ($operation['channels'] as $channel) { + $operations[$operation['name']] = [ + 'action' => $operation['type'], + 'channel' => [ + '$ref' => '#/channels/' . $channel['name'], + ], + ]; + } + } + + return $operations; } } diff --git a/tests/Unit/Schema/V3/OperationRendererTest.php b/tests/Unit/Schema/V3/OperationRendererTest.php index bd87481..ddc835f 100644 --- a/tests/Unit/Schema/V3/OperationRendererTest.php +++ b/tests/Unit/Schema/V3/OperationRendererTest.php @@ -14,17 +14,30 @@ public function testItRendersSendAction(): void { $renderer = new OperationRenderer(); - $document = []; + $document = [ + 'name' => 'UserSignedUp', + 'properties' => [], + 'operations' => [ + [ + 'name' => 'UserSignedUpOperation', + 'type' => 'send', + 'channels' => [ + [ + 'name' => 'UserSignedUpChannel', + 'type' => 'subscribe', + ] + ], + ] + ], + ]; $actual = $renderer->render($document); $expected = [ 'UserSignedUpOperation' => [ 'action' => 'send', - 'messages' => [ - 'UserSignedUp' => [ - '$ref' => '#/components/messages/UserSignedUp', - ] + 'channel' => [ + '$ref' => '#/channels/UserSignedUpChannel', ] ] ]; @@ -36,7 +49,22 @@ public function testItRendersReceiveAction(): void { $renderer = new OperationRenderer(); - $document = []; + $document = [ + 'name' => 'UserSignedUp', + 'properties' => [], + 'operations' => [ + [ + 'name' => 'UserSignedUpOperation', + 'type' => 'receive', + 'channels' => [ + [ + 'name' => 'UserSignedUpChannel', + 'type' => 'subscribe', + ] + ], + ] + ], + ]; $actual = $renderer->render($document); @@ -44,7 +72,7 @@ public function testItRendersReceiveAction(): void 'UserSignedUpOperation' => [ 'action' => 'receive', 'channel' => [ - '$ref' => '#/channels/UserSignedUpChannel' + '$ref' => '#/channels/UserSignedUpChannel', ] ] ]; From bec314333a3a157d948cba04a7aafe31a7620a59 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sat, 3 Feb 2024 20:55:17 +0100 Subject: [PATCH 6/9] a --- src/Attribute/Message.php | 1 + src/Attribute/Operation.php | 5 +++++ .../AttributeDocumentationStrategy.php | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/src/Attribute/Message.php b/src/Attribute/Message.php index 73f8d98..daf7faf 100644 --- a/src/Attribute/Message.php +++ b/src/Attribute/Message.php @@ -17,6 +17,7 @@ public function __construct( public readonly string $name, public array $properties = [], public array $channels = [], + public array $operations = [], ) { } diff --git a/src/Attribute/Operation.php b/src/Attribute/Operation.php index 743c3a6..f52fcdb 100644 --- a/src/Attribute/Operation.php +++ b/src/Attribute/Operation.php @@ -28,4 +28,9 @@ public function toArray(): array ), ]; } + + public function addChannel(Channel $channel): void + { + $this->channels[] = $channel; + } } diff --git a/src/DocumentationStrategy/AttributeDocumentationStrategy.php b/src/DocumentationStrategy/AttributeDocumentationStrategy.php index d3fe12b..931f7e9 100644 --- a/src/DocumentationStrategy/AttributeDocumentationStrategy.php +++ b/src/DocumentationStrategy/AttributeDocumentationStrategy.php @@ -6,6 +6,7 @@ use Ferror\AsyncapiDocBundle\Attribute\Channel; use Ferror\AsyncapiDocBundle\Attribute\Message; +use Ferror\AsyncapiDocBundle\Attribute\Operation; use ReflectionAttribute; use ReflectionClass; use ReflectionException; @@ -36,7 +37,17 @@ public function document(string $class): Message throw new DocumentationStrategyException('Error: class ' . $class . ' must have at least ' . Message::class . ' attribute.'); } + /** @var ReflectionAttribute[] $operationAttributes */ + $operationAttributes = $reflection->getAttributes(Operation::class); + + /** @var ReflectionAttribute[] $channelAttributes */ + $channelAttributes = $reflection->getAttributes(Channel::class); + $message = $messageAttributes[0]->newInstance(); + $operation = $operationAttributes[0]->newInstance(); + $channel = $channelAttributes[0]->newInstance(); + + $operation->addChannel($channel); foreach ($this->propertyExtractor->extract($class) as $property) { $message->addProperty($property); From aa2c967e12f0b486ddafe4ab6eff017770934032 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sat, 3 Feb 2024 23:44:15 +0100 Subject: [PATCH 7/9] feat: operation --- src/Attribute/Message.php | 6 ++++++ .../AttributeDocumentationStrategy.php | 18 ++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Attribute/Message.php b/src/Attribute/Message.php index daf7faf..793ceb6 100644 --- a/src/Attribute/Message.php +++ b/src/Attribute/Message.php @@ -27,6 +27,7 @@ public function toArray(): array 'name' => $this->name, 'properties' => array_map(static fn(PropertyInterface $property) => $property->toArray(), $this->properties), 'channels' => array_map(static fn(Channel $channel) => $channel->toArray(), $this->channels), + 'operations' => array_map(static fn(Operation $operation) => $operation->toArray(), $this->operations), ]; } @@ -40,6 +41,11 @@ public function addChannel(Channel $channel): void $this->channels[] = $channel; } + public function addOperation(Operation $operation): void + { + $this->operations[] = $operation; + } + public function enrich(self $self): self { // UPDATE EXISTING diff --git a/src/DocumentationStrategy/AttributeDocumentationStrategy.php b/src/DocumentationStrategy/AttributeDocumentationStrategy.php index 931f7e9..9978569 100644 --- a/src/DocumentationStrategy/AttributeDocumentationStrategy.php +++ b/src/DocumentationStrategy/AttributeDocumentationStrategy.php @@ -37,17 +37,7 @@ public function document(string $class): Message throw new DocumentationStrategyException('Error: class ' . $class . ' must have at least ' . Message::class . ' attribute.'); } - /** @var ReflectionAttribute[] $operationAttributes */ - $operationAttributes = $reflection->getAttributes(Operation::class); - - /** @var ReflectionAttribute[] $channelAttributes */ - $channelAttributes = $reflection->getAttributes(Channel::class); - $message = $messageAttributes[0]->newInstance(); - $operation = $operationAttributes[0]->newInstance(); - $channel = $channelAttributes[0]->newInstance(); - - $operation->addChannel($channel); foreach ($this->propertyExtractor->extract($class) as $property) { $message->addProperty($property); @@ -61,6 +51,14 @@ public function document(string $class): Message $message->addChannel($channelAttribute->newInstance()); } + // Channels are optional as it's possible to document just Messages. + /** @var ReflectionAttribute[] $operationAttributes */ + $operationAttributes = $reflection->getAttributes(Operation::class); + + foreach ($operationAttributes as $operationAttribute) { + $message->addOperation($operationAttribute->newInstance()); + } + return $message; } } From 4cd77b3b46968bb576d80189cfbdd1358666efae Mon Sep 17 00:00:00 2001 From: Ferror Date: Sat, 4 May 2024 23:49:12 +0200 Subject: [PATCH 8/9] feat: operation --- src/Schema/V3/InfoRenderer.php | 15 +++++++++++---- tests/Unit/DocumentationEditorTest.php | 1 + .../AttributeDocumentationStrategyTest.php | 2 ++ .../ReflectionDocumentationStrategyTest.php | 3 ++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Schema/V3/InfoRenderer.php b/src/Schema/V3/InfoRenderer.php index 9a0c4be..e04b0c5 100644 --- a/src/Schema/V3/InfoRenderer.php +++ b/src/Schema/V3/InfoRenderer.php @@ -6,12 +6,19 @@ final readonly class InfoRenderer { - public function render(array $document): array + public function __construct( + public string $title, + public string $description, + public string $version, + ) { + } + + public function render(): array { return [ - 'title' => $document['title'], - 'version' => $document['version'], - 'description' => $document['description'], + 'title' => $this->title, + 'version' => $this->version, + 'description' => $this->description, ]; } } diff --git a/tests/Unit/DocumentationEditorTest.php b/tests/Unit/DocumentationEditorTest.php index b75b995..c08dfac 100644 --- a/tests/Unit/DocumentationEditorTest.php +++ b/tests/Unit/DocumentationEditorTest.php @@ -43,6 +43,7 @@ public function testDocument(): void ], ], 'channels' => [], + 'operations' => [], ]; $this->assertEquals($expected, $actual); diff --git a/tests/Unit/DocumentationStrategy/AttributeDocumentationStrategyTest.php b/tests/Unit/DocumentationStrategy/AttributeDocumentationStrategyTest.php index 3f723da..6e00d7c 100644 --- a/tests/Unit/DocumentationStrategy/AttributeDocumentationStrategyTest.php +++ b/tests/Unit/DocumentationStrategy/AttributeDocumentationStrategyTest.php @@ -60,6 +60,7 @@ public function testUserSignedUp(): void 'type' => 'subscribe', ], ], + 'operations' => [], ]; $this->assertEquals($expected, $actual); @@ -154,6 +155,7 @@ public function testProductCreated(): void 'type' => 'subscribe', ], ], + 'operations' => [], ]; $this->assertEquals($expected, $actual); diff --git a/tests/Unit/DocumentationStrategy/ReflectionDocumentationStrategyTest.php b/tests/Unit/DocumentationStrategy/ReflectionDocumentationStrategyTest.php index daf47b6..5c18bc2 100644 --- a/tests/Unit/DocumentationStrategy/ReflectionDocumentationStrategyTest.php +++ b/tests/Unit/DocumentationStrategy/ReflectionDocumentationStrategyTest.php @@ -52,6 +52,7 @@ public function test(): void ], ], 'channels' => [], + 'operations' => [], ]; $this->assertEquals($expected, $documentation->document(UserSignedUp::class)->toArray()); @@ -98,9 +99,9 @@ public function testEnum(): void ], ], 'channels' => [], + 'operations' => [], ]; - $actual = $documentation->document(ProductCreated::class)->toArray(); $this->assertEquals($expected, $actual); From 774f7130b830054e41fe09907100174c238e2061 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sun, 5 May 2024 00:03:51 +0200 Subject: [PATCH 9/9] feat: operation --- phpunit.xml | 3 + .../DumpSpecificationConsoleTest.php | 141 +++++++++++++++++- 2 files changed, 143 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 82ce746..1fba752 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,6 +19,9 @@ tests/Unit + + tests/Integration + diff --git a/tests/Integration/DumpSpecificationConsoleTest.php b/tests/Integration/DumpSpecificationConsoleTest.php index aabda68..14f1f9d 100644 --- a/tests/Integration/DumpSpecificationConsoleTest.php +++ b/tests/Integration/DumpSpecificationConsoleTest.php @@ -62,8 +62,12 @@ public function testExecuteClass(): void $this->assertEquals($expectedDisplay, $display); } - public function testExecuteYaml(): void + public function testExecuteYamlV2(): void { + if ($this->isV3()) { + $this->markTestSkipped('Skipping schema V2 test on V3 variable'); + } + $kernel = self::bootKernel(); $application = new Application($kernel); @@ -195,6 +199,141 @@ public function testExecuteYaml(): void } } + public function testExecuteYamlV3(): void + { + if (false === $this->isV3()) { + $this->markTestSkipped('Skipping schema V3 test on V2 variable'); + } + + $kernel = self::bootKernel(); + $application = new Application($kernel); + + $command = $application->find('ferror:asyncapi:dump'); + $commandTester = new CommandTester($command); + $commandTester->execute(['format' => 'yaml']); + + $commandTester->assertCommandIsSuccessful(); + + $display = $commandTester->getDisplay(); + + $expectedDisplay = <<assertEquals($expectedDisplay, $display); + + mkdir(dirname(__DIR__) . '/../var/' . $this->getSchemaVersion()); + + $content = file_put_contents(dirname(__DIR__) . '/../var/' . $this->getSchemaVersion() . '/asyncapi.yaml', $display); + + if (false === $content) { + throw new RuntimeException('Schema file was not save'); + } + } + public function testExecuteJson(): void { $kernel = self::bootKernel();