Skip to content

Commit

Permalink
OperationRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Feb 2, 2024
1 parent a08ea65 commit 88f3529
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/Attribute/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public function __construct(
public ChannelType $type = ChannelType::SUBSCRIBE,
) {
}

public function toArray(): array
{
return [
'name' => $this->name,
'type' => $this->type->value,
];
}
}
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;
}
}
2 changes: 1 addition & 1 deletion tests/Examples/UserSignedUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* This class represents a SIMPLE example of documenting by AttributeStrategy.
*/
#[Message(name: 'UserSignedUp')]
#[Operation(name: 'user_signed_up', channels: [new Channel('user_signed_up')])]
#[Operation(name: 'UserSignedUpOperation', channels: [new Channel('UserSignedUpChannel')])]
final readonly class UserSignedUp
{
public function __construct(
Expand Down
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 88f3529

Please sign in to comment.