Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Feb 3, 2024
1 parent d81334a commit 260be87
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Attribute/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public readonly string $name,
public array $properties = [],
public array $channels = [],
public array $operations = [],
) {
}

Expand Down
5 changes: 5 additions & 0 deletions src/Attribute/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function toArray(): array
),
];
}

public function addChannel(Channel $channel): void
{
$this->channels[] = $channel;
}
}
11 changes: 11 additions & 0 deletions src/DocumentationStrategy/AttributeDocumentationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Operation>[] $operationAttributes */
$operationAttributes = $reflection->getAttributes(Operation::class);

/** @var ReflectionAttribute<Channel>[] $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);
Expand Down
54 changes: 52 additions & 2 deletions src/Schema/V3/SchemaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,63 @@

namespace Ferror\AsyncapiDocBundle\Schema\V3;

use Ferror\AsyncapiDocBundle\ClassFinder\ClassFinderInterface;
use Ferror\AsyncapiDocBundle\DocumentationEditor;
use Ferror\AsyncapiDocBundle\SchemaRendererInterface;
use RuntimeException;

final readonly class SchemaRenderer implements SchemaRendererInterface
{
public function __construct(
private ClassFinderInterface $classFinder,
private DocumentationEditor $documentationEditor,
private InfoRenderer $infoRenderer,
private MessageRenderer $messageRenderer,
private OperationRenderer $operationRenderer,
private ChannelRenderer $channelRenderer,
private array $servers,
private string $schemaVersion,
) {
}

public function generate(): array
{
throw new RuntimeException("Async API V3 not yet supported");
$classes = $this->classFinder->find();

$channels = [];
$messages = [];
$operations = [];

foreach ($classes as $class) {
$document = $this->documentationEditor->document($class);
$document = $document->toArray();

$channel = $this->channelRenderer->render($document);
$message = $this->messageRenderer->render($document);
$operation = $this->operationRenderer->render($document);

$channelKey = key($channel);
$messageKey = key($message);
$operationKey = key($operation);

$channels[$channelKey] = $channel[$channelKey];
$messages[$messageKey] = $message[$messageKey];
$operations[$operationKey] = $operation[$operationKey];
}

$schema = [
'asyncapi' => $this->schemaVersion,
'info' => $this->infoRenderer->render(),

Check failure on line 52 in src/Schema/V3/SchemaRenderer.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Method Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer::render() invoked with 0 parameters, 1 required.

Check failure on line 52 in src/Schema/V3/SchemaRenderer.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Method Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer::render() invoked with 0 parameters, 1 required.

Check failure on line 52 in src/Schema/V3/SchemaRenderer.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies lowest

Method Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer::render() invoked with 0 parameters, 1 required.

Check failure on line 52 in src/Schema/V3/SchemaRenderer.php

View workflow job for this annotation

GitHub Actions / CI - PHP 8.2, Dependencies highest

Method Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer::render() invoked with 0 parameters, 1 required.
'channels' => $channels,
'operations' => $operations,
'components' => [
'messages' => $messages,
],
];

if ($this->servers) {
$schema['servers'] = $this->servers;
}

return $schema;
}
}
78 changes: 76 additions & 2 deletions tests/Unit/Schema/V3/SchemaRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,92 @@

namespace Ferror\AsyncapiDocBundle\Tests\Unit\Schema\V3;

use Ferror\AsyncapiDocBundle\ClassFinder\ManualClassFinder;
use Ferror\AsyncapiDocBundle\DocumentationEditor;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\AttributeDocumentationStrategy;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\PropertyExtractor;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\ReflectionDocumentationStrategy;
use Ferror\AsyncapiDocBundle\Schema\V3\ChannelRenderer;
use Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer;
use Ferror\AsyncapiDocBundle\Schema\V3\MessageRenderer;
use Ferror\AsyncapiDocBundle\Schema\V3\OperationRenderer;
use Ferror\AsyncapiDocBundle\Schema\V3\SchemaRenderer;
use Ferror\AsyncapiDocBundle\Tests\Examples\UserSignedUp;
use PHPUnit\Framework\TestCase;

final class SchemaRendererTest extends TestCase
{
public function testItRenders(): void
{
$renderer = new SchemaRenderer();
$renderer = new SchemaRenderer(
new ManualClassFinder([
UserSignedUp::class,
]),
new DocumentationEditor([
new AttributeDocumentationStrategy(new PropertyExtractor()),
new ReflectionDocumentationStrategy(),
]),
new InfoRenderer(),
new MessageRenderer(),
new OperationRenderer(),
new ChannelRenderer(),
[],
'3.0.0',
);

$actual = $renderer->generate();

$expected = [];
$expected = [
'asyncapi' => '3.0.0',
'info' => [
'title' => 'Account Service',
'version' => '1.0.0',
'description' => 'This service is in charge of processing user signups',
],
'channels' => [
'userSignedUpChannel' => [
'messages' => [
'UserSignedUp' => [
'$ref' => '#/components/messages/UserSignedUp',
],
],
],
],
'operations' => [
'sendUserSignedUpOperation' => [
'action' => 'send',
'channel' => [
'$ref' => '#/channels/userSignedUpChannel',
],
],
'receiveUserSignedUpOperation' => [
'action' => 'receive',
'channel' => [
'$ref' => '#/channels/userSignedUpChannel',
],
],
],
'components' => [
'messages' => [
'UserSignedUp' => [
'payload' => [
'type' => 'object',
'properties' => [
'displayName' => [
'type' => 'string',
'description' => 'Name of the user',
],
'email' => [
'type' => 'string',
'format' => 'email',
'description' => 'Email of the user',
],
],
],
],
],
],
];

$this->assertEquals($expected, $actual);
}
Expand Down

0 comments on commit 260be87

Please sign in to comment.