ExtractedEndpointDataTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Tests\BaseLaravelTest;
  8. use Knuckles\Scribe\Tests\Fixtures\TestController;
  9. class ExtractedEndpointDataTest extends BaseLaravelTest
  10. {
  11. /** @test */
  12. public function normalizes_resource_url_params()
  13. {
  14. Route::apiResource('things', TestController::class)->only('show');
  15. $route = $this->getRoute(['prefixes' => '*']);
  16. $this->assertEquals('things/{thing}', $this->originalUri($route));
  17. $this->assertEquals('things/{id}', $this->expectedUri($route));
  18. Route::apiResource('things.otherthings', TestController::class)->only('destroy');
  19. $route = $this->getRoute(['prefixes' => '*/otherthings/*']);
  20. $this->assertEquals('things/{thing}/otherthings/{otherthing}', $this->originalUri($route));
  21. $this->assertEquals('things/{thing_id}/otherthings/{id}', $this->expectedUri($route));
  22. }
  23. /** @test */
  24. public function normalizes_resource_url_params_from_underscores_to_hyphens()
  25. {
  26. Route::apiResource('audio-things', TestController::class)->only('show');
  27. $route = $this->getRoute(['prefixes' => '*']);
  28. $this->assertEquals('audio-things/{audio_thing}', $this->originalUri($route));
  29. $this->assertEquals('audio-things/{id}', $this->expectedUri($route));
  30. }
  31. /** @test */
  32. public function normalizes_nonresource_url_params_with_inline_bindings()
  33. {
  34. Route::get('things/{thing:slug}', [TestController::class, 'show']);
  35. $route = $this->getRoute(['prefixes' => '*']);
  36. $this->assertEquals('things/{thing}', $this->originalUri($route));
  37. $this->assertEquals('things/{thing_slug}', $this->expectedUri($route));
  38. }
  39. protected function expectedUri(LaravelRoute $route): string
  40. {
  41. return $this->endpoint($route)->uri;
  42. }
  43. protected function originalUri(LaravelRoute $route): string
  44. {
  45. return $route->uri;
  46. }
  47. protected function endpoint(LaravelRoute $route): ExtractedEndpointData
  48. {
  49. return new ExtractedEndpointData([
  50. 'route' => $route,
  51. 'uri' => $route->uri,
  52. 'httpMethods' => $route->methods,
  53. 'method' => new \ReflectionFunction('dump'), // Just so we don't have null
  54. ]);
  55. }
  56. protected function getRoute(array $matchRules): LaravelRoute
  57. {
  58. $routeRules[0]['match'] = array_merge($matchRules, ['domains' => '*']);
  59. $matchedRoutes = (new RouteMatcher)->getRoutes($routeRules);
  60. $this->assertCount(1, $matchedRoutes);
  61. return $matchedRoutes[0]->getRoute();
  62. }
  63. }