From 8fe91d86e63e227873d7d37ad1677f622d9b7ef8 Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 8 Jan 2023 12:37:10 +0100 Subject: [PATCH] Expose default strategy for URL normalization --- camel/Extraction/ExtractedEndpointData.php | 5 +++-- tests/Unit/ExtractedEndpointDataTest.php | 25 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/camel/Extraction/ExtractedEndpointData.php b/camel/Extraction/ExtractedEndpointData.php index 422dbc97..3f6b295a 100644 --- a/camel/Extraction/ExtractedEndpointData.php +++ b/camel/Extraction/ExtractedEndpointData.php @@ -87,10 +87,11 @@ public function __construct(array $parameters = []) parent::__construct($parameters); + $defaultNormalizer = fn() => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method); $this->uri = match (is_callable(Globals::$__normalizeEndpointUrlUsing)) { true => call_user_func_array(Globals::$__normalizeEndpointUrlUsing, - [$this->route->uri, $this->route, $this->method, $this->controller]), - default => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method), + [$this->route->uri, $this->route, $this->method, $this->controller, $defaultNormalizer]), + default => $defaultNormalizer(), }; } diff --git a/tests/Unit/ExtractedEndpointDataTest.php b/tests/Unit/ExtractedEndpointDataTest.php index 5d63a511..b61a3fed 100644 --- a/tests/Unit/ExtractedEndpointDataTest.php +++ b/tests/Unit/ExtractedEndpointDataTest.php @@ -32,7 +32,7 @@ public function normalizes_resource_url_params() /** @test */ public function allows_user_specified_normalization() { - Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route, \ReflectionFunctionAbstract $method, ?\ReflectionClass $controller) { + Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route) { if ($url == 'things/{thing}') return 'things/{the_id_of_the_thing}'; if ($route->named('things.otherthings.destroy')) return 'things/{thing-id}/otherthings/{other_thing-id}'; @@ -51,6 +51,29 @@ public function allows_user_specified_normalization() Scribe::normalizeEndpointUrlUsing(null); } + /** @test */ + public function allows_user_specified_normalization_fallback_to_default() + { + Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route, + \ReflectionFunctionAbstract $method, ?\ReflectionClass $controller, callable $default) { + if ($route->named('things.otherthings.destroy')) return 'things/{thing-id}/otherthings/{other_thing-id}'; + + return $default(); + }); + + Route::apiResource('things', TestController::class)->only('show'); + $route = $this->getRoute(['prefixes' => '*']); + $this->assertEquals('things/{thing}', $this->originalUri($route)); + $this->assertEquals('things/{id}', $this->expectedUri($route)); + + Route::apiResource('things.otherthings', TestController::class)->only('destroy'); + $route = $this->getRoute(['prefixes' => '*/otherthings/*']); + $this->assertEquals('things/{thing}/otherthings/{otherthing}', $this->originalUri($route)); + $this->assertEquals('things/{thing-id}/otherthings/{other_thing-id}', $this->expectedUri($route)); + + Scribe::normalizeEndpointUrlUsing(null); + } + /** @test */ public function normalizes_resource_url_params_from_underscores_to_hyphens() {