Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function it_creates_an_instance_scoped_action_endpoint(): void
$this->assertStringContainsString('Method::'.$this->actionController->route->method->name, $contents);
$this->assertStringContainsString($this->actionController->entity->fqcn->toString(), $contents);
$this->assertStringContainsString(
$this->actionController->entity->name.' $'.$this->actionController->entity->variableName,
$this->actionController->entity->name.' $'.$this->actionController->route->controllerParameterName,
$contents
);
});
Expand Down Expand Up @@ -100,7 +100,7 @@ public function it_creates_a_resource_scoped_action_endpoint(): void
$this->assertStringContainsString('Method::'.$this->resourceActionController->route->method->name, $contents);
$this->assertStringNotContainsString($this->resourceActionController->entity->fqcn->toString(), $contents);
$this->assertStringNotContainsString(
$this->resourceActionController->entity->name.' $'.$this->resourceActionController->entity->variableName,
$this->resourceActionController->entity->name.' $'.$this->resourceActionController->route->controllerParameterName,
$contents
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function it_populates_route_attributes_in_controller(): void
$this->assertStringContainsString('Method::'.$this->showController->route->method->name, $contents);
$this->assertStringContainsString($this->showController->entity->fqcn->toString(), $contents);
$this->assertStringContainsString(
$this->showController->entity->name.' $'.$this->showController->entity->variableName, $contents
$this->showController->entity->name.' $'.$this->showController->route->controllerParameterName, $contents
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function buildControllerParameters(): string
->when(
$this->controller->scope === Scope::Instance,
fn ($parameters) => $parameters
->push($this->controller->entity->name.' $'.$this->controller->entity->variableName)
->push($this->controller->entity->name.' $'.$this->controller->route->controllerParameterName)
)->implode(', ');
}

Expand Down
16 changes: 12 additions & 4 deletions src/Support/Http/Api/References/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ public static function make(Stringable|string $apiVersion, Entity $entity, Endpo
return $route;
}

public Stringable $routeParameterName {
get => $this->entity->name->snake();
}

public Stringable $controllerParameterName {
get => $this->entity->name->camel();
}

public Stringable $routeName {
get {
$base = str('api.')->append($this->apiVersion->lower()->toString())->append('.', $this->entity->plural->lower()->toString());
$base = str('api.')->append($this->apiVersion->lower()->toString())->append('.', $this->entity->plural->kebab()->toString());

return match ($this->endpointType) {
EndpointType::Action => $base->append('.actions.', Str::kebab($this->endpointName->toString())),
Expand All @@ -58,19 +66,19 @@ public static function make(Stringable|string $apiVersion, Entity $entity, Endpo

public Stringable $uri {
get {
$base = str('api/')->append($this->apiVersion->lower()->toString())->append('/', $this->entity->plural->lower()->toString());
$base = str('api/')->append($this->apiVersion->lower()->toString())->append('/', $this->entity->plural->kebab()->toString());

return match ($this->endpointType) {
EndpointType::Action => match ($this->scope) {
Scope::Instance => $base->append(
'/{', $this->entity->variableName->toString(),
'/{', $this->routeParameterName->toString(),
'}/actions/',
Str::kebab($this->endpointName->toString())
),
Scope::Resource => $base->append('/actions/', Str::kebab($this->endpointName->toString())),
},
EndpointType::Rest => match ($this->scope) {
Scope::Instance => $base->append('/{', $this->entity->variableName->toString(), '}'),
Scope::Instance => $base->append('/{', $this->routeParameterName->toString(), '}'),
Scope::Resource => $base,
},
};
Expand Down
40 changes: 40 additions & 0 deletions src/Support/Http/Api/References/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ public function it_resolves_method_for_action(): void
$this->assertSame(Method::Post, $post->method);
}

#[Test]
public function it_resolves_route_name_for_multi_word_entity(): void
{
$route = Route::make(
apiVersion: 'V1',
entity: new Entity(name: 'MarketingKitTemplate', baseNamespace: 'Workbench\\App\\'),
endpointType: EndpointType::Rest,
endpointName: Endpoint::Index->value,
scope: Scope::Resource,
);

$this->assertSame('api.v1.marketing-kit-templates.index', $route->routeName->toString());
}

#[Test]
public function it_resolves_uri_for_multi_word_entity(): void
{
$entity = new Entity(name: 'MarketingKitTemplate', baseNamespace: 'Workbench\\App\\');

$index = Route::make(
apiVersion: 'V1',
entity: $entity,
endpointType: EndpointType::Rest,
endpointName: Endpoint::Index->value,
scope: Scope::Resource,
);

$show = Route::make(
apiVersion: 'V1',
entity: $entity,
endpointType: EndpointType::Rest,
endpointName: Endpoint::Show->value,
);

$this->assertSame('api/v1/marketing-kit-templates', $index->uri->toString());
$this->assertSame('api/v1/marketing-kit-templates/{marketing_kit_template}', $show->uri->toString());
$this->assertSame('marketing_kit_template', $show->routeParameterName->toString());
$this->assertSame('marketingKitTemplate', $show->controllerParameterName->toString());
}

private function makeRoute(EndpointType $endpointType, string $endpointName, ActionMethod $actionMethod = ActionMethod::Post, Scope $scope = Scope::Instance): Route
{
return Route::make(
Expand Down
Loading