Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom endpoint metadata definitions #381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions camel/Extraction/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Knuckles\Camel\Extraction;


use Knuckles\Camel\BaseDTO;

class Metadata extends BaseDTO
Expand All @@ -28,4 +27,6 @@ class Metadata extends BaseDTO
public ?string $description;

public bool $authenticated = false;
}

public array $custom = [];
}
15 changes: 15 additions & 0 deletions tests/Fixtures/TestCustomEndpointMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Extracting\Strategies\Strategy;

class TestCustomEndpointMetadata extends Strategy
{
public function __invoke(ExtractedEndpointData $endpointData, array $routeRules): ?array
{
$endpointData->metadata->custom['myProperty'] = 'some custom metadata';
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this matters, does it? Functions in PHP implicitly return null.

Oh, this is because of the type annotation requiring a return statement.

}
}
9 changes: 9 additions & 0 deletions tests/Unit/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ExtractorTest extends TestCase
'strategies' => [
'metadata' => [
\Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
\Knuckles\Scribe\Tests\Fixtures\TestCustomEndpointMetadata::class,
],
'urlParameters' => [
\Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLaravelAPI::class,
Expand Down Expand Up @@ -264,6 +265,14 @@ public function can_use_closure_in_routes_uses()
$this->assertSame('Name of the location', $parsed->bodyParameters['name']->description);
}

/** @test */
public function endpoint_metadata_supports_custom_declarations()
{
$route = $this->createRoute('POST', '/api/test', 'dummy');
$parsed = $this->generator->processRoute($route);
$this->assertSame('some custom metadata', $parsed->metadata->custom['myProperty']);
}

public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
{
return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
Expand Down