Skip to content

Commit

Permalink
Fix documentation generation when using binded interfaces instead of …
Browse files Browse the repository at this point in the history
…model classes as typed arguments
  • Loading branch information
toyi committed Jul 9, 2022
1 parent a8fbe1c commit ef0e024
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
24 changes: 18 additions & 6 deletions camel/Extraction/ExtractedEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,21 @@ public function forSerialisation()
return $copy;
}

public static function getFieldBindingForUrlParam(Route $route, string $paramName, array $typeHintedArguments = [],
protected static function instantiateTypedArgument(\ReflectionNamedType $argumentType): ?object
{
$argumentInstance = null;
$argumentClassName = $argumentType->getName();

if (class_exists($argumentClassName)) {
$argumentInstance = new $argumentClassName;
} else if (interface_exists($argumentClassName)) {
$argumentInstance = app($argumentClassName);
}

return $argumentInstance;
}

public static function getFieldBindingForUrlParam(Route $route, string $paramName, array $typeHintedArguments = [],
string $default = null): ?string
{
$binding = null;
Expand All @@ -222,9 +236,8 @@ public static function getFieldBindingForUrlParam(Route $route, string $paramNam
// Search for a type-hinted variable whose name matches the route segment name
if (is_null($binding) && array_key_exists($paramName, $typeHintedArguments)) {
$argumentType = $typeHintedArguments[$paramName]->getType();
$argumentClassName = $argumentType->getName();
$argumentInstance = new $argumentClassName;
$binding = $argumentInstance->getRouteKeyName();
$argumentInstance = self::instantiateTypedArgument($argumentType);
$binding = $argumentInstance instanceof Model ? $argumentInstance->getRouteKeyName() : null;
}

return $binding ?: $default;
Expand Down Expand Up @@ -258,8 +271,7 @@ protected function argumentHasModelType(\ReflectionParameter $argument): bool
// The argument does not have a type-hint
return false;
} else {
$argumentClassName = $argumentType->getName();
$argumentInstance = new $argumentClassName;
$argumentInstance = self::instantiateTypedArgument($argumentType);
return ($argumentInstance instanceof Model);
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/TestPostBindedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

interface TestPostBindedInterface
{
}
10 changes: 10 additions & 0 deletions tests/Fixtures/TestPostBindedInterfaceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Knuckles\Scribe\Tests\Fixtures;

class TestPostBindedInterfaceController
{
public function update(TestPostBindedInterface $post)
{
}
}
20 changes: 20 additions & 0 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
use Knuckles\Scribe\Tests\Fixtures\TestPost;
use Knuckles\Scribe\Tests\Fixtures\TestPostBindedInterface;
use Knuckles\Scribe\Tests\Fixtures\TestPostController;
use Knuckles\Scribe\Tests\Fixtures\TestPostBindedInterfaceController;
use Knuckles\Scribe\Tests\Fixtures\TestPostUserController;
use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
use Knuckles\Scribe\Tests\Fixtures\TestUser;
Expand Down Expand Up @@ -433,6 +435,24 @@ public function generates_correct_url_params_from_resource_routes_and_model_bind
$this->assertEquals('posts/{post_slug}/users/{id}', $group['endpoints'][1]['uri']);
}

/** @test */
public function generates_correct_url_params_from_resource_routes_and_model_binding_with_binded_interfaces()
{
$this->app->bind(TestPostBindedInterface::class, function(){
return new TestPost();
});

RouteFacade::resource('posts', TestPostBindedInterfaceController::class)->only('update');

config(['scribe.routes.0.match.prefixes' => ['*']]);
config(['scribe.routes.0.apply.response_calls.methods' => []]);

$this->artisan('scribe:generate');

$group = Yaml::parseFile('.scribe/endpoints/00.yaml');
$this->assertEquals('posts/{slug}', $group['endpoints'][0]['uri']);
}

/** @test */
public function generates_correct_url_params_from_non_resource_routes_and_model_binding()
{
Expand Down

0 comments on commit ef0e024

Please sign in to comment.