Skip to content

Commit

Permalink
OperationRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Feb 3, 2024
1 parent 78b1906 commit d81334a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Attribute/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
];
}
}
15 changes: 14 additions & 1 deletion src/Schema/V3/OperationRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
42 changes: 35 additions & 7 deletions tests/Unit/Schema/V3/OperationRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
]
];
Expand All @@ -36,15 +49,30 @@ 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);

$expected = [
'UserSignedUpOperation' => [
'action' => 'receive',
'channel' => [
'$ref' => '#/channels/UserSignedUpChannel'
'$ref' => '#/channels/UserSignedUpChannel',
]
]
];
Expand Down

0 comments on commit d81334a

Please sign in to comment.