ExtractedEndpointDataTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  49. /** @test */
  50. public function normalizes_nonresource_url_params_with_inline_bindings()
  51. {
  52. Route::get('things/{thing:slug}', [TestController::class, 'show']);
  53. $route = $this->getRoute(['prefixes' => '*']);
  54. $this->assertEquals('things/{thing}', $this->originalUri($route));
  55. $this->assertEquals('things/{thing_slug}', $this->expectedUri($route));
  56. }
  57. protected function expectedUri(LaravelRoute $route): string
  58. {
  59. return $this->endpoint($route)->uri;
  60. }
  61. protected function originalUri(LaravelRoute $route): string
  62. {
  63. return $route->uri;
  64. }
  65. protected function endpoint(LaravelRoute $route): ExtractedEndpointData
  66. {
  67. return new ExtractedEndpointData([
  68. 'route' => $route,
  69. 'uri' => $route->uri,
  70. 'httpMethods' => $route->methods,
  71. 'method' => new \ReflectionFunction('dump'), // Just so we don't have null
  72. ]);
  73. }
  74. protected function getRoute(array $matchRules): LaravelRoute
  75. {
  76. $routeRules[0]['match'] = array_merge($matchRules, ['domains' => '*']);
  77. $matchedRoutes = (new RouteMatcher)->getRoutes($routeRules);
  78. $this->assertCount(1, $matchedRoutes);
  79. return $matchedRoutes[0]->getRoute();
  80. }
  81. }