RouteMatcherTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Dingo\Api\Routing\Router;
  4. use Orchestra\Testbench\TestCase;
  5. use Mpociot\ApiDoc\Tools\RouteMatcher;
  6. use Illuminate\Support\Facades\Route as RouteFacade;
  7. class RouteMatcherTest extends TestCase
  8. {
  9. /**
  10. * @var RouteMatcher
  11. */
  12. private $matcher;
  13. protected function setUp()
  14. {
  15. parent::setUp();
  16. $this->matcher = new RouteMatcher();
  17. }
  18. protected function getPackageProviders($app)
  19. {
  20. return [
  21. \Dingo\Api\Provider\LaravelServiceProvider::class,
  22. ];
  23. }
  24. public function testRespectsDomainsRuleForLaravelRouter()
  25. {
  26. $this->registerLaravelRoutes();
  27. $routeRules[0]['match']['prefixes'] = ['*'];
  28. $routeRules[0]['match']['domains'] = ['*'];
  29. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  30. $this->assertCount(12, $routes);
  31. $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*'];
  32. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  33. $this->assertCount(12, $routes);
  34. $routeRules[0]['match']['domains'] = ['domain1.*'];
  35. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  36. $this->assertCount(6, $routes);
  37. foreach ($routes as $route) {
  38. $this->assertContains('domain1', $route['route']->getDomain());
  39. }
  40. $routeRules[0]['match']['domains'] = ['domain2.*'];
  41. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  42. $this->assertCount(6, $routes);
  43. foreach ($routes as $route) {
  44. $this->assertContains('domain2', $route['route']->getDomain());
  45. }
  46. }
  47. public function testRespectsDomainsRuleForDingoRouter()
  48. {
  49. $this->registerDingoRoutes();
  50. $routeRules[0]['match']['versions'] = ['v1'];
  51. $routeRules[0]['match']['prefixes'] = ['*'];
  52. $routeRules[0]['match']['domains'] = ['*'];
  53. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  54. $this->assertCount(12, $routes);
  55. $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*'];
  56. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  57. $this->assertCount(12, $routes);
  58. $routeRules[0]['match']['domains'] = ['domain1.*'];
  59. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  60. $this->assertCount(6, $routes);
  61. foreach ($routes as $route) {
  62. $this->assertContains('domain1', $route['route']->getDomain());
  63. }
  64. $routeRules[0]['match']['domains'] = ['domain2.*'];
  65. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  66. $this->assertCount(6, $routes);
  67. foreach ($routes as $route) {
  68. $this->assertContains('domain2', $route['route']->getDomain());
  69. }
  70. }
  71. public function testRespectsPrefixesRuleForLaravelRouter()
  72. {
  73. $this->registerLaravelRoutes();
  74. $routeRules[0]['match']['domains'] = ['*'];
  75. $routeRules[0]['match']['prefixes'] = ['*'];
  76. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  77. $this->assertCount(12, $routes);
  78. $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*'];
  79. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  80. $this->assertCount(8, $routes);
  81. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  82. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  83. $this->assertCount(4, $routes);
  84. foreach ($routes as $route) {
  85. $this->assertTrue(str_is('prefix1/*', $route['route']->uri()));
  86. }
  87. $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
  88. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  89. $this->assertCount(4, $routes);
  90. foreach ($routes as $route) {
  91. $this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
  92. }
  93. }
  94. public function testRespectsPrefixesRuleForDingoRouter()
  95. {
  96. $this->registerDingoRoutes();
  97. $routeRules[0]['match']['versions'] = ['v1'];
  98. $routeRules[0]['match']['domains'] = ['*'];
  99. $routeRules[0]['match']['prefixes'] = ['*'];
  100. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  101. $this->assertCount(12, $routes);
  102. $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*'];
  103. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  104. $this->assertCount(8, $routes);
  105. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  106. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  107. $this->assertCount(4, $routes);
  108. foreach ($routes as $route) {
  109. $this->assertTrue(str_is('prefix1/*', $route['route']->uri()));
  110. }
  111. $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
  112. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  113. $this->assertCount(4, $routes);
  114. foreach ($routes as $route) {
  115. $this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
  116. }
  117. }
  118. public function testRespectsVersionsRuleForDingoRouter()
  119. {
  120. $this->registerDingoRoutes();
  121. $routeRules[0]['match']['versions'] = ['v2'];
  122. $routeRules[0]['match']['domains'] = ['*'];
  123. $routeRules[0]['match']['prefixes'] = ['*'];
  124. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  125. $this->assertCount(6, $routes);
  126. foreach ($routes as $route) {
  127. $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2']));
  128. }
  129. $routeRules[0]['match']['versions'] = ['v1', 'v2'];
  130. $routeRules[0]['match']['domains'] = ['*'];
  131. $routeRules[0]['match']['prefixes'] = ['*'];
  132. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  133. $this->assertCount(18, $routes);
  134. }
  135. public function testWillIncludeRouteIfListedExplicitlyForLaravelRouter()
  136. {
  137. $this->registerLaravelRoutes();
  138. $mustInclude = 'domain1-1';
  139. $routeRules[0]['include'] = [$mustInclude];
  140. $routeRules[0]['match']['domains'] = ['domain1.*'];
  141. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  142. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  143. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  144. return $route['route']->getName() === $mustInclude;
  145. });
  146. $this->assertCount(1, $oddRuleOut);
  147. }
  148. public function testWillIncludeRouteIfListedExplicitlyForDingoRouter()
  149. {
  150. $this->registerDingoRoutes();
  151. $mustInclude = 'v2.domain2';
  152. $routeRules = [
  153. [
  154. 'match' => [
  155. 'domains' => ['domain1.*'],
  156. 'prefixes' => ['prefix1/*'],
  157. 'versions' => ['v1'],
  158. ],
  159. 'include' => [$mustInclude],
  160. ],
  161. ];
  162. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  163. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  164. return $route['route']->getName() === $mustInclude;
  165. });
  166. $this->assertCount(1, $oddRuleOut);
  167. }
  168. public function testWillExcludeRouteIfListedExplicitlyForLaravelRouter()
  169. {
  170. $this->registerLaravelRoutes();
  171. $mustNotInclude = 'prefix1.domain1-1';
  172. $routeRules[0]['exclude'] = [$mustNotInclude];
  173. $routeRules[0]['match']['domains'] = ['domain1.*'];
  174. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  175. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  176. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  177. return $route['route']->getName() === $mustNotInclude;
  178. });
  179. $this->assertCount(0, $oddRuleOut);
  180. }
  181. public function testWillExcludeRouteIfListedExplicitlyForDingoRouter()
  182. {
  183. $this->registerDingoRoutes();
  184. $mustNotInclude = 'v2.domain2';
  185. $routeRules = [
  186. [
  187. 'match' => [
  188. 'domains' => ['domain2.*'],
  189. 'prefixes' => ['*'],
  190. 'versions' => ['v2'],
  191. ],
  192. 'exclude' => [$mustNotInclude],
  193. ],
  194. ];
  195. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  196. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  197. return $route['route']->getName() === $mustNotInclude;
  198. });
  199. $this->assertCount(0, $oddRuleOut);
  200. }
  201. public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
  202. {
  203. $this->registerLaravelRoutes();
  204. $routeRules = [
  205. [
  206. 'match' => [
  207. 'domains' => ['domain1.*'],
  208. 'prefixes' => ['prefix1/*'],
  209. ],
  210. ],
  211. [
  212. 'match' => [
  213. 'domains' => ['domain2.*'],
  214. 'prefixes' => ['prefix2*'],
  215. ],
  216. ],
  217. ];
  218. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  219. $this->assertCount(4, $routes);
  220. $routes = collect($routes);
  221. $firstRuleGroup = $routes->filter(function ($route) {
  222. return str_is('prefix1/*', $route['route']->uri())
  223. && str_is('domain1.*', $route['route']->getDomain());
  224. });
  225. $this->assertCount(2, $firstRuleGroup);
  226. $secondRuleGroup = $routes->filter(function ($route) {
  227. return str_is('prefix2/*', $route['route']->uri())
  228. && str_is('domain2.*', $route['route']->getDomain());
  229. });
  230. $this->assertCount(2, $secondRuleGroup);
  231. }
  232. public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter()
  233. {
  234. $this->registerDingoRoutes();
  235. $routeRules = [
  236. [
  237. 'match' => [
  238. 'domains' => ['*'],
  239. 'prefixes' => ['*'],
  240. 'versions' => ['v1'],
  241. ],
  242. ],
  243. [
  244. 'match' => [
  245. 'domains' => ['*'],
  246. 'prefixes' => ['*'],
  247. 'versions' => ['v2'],
  248. ],
  249. ],
  250. ];
  251. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  252. $this->assertCount(18, $routes);
  253. $routes = collect($routes);
  254. $firstRuleGroup = $routes->filter(function ($route) {
  255. return ! empty(array_intersect($route['route']->versions(), ['v1']));
  256. });
  257. $this->assertCount(12, $firstRuleGroup);
  258. $secondRuleGroup = $routes->filter(function ($route) {
  259. return ! empty(array_intersect($route['route']->versions(), ['v2']));
  260. });
  261. $this->assertCount(6, $secondRuleGroup);
  262. }
  263. private function registerLaravelRoutes()
  264. {
  265. RouteFacade::group(['domain' => 'domain1.app.test'], function () {
  266. RouteFacade::post('/domain1-1', function () {
  267. return 'hi';
  268. })->name('domain1-1');
  269. RouteFacade::get('domain1-2', function () {
  270. return 'hi';
  271. })->name('domain1-2');
  272. RouteFacade::get('/prefix1/domain1-1', function () {
  273. return 'hi';
  274. })->name('prefix1.domain1-1');
  275. RouteFacade::get('prefix1/domain1-2', function () {
  276. return 'hi';
  277. })->name('prefix1.domain1-2');
  278. RouteFacade::get('/prefix2/domain1-1', function () {
  279. return 'hi';
  280. })->name('prefix2.domain1-1');
  281. RouteFacade::get('prefix2/domain1-2', function () {
  282. return 'hi';
  283. })->name('prefix2.domain1-2');
  284. });
  285. RouteFacade::group(['domain' => 'domain2.app.test'], function () {
  286. RouteFacade::post('/domain2-1', function () {
  287. return 'hi';
  288. })->name('domain2-1');
  289. RouteFacade::get('domain2-2', function () {
  290. return 'hi';
  291. })->name('domain2-2');
  292. RouteFacade::get('/prefix1/domain2-1', function () {
  293. return 'hi';
  294. })->name('prefix1.domain2-1');
  295. RouteFacade::get('prefix1/domain2-2', function () {
  296. return 'hi';
  297. })->name('prefix1.domain2-2');
  298. RouteFacade::get('/prefix2/domain2-1', function () {
  299. return 'hi';
  300. })->name('prefix2.domain2-1');
  301. RouteFacade::get('prefix2/domain2-2', function () {
  302. return 'hi';
  303. })->name('prefix2.domain2-2');
  304. });
  305. }
  306. private function registerDingoRoutes()
  307. {
  308. $api = app('api.router');
  309. $api->version('v1', function (Router $api) {
  310. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  311. $api->post('/domain1-1', function () {
  312. return 'hi';
  313. })->name('v1.domain1-1');
  314. $api->get('domain1-2', function () {
  315. return 'hi';
  316. })->name('v1.domain1-2');
  317. $api->get('/prefix1/domain1-1', function () {
  318. return 'hi';
  319. })->name('v1.prefix1.domain1-1');
  320. $api->get('prefix1/domain1-2', function () {
  321. return 'hi';
  322. })->name('v1.prefix1.domain1-2');
  323. $api->get('/prefix2/domain1-1', function () {
  324. return 'hi';
  325. })->name('v1.prefix2.domain1-1');
  326. $api->get('prefix2/domain1-2', function () {
  327. return 'hi';
  328. })->name('v1.prefix2.domain1-2');
  329. });
  330. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  331. $api->post('/domain2-1', function () {
  332. return 'hi';
  333. })->name('v1.domain2-1');
  334. $api->get('domain2-2', function () {
  335. return 'hi';
  336. })->name('v1.domain2-2');
  337. $api->get('/prefix1/domain2-1', function () {
  338. return 'hi';
  339. })->name('v1.prefix1.domain2-1');
  340. $api->get('prefix1/domain2-2', function () {
  341. return 'hi';
  342. })->name('v1.prefix1.domain2-2');
  343. $api->get('/prefix2/domain2-1', function () {
  344. return 'hi';
  345. })->name('v1.prefix2.domain2-1');
  346. $api->get('prefix2/domain2-2', function () {
  347. return 'hi';
  348. })->name('v1.prefix2.domain2-2');
  349. });
  350. });
  351. $api->version('v2', function (Router $api) {
  352. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  353. $api->post('/domain1', function () {
  354. return 'hi';
  355. })->name('v2.domain1');
  356. $api->get('/prefix1/domain1', function () {
  357. return 'hi';
  358. })->name('v2.prefix1.domain1');
  359. $api->get('/prefix2/domain1', function () {
  360. return 'hi';
  361. })->name('v2.prefix2.domain1');
  362. });
  363. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  364. $api->post('/domain2', function () {
  365. return 'hi';
  366. })->name('v2.domain2');
  367. $api->get('/prefix1/domain2', function () {
  368. return 'hi';
  369. })->name('v2.prefix1.domain2');
  370. $api->get('/prefix2/domain2', function () {
  371. return 'hi';
  372. })->name('v2.prefix2.domain2');
  373. });
  374. });
  375. }
  376. }