瀏覽代碼

Expose default strategy for URL normalization

shalvah 2 年之前
父節點
當前提交
8fe91d86e6
共有 2 個文件被更改,包括 27 次插入3 次删除
  1. 3 2
      camel/Extraction/ExtractedEndpointData.php
  2. 24 1
      tests/Unit/ExtractedEndpointDataTest.php

+ 3 - 2
camel/Extraction/ExtractedEndpointData.php

@@ -87,10 +87,11 @@ class ExtractedEndpointData extends BaseDTO
 
         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(),
         };
     }
 

+ 24 - 1
tests/Unit/ExtractedEndpointDataTest.php

@@ -32,7 +32,7 @@ class ExtractedEndpointDataTest extends BaseLaravelTest
     /** @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 @@ class ExtractedEndpointDataTest extends BaseLaravelTest
         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()
     {