RouteMatcherTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Dingo\Api\Routing\Router;
  4. use Illuminate\Support\Facades\Route as RouteFacade;
  5. use Illuminate\Support\Str;
  6. use Mpociot\ApiDoc\Matching\RouteMatcher;
  7. use Orchestra\Testbench\TestCase;
  8. class RouteMatcherTest extends TestCase
  9. {
  10. protected function getPackageProviders($app)
  11. {
  12. return [
  13. \Dingo\Api\Provider\LaravelServiceProvider::class,
  14. ];
  15. }
  16. public function testRespectsDomainsRuleForLaravelRouter()
  17. {
  18. $this->registerLaravelRoutes();
  19. $routeRules[0]['match']['prefixes'] = ['*'];
  20. $routeRules[0]['match']['domains'] = ['*'];
  21. $matcher = new RouteMatcher();
  22. $routes = $matcher->getRoutes($routeRules);
  23. $this->assertCount(12, $routes);
  24. $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*'];
  25. $matcher = new RouteMatcher();
  26. $routes = $matcher->getRoutes($routeRules);
  27. $this->assertCount(12, $routes);
  28. $routeRules[0]['match']['domains'] = ['domain1.*'];
  29. $matcher = new RouteMatcher();
  30. $routes = $matcher->getRoutes($routeRules);
  31. $this->assertCount(6, $routes);
  32. foreach ($routes as $route) {
  33. $this->assertStringContainsString('domain1', $route['route']->getDomain());
  34. }
  35. $routeRules[0]['match']['domains'] = ['domain2.*'];
  36. $matcher = new RouteMatcher();
  37. $routes = $matcher->getRoutes($routeRules);
  38. $this->assertCount(6, $routes);
  39. foreach ($routes as $route) {
  40. $this->assertStringContainsString('domain2', $route['route']->getDomain());
  41. }
  42. }
  43. public function testRespectsDomainsRuleForDingoRouter()
  44. {
  45. $this->registerDingoRoutes();
  46. $routeRules[0]['match']['versions'] = ['v1'];
  47. $routeRules[0]['match']['prefixes'] = ['*'];
  48. $routeRules[0]['match']['domains'] = ['*'];
  49. $matcher = new RouteMatcher();
  50. $routes = $matcher->getRoutes($routeRules, 'dingo');
  51. $this->assertCount(12, $routes);
  52. $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*'];
  53. $matcher = new RouteMatcher();
  54. $routes = $matcher->getRoutes($routeRules, 'dingo');
  55. $this->assertCount(12, $routes);
  56. $routeRules[0]['match']['domains'] = ['domain1.*'];
  57. $matcher = new RouteMatcher();
  58. $routes = $matcher->getRoutes($routeRules, 'dingo');
  59. $this->assertCount(6, $routes);
  60. foreach ($routes as $route) {
  61. $this->assertStringContainsString('domain1', $route['route']->getDomain());
  62. }
  63. $routeRules[0]['match']['domains'] = ['domain2.*'];
  64. $matcher = new RouteMatcher();
  65. $routes = $matcher->getRoutes($routeRules, 'dingo');
  66. $this->assertCount(6, $routes);
  67. foreach ($routes as $route) {
  68. $this->assertStringContainsString('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. $matcher = new RouteMatcher();
  77. $routes = $matcher->getRoutes($routeRules);
  78. $this->assertCount(12, $routes);
  79. $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*'];
  80. $matcher = new RouteMatcher();
  81. $routes = $matcher->getRoutes($routeRules);
  82. $this->assertCount(8, $routes);
  83. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  84. $matcher = new RouteMatcher();
  85. $routes = $matcher->getRoutes($routeRules);
  86. $this->assertCount(4, $routes);
  87. foreach ($routes as $route) {
  88. $this->assertTrue(Str::is('prefix1/*', $route['route']->uri()));
  89. }
  90. $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
  91. $matcher = new RouteMatcher();
  92. $routes = $matcher->getRoutes($routeRules);
  93. $this->assertCount(4, $routes);
  94. foreach ($routes as $route) {
  95. $this->assertTrue(Str::is('prefix2/*', $route['route']->uri()));
  96. }
  97. }
  98. public function testRespectsPrefixesRuleForDingoRouter()
  99. {
  100. $this->registerDingoRoutes();
  101. $routeRules[0]['match']['versions'] = ['v1'];
  102. $routeRules[0]['match']['domains'] = ['*'];
  103. $routeRules[0]['match']['prefixes'] = ['*'];
  104. $matcher = new RouteMatcher();
  105. $routes = $matcher->getRoutes($routeRules, 'dingo');
  106. $this->assertCount(12, $routes);
  107. $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*'];
  108. $matcher = new RouteMatcher();
  109. $routes = $matcher->getRoutes($routeRules, 'dingo');
  110. $this->assertCount(8, $routes);
  111. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  112. $matcher = new RouteMatcher();
  113. $routes = $matcher->getRoutes($routeRules, 'dingo');
  114. $this->assertCount(4, $routes);
  115. foreach ($routes as $route) {
  116. $this->assertTrue(Str::is('prefix1/*', $route['route']->uri()));
  117. }
  118. $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
  119. $matcher = new RouteMatcher();
  120. $routes = $matcher->getRoutes($routeRules, 'dingo');
  121. $this->assertCount(4, $routes);
  122. foreach ($routes as $route) {
  123. $this->assertTrue(Str::is('prefix2/*', $route['route']->uri()));
  124. }
  125. }
  126. public function testRespectsVersionsRuleForDingoRouter()
  127. {
  128. $this->registerDingoRoutes();
  129. $routeRules[0]['match']['versions'] = ['v2'];
  130. $routeRules[0]['match']['domains'] = ['*'];
  131. $routeRules[0]['match']['prefixes'] = ['*'];
  132. $matcher = new RouteMatcher();
  133. $routes = $matcher->getRoutes($routeRules, 'dingo');
  134. $this->assertCount(6, $routes);
  135. foreach ($routes as $route) {
  136. $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2']));
  137. }
  138. $routeRules[0]['match']['versions'] = ['v1', 'v2'];
  139. $routeRules[0]['match']['domains'] = ['*'];
  140. $routeRules[0]['match']['prefixes'] = ['*'];
  141. $matcher = new RouteMatcher();
  142. $routes = $matcher->getRoutes($routeRules, 'dingo');
  143. $this->assertCount(18, $routes);
  144. }
  145. public function testWillIncludeRouteIfListedExplicitlyForLaravelRouter()
  146. {
  147. $this->registerLaravelRoutes();
  148. $mustInclude = 'domain1-1';
  149. $routeRules[0]['include'] = [$mustInclude];
  150. $routeRules[0]['match']['domains'] = ['domain1.*'];
  151. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  152. $matcher = new RouteMatcher();
  153. $routes = $matcher->getRoutes($routeRules);
  154. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  155. return $route['route']->getName() === $mustInclude;
  156. });
  157. $this->assertCount(1, $oddRuleOut);
  158. }
  159. public function testWillIncludeRouteIfListedExplicitlyForDingoRouter()
  160. {
  161. $this->registerDingoRoutes();
  162. $mustInclude = 'v2.domain2';
  163. $routeRules = [
  164. [
  165. 'match' => [
  166. 'domains' => ['domain1.*'],
  167. 'prefixes' => ['prefix1/*'],
  168. 'versions' => ['v1'],
  169. ],
  170. 'include' => [$mustInclude],
  171. ],
  172. ];
  173. $matcher = new RouteMatcher();
  174. $routes = $matcher->getRoutes($routeRules, 'dingo');
  175. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  176. return $route['route']->getName() === $mustInclude;
  177. });
  178. $this->assertCount(1, $oddRuleOut);
  179. }
  180. public function testWillIncludeRouteIfMatchForAnIncludePatternForLaravelRouter()
  181. {
  182. $this->registerLaravelRoutes();
  183. $mustInclude = ['domain1-1', 'domain1-2'];
  184. $includePattern = 'domain1-*';
  185. $routeRules[0]['include'] = [$includePattern];
  186. $routeRules[0]['match']['domains'] = ['domain1.*'];
  187. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  188. $matcher = new RouteMatcher();
  189. $routes = $matcher->getRoutes($routeRules);
  190. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  191. return in_array($route['route']->getName(), $mustInclude);
  192. });
  193. $this->assertCount(count($mustInclude), $oddRuleOut);
  194. }
  195. public function testWillIncludeRouteIfMatchForAnIncludePatternForDingoRouter()
  196. {
  197. $this->registerDingoRoutes();
  198. $mustInclude = ['v2.domain1', 'v2.domain2'];
  199. $includePattern = 'v2.domain*';
  200. $routeRules = [
  201. [
  202. 'match' => [
  203. 'domains' => ['domain1.*'],
  204. 'prefixes' => ['prefix1/*'],
  205. 'versions' => ['v1'],
  206. ],
  207. 'include' => [$includePattern],
  208. ],
  209. ];
  210. $matcher = new RouteMatcher();
  211. $routes = $matcher->getRoutes($routeRules, 'dingo');
  212. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  213. return in_array($route['route']->getName(), $mustInclude);
  214. });
  215. $this->assertCount(count($mustInclude), $oddRuleOut);
  216. }
  217. public function testWillExcludeRouteIfListedExplicitlyForLaravelRouter()
  218. {
  219. $this->registerLaravelRoutes();
  220. $mustNotInclude = 'prefix1.domain1-1';
  221. $routeRules[0]['exclude'] = [$mustNotInclude];
  222. $routeRules[0]['match']['domains'] = ['domain1.*'];
  223. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  224. $matcher = new RouteMatcher();
  225. $routes = $matcher->getRoutes($routeRules);
  226. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  227. return $route['route']->getName() === $mustNotInclude;
  228. });
  229. $this->assertCount(0, $oddRuleOut);
  230. }
  231. public function testWillExcludeRouteIfListedExplicitlyForDingoRouter()
  232. {
  233. $this->registerDingoRoutes();
  234. $mustNotInclude = 'v2.domain2';
  235. $routeRules = [
  236. [
  237. 'match' => [
  238. 'domains' => ['domain2.*'],
  239. 'prefixes' => ['*'],
  240. 'versions' => ['v2'],
  241. ],
  242. 'exclude' => [$mustNotInclude],
  243. ],
  244. ];
  245. $matcher = new RouteMatcher();
  246. $routes = $matcher->getRoutes($routeRules, 'dingo');
  247. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  248. return $route['route']->getName() === $mustNotInclude;
  249. });
  250. $this->assertCount(0, $oddRuleOut);
  251. }
  252. public function testWillExcludeRouteIfMatchForAnExcludePatternForLaravelRouter()
  253. {
  254. $this->registerLaravelRoutes();
  255. $mustNotInclude = ['prefix1.domain1-1', 'prefix1.domain1-2'];
  256. $excludePattern = 'prefix1.domain1-*';
  257. $routeRules[0]['exclude'] = [$excludePattern];
  258. $routeRules[0]['match']['domains'] = ['domain1.*'];
  259. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  260. $matcher = new RouteMatcher();
  261. $routes = $matcher->getRoutes($routeRules);
  262. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  263. return in_array($route['route']->getName(), $mustNotInclude);
  264. });
  265. $this->assertCount(0, $oddRuleOut);
  266. }
  267. public function testWillExcludeRouteIfMatchForAnExcludePatterForDingoRouter()
  268. {
  269. $this->registerDingoRoutes();
  270. $mustNotInclude = ['v2.prefix1.domain2', 'v2.prefix2.domain2'];
  271. $excludePattern = 'v2.*.domain2';
  272. $routeRules = [
  273. [
  274. 'match' => [
  275. 'domains' => ['domain2.*'],
  276. 'prefixes' => ['*'],
  277. 'versions' => ['v2'],
  278. ],
  279. 'exclude' => [$excludePattern],
  280. ],
  281. ];
  282. $matcher = new RouteMatcher();
  283. $routes = $matcher->getRoutes($routeRules, 'dingo');
  284. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  285. return in_array($route['route']->getName(), $mustNotInclude);
  286. });
  287. $this->assertCount(0, $oddRuleOut);
  288. }
  289. public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
  290. {
  291. $this->registerLaravelRoutes();
  292. $routeRules = [
  293. [
  294. 'match' => [
  295. 'domains' => ['domain1.*'],
  296. 'prefixes' => ['prefix1/*'],
  297. ],
  298. ],
  299. [
  300. 'match' => [
  301. 'domains' => ['domain2.*'],
  302. 'prefixes' => ['prefix2*'],
  303. ],
  304. ],
  305. ];
  306. $matcher = new RouteMatcher();
  307. $routes = $matcher->getRoutes($routeRules);
  308. $this->assertCount(4, $routes);
  309. $routes = collect($routes);
  310. $firstRuleGroup = $routes->filter(function ($route) {
  311. return Str::is('prefix1/*', $route['route']->uri())
  312. && Str::is('domain1.*', $route['route']->getDomain());
  313. });
  314. $this->assertCount(2, $firstRuleGroup);
  315. $secondRuleGroup = $routes->filter(function ($route) {
  316. return Str::is('prefix2/*', $route['route']->uri())
  317. && Str::is('domain2.*', $route['route']->getDomain());
  318. });
  319. $this->assertCount(2, $secondRuleGroup);
  320. }
  321. public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter()
  322. {
  323. $this->registerDingoRoutes();
  324. $routeRules = [
  325. [
  326. 'match' => [
  327. 'domains' => ['*'],
  328. 'prefixes' => ['*'],
  329. 'versions' => ['v1'],
  330. ],
  331. ],
  332. [
  333. 'match' => [
  334. 'domains' => ['*'],
  335. 'prefixes' => ['*'],
  336. 'versions' => ['v2'],
  337. ],
  338. ],
  339. ];
  340. $matcher = new RouteMatcher();
  341. $routes = $matcher->getRoutes($routeRules, 'dingo');
  342. $this->assertCount(18, $routes);
  343. $routes = collect($routes);
  344. $firstRuleGroup = $routes->filter(function ($route) {
  345. return ! empty(array_intersect($route['route']->versions(), ['v1']));
  346. });
  347. $this->assertCount(12, $firstRuleGroup);
  348. $secondRuleGroup = $routes->filter(function ($route) {
  349. return ! empty(array_intersect($route['route']->versions(), ['v2']));
  350. });
  351. $this->assertCount(6, $secondRuleGroup);
  352. }
  353. private function registerLaravelRoutes()
  354. {
  355. RouteFacade::group(['domain' => 'domain1.app.test'], function () {
  356. RouteFacade::post('/domain1-1', function () {
  357. return 'hi';
  358. })->name('domain1-1');
  359. RouteFacade::get('domain1-2', function () {
  360. return 'hi';
  361. })->name('domain1-2');
  362. RouteFacade::get('/prefix1/domain1-1', function () {
  363. return 'hi';
  364. })->name('prefix1.domain1-1');
  365. RouteFacade::get('prefix1/domain1-2', function () {
  366. return 'hi';
  367. })->name('prefix1.domain1-2');
  368. RouteFacade::get('/prefix2/domain1-1', function () {
  369. return 'hi';
  370. })->name('prefix2.domain1-1');
  371. RouteFacade::get('prefix2/domain1-2', function () {
  372. return 'hi';
  373. })->name('prefix2.domain1-2');
  374. });
  375. RouteFacade::group(['domain' => 'domain2.app.test'], function () {
  376. RouteFacade::post('/domain2-1', function () {
  377. return 'hi';
  378. })->name('domain2-1');
  379. RouteFacade::get('domain2-2', function () {
  380. return 'hi';
  381. })->name('domain2-2');
  382. RouteFacade::get('/prefix1/domain2-1', function () {
  383. return 'hi';
  384. })->name('prefix1.domain2-1');
  385. RouteFacade::get('prefix1/domain2-2', function () {
  386. return 'hi';
  387. })->name('prefix1.domain2-2');
  388. RouteFacade::get('/prefix2/domain2-1', function () {
  389. return 'hi';
  390. })->name('prefix2.domain2-1');
  391. RouteFacade::get('prefix2/domain2-2', function () {
  392. return 'hi';
  393. })->name('prefix2.domain2-2');
  394. });
  395. }
  396. private function registerDingoRoutes()
  397. {
  398. $api = app('api.router');
  399. $api->version('v1', function (Router $api) {
  400. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  401. $api->post('/domain1-1', function () {
  402. return 'hi';
  403. })->name('v1.domain1-1');
  404. $api->get('domain1-2', function () {
  405. return 'hi';
  406. })->name('v1.domain1-2');
  407. $api->get('/prefix1/domain1-1', function () {
  408. return 'hi';
  409. })->name('v1.prefix1.domain1-1');
  410. $api->get('prefix1/domain1-2', function () {
  411. return 'hi';
  412. })->name('v1.prefix1.domain1-2');
  413. $api->get('/prefix2/domain1-1', function () {
  414. return 'hi';
  415. })->name('v1.prefix2.domain1-1');
  416. $api->get('prefix2/domain1-2', function () {
  417. return 'hi';
  418. })->name('v1.prefix2.domain1-2');
  419. });
  420. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  421. $api->post('/domain2-1', function () {
  422. return 'hi';
  423. })->name('v1.domain2-1');
  424. $api->get('domain2-2', function () {
  425. return 'hi';
  426. })->name('v1.domain2-2');
  427. $api->get('/prefix1/domain2-1', function () {
  428. return 'hi';
  429. })->name('v1.prefix1.domain2-1');
  430. $api->get('prefix1/domain2-2', function () {
  431. return 'hi';
  432. })->name('v1.prefix1.domain2-2');
  433. $api->get('/prefix2/domain2-1', function () {
  434. return 'hi';
  435. })->name('v1.prefix2.domain2-1');
  436. $api->get('prefix2/domain2-2', function () {
  437. return 'hi';
  438. })->name('v1.prefix2.domain2-2');
  439. });
  440. });
  441. $api->version('v2', function (Router $api) {
  442. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  443. $api->post('/domain1', function () {
  444. return 'hi';
  445. })->name('v2.domain1');
  446. $api->get('/prefix1/domain1', function () {
  447. return 'hi';
  448. })->name('v2.prefix1.domain1');
  449. $api->get('/prefix2/domain1', function () {
  450. return 'hi';
  451. })->name('v2.prefix2.domain1');
  452. });
  453. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  454. $api->post('/domain2', function () {
  455. return 'hi';
  456. })->name('v2.domain2');
  457. $api->get('/prefix1/domain2', function () {
  458. return 'hi';
  459. })->name('v2.prefix1.domain2');
  460. $api->get('/prefix2/domain2', function () {
  461. return 'hi';
  462. })->name('v2.prefix2.domain2');
  463. });
  464. });
  465. }
  466. }