ExtractedEndpointDataTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Support\Facades\Route;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Matching\RouteMatcher;
  6. use Knuckles\Scribe\Tests\BaseLaravelTest;
  7. use Knuckles\Scribe\Tests\Fixtures\TestController;
  8. class ExtractedEndpointDataTest extends BaseLaravelTest
  9. {
  10. /** @test */
  11. public function will_normalize_resource_url_params()
  12. {
  13. Route::apiResource('things', TestController::class)
  14. ->only('show');
  15. $routeRules[0]['match'] = ['prefixes' => '*', 'domains' => '*'];
  16. $matcher = new RouteMatcher();
  17. $matchedRoutes = $matcher->getRoutes($routeRules);
  18. foreach ($matchedRoutes as $matchedRoute) {
  19. $route = $matchedRoute->getRoute();
  20. $this->assertEquals('things/{thing}', $route->uri);
  21. $endpoint = new ExtractedEndpointData([
  22. 'route' => $route,
  23. 'uri' => $route->uri,
  24. 'httpMethods' => $route->methods,
  25. ]);
  26. $this->assertEquals('things/{id}', $endpoint->uri);
  27. }
  28. Route::apiResource('things.otherthings', TestController::class)
  29. ->only( 'destroy');
  30. $routeRules[0]['match'] = ['prefixes' => '*/otherthings/*', 'domains' => '*'];
  31. $matchedRoutes = $matcher->getRoutes($routeRules);
  32. foreach ($matchedRoutes as $matchedRoute) {
  33. $route = $matchedRoute->getRoute();
  34. $this->assertEquals('things/{thing}/otherthings/{otherthing}', $route->uri);
  35. $endpoint = new ExtractedEndpointData([
  36. 'route' => $route,
  37. 'uri' => $route->uri,
  38. 'httpMethods' => $route->methods,
  39. ]);
  40. $this->assertEquals('things/{thing_id}/otherthings/{id}', $endpoint->uri);
  41. }
  42. }
  43. /** @test */
  44. public function will_normalize_resource_url_params_with_hyphens()
  45. {
  46. Route::apiResource('audio-things', TestController::class)
  47. ->only('show');
  48. $routeRules[0]['match'] = ['prefixes' => '*', 'domains' => '*'];
  49. $matcher = new RouteMatcher();
  50. $matchedRoutes = $matcher->getRoutes($routeRules);
  51. foreach ($matchedRoutes as $matchedRoute) {
  52. $route = $matchedRoute->getRoute();
  53. $this->assertEquals('audio-things/{audio_thing}', $route->uri);
  54. $endpoint = new ExtractedEndpointData([
  55. 'route' => $route,
  56. 'uri' => $route->uri,
  57. 'httpMethods' => $route->methods,
  58. ]);
  59. $this->assertEquals('audio-things/{id}', $endpoint->uri);
  60. }
  61. }
  62. }