ExtractedEndpointDataTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if (version_compare($this->app->version(), '7.0.0', '<')) {
  35. $this->markTestSkipped("Field binding syntax was introduced in Laravel 7.");
  36. return;
  37. }
  38. Route::get('things/{thing:slug}', [TestController::class, 'show']);
  39. $route = $this->getRoute(['prefixes' => '*']);
  40. $this->assertEquals('things/{thing}', $this->originalUri($route));
  41. $this->assertEquals('things/{thing_slug}', $this->expectedUri($route));
  42. }
  43. protected function expectedUri(LaravelRoute $route): string
  44. {
  45. return $this->endpoint($route)->uri;
  46. }
  47. protected function originalUri(LaravelRoute $route): string
  48. {
  49. return $route->uri;
  50. }
  51. protected function endpoint(LaravelRoute $route): ExtractedEndpointData
  52. {
  53. return new ExtractedEndpointData([
  54. 'route' => $route,
  55. 'uri' => $route->uri,
  56. 'httpMethods' => $route->methods,
  57. 'method' => new \ReflectionFunction('dump'), // Just so we don't have null
  58. ]);
  59. }
  60. protected function getRoute(array $matchRules): LaravelRoute
  61. {
  62. $routeRules[0]['match'] = array_merge($matchRules, ['domains' => '*']);
  63. $matchedRoutes = (new RouteMatcher)->getRoutes($routeRules);
  64. $this->assertCount(1, $matchedRoutes);
  65. return $matchedRoutes[0]->getRoute();
  66. }
  67. }