Skip to content

Commit

Permalink
chore: enriching
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Jan 29, 2024
1 parent 4dc5776 commit 93fabf4
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/Attribute/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Message
{
/**
* @param PropertyInterface[] $properties
* @param array<Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject> $properties
*/
public function __construct(
public readonly string $name,
Expand All @@ -31,7 +31,7 @@ public function toArray(): array
];
}

public function addProperty(PropertyInterface $property): void
public function addProperty(Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject $property): void
{
$this->properties[] = $property;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DocumentationEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function document(string $class): Message
$strategies[$documentationStrategy::getDefaultPriority()] = $documentationStrategy;
}

$firstStrategy = array_pop($strategies);
$firstStrategy = array_shift($strategies);

$documentedMessage = $firstStrategy->document($class);

Expand Down
2 changes: 1 addition & 1 deletion src/DocumentationStrategy/PropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class PropertyExtractor
{
/**
* @return iterable<PropertyInterface>
* @return iterable<Property|PropertyArray|PropertyEnum|PropertyObject|PropertyArrayObject>
*
* @throws ReflectionException
*/
Expand Down
26 changes: 19 additions & 7 deletions src/DocumentationStrategy/ReflectionDocumentationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,25 @@ public function document(string $class): Message
$type = $property->getType();
$name = $property->getName();

$message->addProperty(
new Property(
name: $name,
type: PropertyType::fromNative($type?->getName()),
required: $type && !$type->allowsNull(),
)
);
if (null === $type) {
break;
}

// ATM we don't support array types in ReflectionStrategy
if (in_array($type->getName(), ['array', 'object', 'resource'])) {
break;
}

// ATM we support only builtin types like integer, string, boolean, float
if ($type->isBuiltin()) {
$message->addProperty(
new Property(
name: $name,
type: PropertyType::fromNative($type->getName()),
required: !$type->allowsNull(),
)
);
}
}

return $message;
Expand Down
7 changes: 3 additions & 4 deletions tests/Examples/PaymentExecuted.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Ferror\AsyncapiDocBundle\Tests\Examples;

use Ferror\AsyncapiDocBundle\Attribute\Message;
use Ferror\AsyncapiDocBundle\Attribute\Property;
use Ferror\AsyncapiDocBundle\Schema\Format;
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

/**
* This class represents an example of documenting by ReflectionStrategy
*/
#[Message(name: 'PaymentExecuted', channel: 'payment_executed')]
final readonly class PaymentExecuted
{
public function __construct(
public float $amount,
#[Property(name: 'createdAt', type: PropertyType::STRING, description: 'Creation date', format: Format::DATETIME, example: '2023-11-23 13:41:21')]
public string $createdAt,
) {
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Examples/ProductCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Ferror\AsyncapiDocBundle\Schema\Format;
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

/**
* This class represents an example of documenting by AttributeStrategy. It contains all types.
*/
#[Message(name: 'ProductCreated', channel: 'product.created')]
final readonly class ProductCreated
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Examples/ProductUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Ferror\AsyncapiDocBundle\Schema\Format;
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

/**
* This class represents an example of documenting by AttributeStrategy. The properties are provided via objects.
*/
#[Message(
name: 'ProductUpdated',
channel: 'product.updated',
Expand Down
3 changes: 3 additions & 0 deletions tests/Examples/UserSignedUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Ferror\AsyncapiDocBundle\Schema\Format;
use Ferror\AsyncapiDocBundle\Schema\PropertyType;

/**
* This class represents a SIMPLE example of documenting by AttributeStrategy.
*/
#[Message(name: 'UserSignedUp', channel: 'user_signed_up')]
final readonly class UserSignedUp
{
Expand Down
58 changes: 23 additions & 35 deletions tests/Integration/DumpSpecificationConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,8 @@ public function testExecuteYaml(): void
properties:
amount:
type: number
description: 'Payment amount'
format: float
example: '1000'
createdAt:
type: string
description: 'Creation date'
format: date-time
example: '2023-11-23 13:41:21'
required:
- amount
- createdAt
Expand Down Expand Up @@ -284,35 +278,29 @@ public function testExecuteJson(): void
"example": "true"
}
},
"required": [
"name",
"email",
"age",
"isCitizen"
]
"required": [
"name",
"email",
"age",
"isCitizen"
]
}
},
"PaymentExecuted": {
"payload": {
"type": "object",
"properties": {
"amount": {
"type": "number",
"description": "Payment amount",
"format": "float",
"example": "1000"
"type": "number"
},
"createdAt": {
"type": "string",
"description": "Creation date",
"format": "date-time",
"example": "2023-11-23 13:41:21"
"type": "string"
}
},
"required": [
"amount",
"createdAt"
]
"required": [
"amount",
"createdAt"
]
}
},
"ProductCreated": {
Expand Down Expand Up @@ -348,17 +336,17 @@ public function testExecuteJson(): void
"type": "string"
}
},
"required": [
"id",
"amount",
"currency",
"isPaid",
"createdAt",
"week",
"payment",
"products",
"tags"
]
"required": [
"id",
"amount",
"currency",
"isPaid",
"createdAt",
"week",
"payment",
"products",
"tags"
]
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions tests/Unit/DocumentationEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@ public function testDocument(): void
$actual = $documentationEditor->document(PaymentExecuted::class)->toArray();

$expected = [
'name' => "PaymentExecuted",
'channel' => "payment_executed",
"channelType" => "subscribe",
'name' => 'PaymentExecuted',
'channel' => 'payment_executed',
'channelType' => 'subscribe',
'properties' => [
[
"name" => "amount",
"required" => true,
"type" => "float",
'name' => 'amount',
'required' => true,
'type' => 'number',
'description' => '',
'format' => null,
'example' => null,
],
[
"name" => "createdAt",
"description" => "Creation date",
"required" => true,
"type" => "string",
"format" => "date-time",
"example" => "2023-11-23 13:41:21",
'name' => 'createdAt',
'description' => '',
'required' => true,
'type' => 'string',
'format' => null,
'example' => null,
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ferror\AsyncapiDocBundle\Tests\Unit\DocumentationStrategy;

use Ferror\AsyncapiDocBundle\DocumentationStrategy\ReflectionDocumentationStrategy;
use Ferror\AsyncapiDocBundle\Tests\Examples\ProductCreated;
use Ferror\AsyncapiDocBundle\Tests\Examples\UserSignedUp;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -56,4 +57,54 @@ public function test(): void

$this->assertEquals($expected, $documentation->document(UserSignedUp::class)->toArray());
}

public function testEnum(): void
{
$documentation = new ReflectionDocumentationStrategy();

$expected = [
'name' => 'ProductCreated',
'channel' => 'product.created',
'channelType' => 'subscribe',
'properties' => [
[
'name' => 'id',
'description' => '',
'required' => true,
'type' => 'integer',
'format' => null,
'example' => null
],
[
'name' => 'amount',
'description' => '',
'required' => true,
'type' => 'number',
'format' => null,
'example' => null
],
[
'name' => 'currency',
'description' => '',
'required' => true,
'type' => 'string',
'format' => null,
'example' => null
],
[
'name' => 'isPaid',
'description' => '',
'required' => true,
'type' => 'boolean',
'format' => null,
'example' => null
],
],
];


$actual = $documentation->document(ProductCreated::class)->toArray();

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

0 comments on commit 93fabf4

Please sign in to comment.