Skip to content

Commit

Permalink
feat: channel and operation attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Jan 25, 2024
1 parent e60812b commit 1f9f029
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/Attribute/Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Attribute;

use Attribute;
use Ferror\AsyncapiDocBundle\Schema\V2\ChannelType;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class Channel
{
public function __construct(
public string $name,
public ChannelType $type = ChannelType::SUBSCRIBE,
) {
}
}
11 changes: 3 additions & 8 deletions src/Attribute/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,24 @@
namespace Ferror\AsyncapiDocBundle\Attribute;

use Attribute;
use Ferror\AsyncapiDocBundle\Schema\V2\ChannelType;

#[Attribute(Attribute::TARGET_CLASS)]
class Message implements PropertyInterface
readonly class Message implements PropertyInterface
{
/**
* @param PropertyInterface[] $properties
*/
public function __construct(
public readonly string $name,
public readonly string $channel,
public readonly array $properties = [],
public readonly ChannelType $channelType = ChannelType::SUBSCRIBE,
public string $name,
public array $properties = [],
) {
}

public function toArray(): array
{
return [
'name' => $this->name,
'channel' => $this->channel,
'properties' => array_map(static fn(PropertyInterface $property) => $property->toArray(), $this->properties),
'channelType' => $this->channelType->value,
];
}
}
18 changes: 18 additions & 0 deletions src/Attribute/Operation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Attribute;

use Attribute;
use Ferror\AsyncapiDocBundle\Schema\V3\OperationType;

#[Attribute(Attribute::TARGET_CLASS)]
class Operation
{
public function __construct(
public string $name,
public OperationType $type = OperationType::SEND,
) {
}
}
15 changes: 4 additions & 11 deletions src/Schema/V3/InfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@

final readonly class InfoRenderer
{
public function __construct(
public string $title,
public string $description,
public string $version,
) {
}

public function render(): array
public function render(array $document): array
{
return [
'title' => $this->title,
'version' => $this->version,
'description' => $this->description,
'title' => $document['title'],
'version' => $document['version'],
'description' => $document['description'],
];
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Schema/V3/InfoRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Ferror\AsyncapiDocBundle\Schema\V3\InfoRenderer;
use PHPUnit\Framework\TestCase;

final class InfoRendererTest extends TestCase
{
public function testItRenders(): void
{
$renderer = new InfoRenderer();

$document = [];

$actual = $renderer->render($document);

$expected = [
'version' => '2.6.0',
'title' => 'Async API Title',
'description' => 'Async API Description',
];

$this->assertEquals($expected, $actual);
}
}
148 changes: 148 additions & 0 deletions tests/Unit/Schema/V3/MessageRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

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

use Ferror\AsyncapiDocBundle\Schema\V3\MessageRenderer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

class MessageRendererTest extends TestCase
{
public function testReflection(): void
{
$document = [
'name' => 'UserSignedUp',
'properties' => [
[
'name' => 'name',
'type' => 'string',
'required' => true,
],
[
'name' => 'email',
'type' => 'string',
'required' => true,
],
[
'name' => 'age',
'type' => 'int',
'required' => true,
],
[
'name' => 'isCitizen',
'type' => 'bool',
'required' => true,
],
],
];

$schema = new MessageRenderer();

$specification = $schema->render($document);

$expectedSpecification = <<<YAML
UserSignedUp:
payload:
type: object
properties:
name:
type: string
email:
type: string
age:
type: integer
isCitizen:
type: boolean
required:
- name
- email
- age
- isCitizen
YAML;

$this->assertEquals($expectedSpecification, Yaml::dump($specification, 10, 2));
}

public function testAttributes(): void
{
$document = [
'name' => 'UserSignedUp',
'properties' => [
[
'name' => 'name',
'type' => 'string',
'description' => 'Name of the user',
'example' => 'John',
'format' => 'string',
'required' => true,
],
[
'name' => 'email',
'type' => 'string',
'description' => 'Email of the user',
'format' => 'email',
'example' => '[email protected]',
'required' => true,
],
[
'name' => 'age',
'type' => 'integer',
'description' => 'Age of the user',
'format' => 'int',
'example' => '18',
'required' => true,
],
[
'name' => 'isCitizen',
'type' => 'boolean',
'description' => 'Is user a citizen',
'format' => 'boolean',
'example' => 'true',
'required' => true,
],
],
];

$schema = new MessageRenderer();

$specification = $schema->render($document);

$expectedSpecification = <<<YAML
UserSignedUp:
payload:
type: object
properties:
name:
type: string
description: 'Name of the user'
format: string
example: John
email:
type: string
description: 'Email of the user'
format: email
example: [email protected]
age:
type: integer
description: 'Age of the user'
format: int
example: '18'
isCitizen:
type: boolean
description: 'Is user a citizen'
format: boolean
example: 'true'
required:
- name
- email
- age
- isCitizen
YAML;

$this->assertEquals($expectedSpecification, Yaml::dump($specification, 10, 2));
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Schema/V3/SchemaRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

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

use Ferror\AsyncapiDocBundle\Schema\V3\SchemaRenderer;
use PHPUnit\Framework\TestCase;

final class SchemaRendererTest extends TestCase
{
public function testItRenders(): void
{
$renderer = new SchemaRenderer();

$actual = $renderer->generate();

$expected = [];

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

0 comments on commit 1f9f029

Please sign in to comment.