ExtractedEndpointDataTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Routing\Route as LaravelRoute;
  4. use Illuminate\Support\Facades\Route;
  5. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  6. use Knuckles\Scribe\Matching\RouteMatcher;
  7. use Knuckles\Scribe\Scribe;
  8. use Knuckles\Scribe\Tests\BaseLaravelTest;
  9. use Knuckles\Scribe\Tests\Fixtures\TestController;
  10. class ExtractedEndpointDataTest extends BaseLaravelTest
  11. {
  12. /** @test */
  13. public function normalizes_resource_url_params()
  14. {
  15. Route::apiResource('things', TestController::class)->only('show');
  16. $route = $this->getRoute(['prefixes' => '*']);
  17. $this->assertEquals('things/{thing}', $this->originalUri($route));
  18. $this->assertEquals('things/{id}', $this->expectedUri($route));
  19. Route::apiResource('things.otherthings', TestController::class)->only('destroy');
  20. $route = $this->getRoute(['prefixes' => '*/otherthings/*']);
  21. $this->assertEquals('things/{thing}/otherthings/{otherthing}', $this->originalUri($route));
  22. $this->assertEquals('things/{thing_id}/otherthings/{id}', $this->expectedUri($route));
  23. }
  24. /** @test */
  25. public function allows_user_specified_normalization()
  26. {
  27. Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route, \ReflectionFunctionAbstract $method, ?\ReflectionClass $controller) {
  28. if ($url == 'things/{thing}') return 'things/{the_id_of_the_thing}';
  29. if ($route->named('things.otherthings.destroy')) return 'things/{thing-id}/otherthings/{other_thing-id}';
  30. });
  31. Route::apiResource('things', TestController::class)->only('show');
  32. $route = $this->getRoute(['prefixes' => '*']);
  33. $this->assertEquals('things/{thing}', $this->originalUri($route));
  34. $this->assertEquals('things/{the_id_of_the_thing}', $this->expectedUri($route));
  35. Route::apiResource('things.otherthings', TestController::class)->only('destroy');
  36. $route = $this->getRoute(['prefixes' => '*/otherthings/*']);
  37. $this->assertEquals('things/{thing}/otherthings/{otherthing}', $this->originalUri($route));
  38. $this->assertEquals('things/{thing-id}/otherthings/{other_thing-id}', $this->expectedUri($route));
  39. Scribe::normalizeEndpointUrlUsing(null);
  40. }
  41. /** @test */
  42. public function normalizes_resource_url_params_from_underscores_to_hyphens()
  43. {
  44. Route::apiResource('audio-things', TestController::class)->only('show');
  45. $route = $this->getRoute(['prefixes' => '*']);
  46. $this->assertEquals('audio-things/{audio_thing}', $this->originalUri($route));
  47. $this->assertEquals('audio-things/{id}', $this->expectedUri($route));
  48. Route::apiResource('big-users.audio-things.things', TestController::class)->only('store');
  49. $route = $this->getRoute(['prefixes' => '*big-users*']);
  50. $this->assertEquals('big-users/{big_user}/audio-things/{audio_thing}/things', $this->originalUri($route));
  51. $this->assertEquals('big-users/{big_user_id}/audio-things/{audio_thing_id}/things', $this->expectedUri($route));
  52. }
  53. /** @test */
  54. public function normalizes_nonresource_url_params_with_inline_bindings()
  55. {
  56. Route::get('things/{thing:slug}', [TestController::class, 'show']);
  57. $route = $this->getRoute(['prefixes' => '*']);
  58. $this->assertEquals('things/{thing}', $this->originalUri($route));
  59. $this->assertEquals('things/{thing_slug}', $this->expectedUri($route));
  60. }
  61. protected function expectedUri(LaravelRoute $route): string
  62. {
  63. return $this->endpoint($route)->uri;
  64. }
  65. protected function originalUri(LaravelRoute $route): string
  66. {
  67. return $route->uri;
  68. }
  69. protected function endpoint(LaravelRoute $route): ExtractedEndpointData
  70. {
  71. return new ExtractedEndpointData([
  72. 'route' => $route,
  73. 'uri' => $route->uri,
  74. 'httpMethods' => $route->methods,
  75. 'method' => new \ReflectionFunction('dump'), // Just so we don't have null
  76. ]);
  77. }
  78. protected function getRoute(array $matchRules): LaravelRoute
  79. {
  80. $routeRules[0]['match'] = array_merge($matchRules, ['domains' => '*']);
  81. $matchedRoutes = (new RouteMatcher)->getRoutes($routeRules);
  82. $this->assertCount(1, $matchedRoutes);
  83. return $matchedRoutes[0]->getRoute();
  84. }
  85. }