RouteMatcherTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 testWillIncludeRouteIfMatchForAnIncludePatternForLaravelRouter()
  169. {
  170. $this->registerLaravelRoutes();
  171. $mustInclude = ['domain1-1', 'domain1-2'];
  172. $includePattern = 'domain1-*';
  173. $routeRules[0]['include'] = [$includePattern];
  174. $routeRules[0]['match']['domains'] = ['domain1.*'];
  175. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  176. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  177. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  178. return in_array($route['route']->getName(), $mustInclude);
  179. });
  180. $this->assertCount(count($mustInclude), $oddRuleOut);
  181. }
  182. public function testWillIncludeRouteIfMatchForAnIncludePatternForDingoRouter()
  183. {
  184. $this->registerDingoRoutes();
  185. $mustInclude = ['v2.domain1', 'v2.domain2'];
  186. $includePattern = 'v2.domain*';
  187. $routeRules = [
  188. [
  189. 'match' => [
  190. 'domains' => ['domain1.*'],
  191. 'prefixes' => ['prefix1/*'],
  192. 'versions' => ['v1'],
  193. ],
  194. 'include' => [$includePattern],
  195. ],
  196. ];
  197. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  198. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  199. return in_array($route['route']->getName(), $mustInclude);
  200. });
  201. $this->assertCount(count($mustInclude), $oddRuleOut);
  202. }
  203. public function testWillExcludeRouteIfListedExplicitlyForLaravelRouter()
  204. {
  205. $this->registerLaravelRoutes();
  206. $mustNotInclude = 'prefix1.domain1-1';
  207. $routeRules[0]['exclude'] = [$mustNotInclude];
  208. $routeRules[0]['match']['domains'] = ['domain1.*'];
  209. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  210. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  211. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  212. return $route['route']->getName() === $mustNotInclude;
  213. });
  214. $this->assertCount(0, $oddRuleOut);
  215. }
  216. public function testWillExcludeRouteIfListedExplicitlyForDingoRouter()
  217. {
  218. $this->registerDingoRoutes();
  219. $mustNotInclude = 'v2.domain2';
  220. $routeRules = [
  221. [
  222. 'match' => [
  223. 'domains' => ['domain2.*'],
  224. 'prefixes' => ['*'],
  225. 'versions' => ['v2'],
  226. ],
  227. 'exclude' => [$mustNotInclude],
  228. ],
  229. ];
  230. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  231. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  232. return $route['route']->getName() === $mustNotInclude;
  233. });
  234. $this->assertCount(0, $oddRuleOut);
  235. }
  236. public function testWillExcludeRouteIfMatchForAnExcludePatternForLaravelRouter()
  237. {
  238. $this->registerLaravelRoutes();
  239. $mustNotInclude = ['prefix1.domain1-1', 'prefix1.domain1-2'];
  240. $excludePattern = 'prefix1.domain1-*';
  241. $routeRules[0]['exclude'] = [$excludePattern];
  242. $routeRules[0]['match']['domains'] = ['domain1.*'];
  243. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  244. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  245. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  246. return in_array($route['route']->getName(), $mustNotInclude);
  247. });
  248. $this->assertCount(0, $oddRuleOut);
  249. }
  250. public function testWillExcludeRouteIfMatchForAnExcludePatterForDingoRouter()
  251. {
  252. $this->registerDingoRoutes();
  253. $mustNotInclude = ['v2.prefix1.domain2', 'v2.prefix2.domain2'];
  254. $excludePattern = 'v2.*.domain2';
  255. $routeRules = [
  256. [
  257. 'match' => [
  258. 'domains' => ['domain2.*'],
  259. 'prefixes' => ['*'],
  260. 'versions' => ['v2'],
  261. ],
  262. 'exclude' => [$excludePattern],
  263. ],
  264. ];
  265. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  266. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  267. return in_array($route['route']->getName(), $mustNotInclude);
  268. });
  269. $this->assertCount(0, $oddRuleOut);
  270. }
  271. public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
  272. {
  273. $this->registerLaravelRoutes();
  274. $routeRules = [
  275. [
  276. 'match' => [
  277. 'domains' => ['domain1.*'],
  278. 'prefixes' => ['prefix1/*'],
  279. ],
  280. ],
  281. [
  282. 'match' => [
  283. 'domains' => ['domain2.*'],
  284. 'prefixes' => ['prefix2*'],
  285. ],
  286. ],
  287. ];
  288. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  289. $this->assertCount(4, $routes);
  290. $routes = collect($routes);
  291. $firstRuleGroup = $routes->filter(function ($route) {
  292. return str_is('prefix1/*', $route['route']->uri())
  293. && str_is('domain1.*', $route['route']->getDomain());
  294. });
  295. $this->assertCount(2, $firstRuleGroup);
  296. $secondRuleGroup = $routes->filter(function ($route) {
  297. return str_is('prefix2/*', $route['route']->uri())
  298. && str_is('domain2.*', $route['route']->getDomain());
  299. });
  300. $this->assertCount(2, $secondRuleGroup);
  301. }
  302. public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter()
  303. {
  304. $this->registerDingoRoutes();
  305. $routeRules = [
  306. [
  307. 'match' => [
  308. 'domains' => ['*'],
  309. 'prefixes' => ['*'],
  310. 'versions' => ['v1'],
  311. ],
  312. ],
  313. [
  314. 'match' => [
  315. 'domains' => ['*'],
  316. 'prefixes' => ['*'],
  317. 'versions' => ['v2'],
  318. ],
  319. ],
  320. ];
  321. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  322. $this->assertCount(18, $routes);
  323. $routes = collect($routes);
  324. $firstRuleGroup = $routes->filter(function ($route) {
  325. return ! empty(array_intersect($route['route']->versions(), ['v1']));
  326. });
  327. $this->assertCount(12, $firstRuleGroup);
  328. $secondRuleGroup = $routes->filter(function ($route) {
  329. return ! empty(array_intersect($route['route']->versions(), ['v2']));
  330. });
  331. $this->assertCount(6, $secondRuleGroup);
  332. }
  333. private function registerLaravelRoutes()
  334. {
  335. RouteFacade::group(['domain' => 'domain1.app.test'], function () {
  336. RouteFacade::post('/domain1-1', function () {
  337. return 'hi';
  338. })->name('domain1-1');
  339. RouteFacade::get('domain1-2', function () {
  340. return 'hi';
  341. })->name('domain1-2');
  342. RouteFacade::get('/prefix1/domain1-1', function () {
  343. return 'hi';
  344. })->name('prefix1.domain1-1');
  345. RouteFacade::get('prefix1/domain1-2', function () {
  346. return 'hi';
  347. })->name('prefix1.domain1-2');
  348. RouteFacade::get('/prefix2/domain1-1', function () {
  349. return 'hi';
  350. })->name('prefix2.domain1-1');
  351. RouteFacade::get('prefix2/domain1-2', function () {
  352. return 'hi';
  353. })->name('prefix2.domain1-2');
  354. });
  355. RouteFacade::group(['domain' => 'domain2.app.test'], function () {
  356. RouteFacade::post('/domain2-1', function () {
  357. return 'hi';
  358. })->name('domain2-1');
  359. RouteFacade::get('domain2-2', function () {
  360. return 'hi';
  361. })->name('domain2-2');
  362. RouteFacade::get('/prefix1/domain2-1', function () {
  363. return 'hi';
  364. })->name('prefix1.domain2-1');
  365. RouteFacade::get('prefix1/domain2-2', function () {
  366. return 'hi';
  367. })->name('prefix1.domain2-2');
  368. RouteFacade::get('/prefix2/domain2-1', function () {
  369. return 'hi';
  370. })->name('prefix2.domain2-1');
  371. RouteFacade::get('prefix2/domain2-2', function () {
  372. return 'hi';
  373. })->name('prefix2.domain2-2');
  374. });
  375. }
  376. private function registerDingoRoutes()
  377. {
  378. $api = app('api.router');
  379. $api->version('v1', function (Router $api) {
  380. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  381. $api->post('/domain1-1', function () {
  382. return 'hi';
  383. })->name('v1.domain1-1');
  384. $api->get('domain1-2', function () {
  385. return 'hi';
  386. })->name('v1.domain1-2');
  387. $api->get('/prefix1/domain1-1', function () {
  388. return 'hi';
  389. })->name('v1.prefix1.domain1-1');
  390. $api->get('prefix1/domain1-2', function () {
  391. return 'hi';
  392. })->name('v1.prefix1.domain1-2');
  393. $api->get('/prefix2/domain1-1', function () {
  394. return 'hi';
  395. })->name('v1.prefix2.domain1-1');
  396. $api->get('prefix2/domain1-2', function () {
  397. return 'hi';
  398. })->name('v1.prefix2.domain1-2');
  399. });
  400. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  401. $api->post('/domain2-1', function () {
  402. return 'hi';
  403. })->name('v1.domain2-1');
  404. $api->get('domain2-2', function () {
  405. return 'hi';
  406. })->name('v1.domain2-2');
  407. $api->get('/prefix1/domain2-1', function () {
  408. return 'hi';
  409. })->name('v1.prefix1.domain2-1');
  410. $api->get('prefix1/domain2-2', function () {
  411. return 'hi';
  412. })->name('v1.prefix1.domain2-2');
  413. $api->get('/prefix2/domain2-1', function () {
  414. return 'hi';
  415. })->name('v1.prefix2.domain2-1');
  416. $api->get('prefix2/domain2-2', function () {
  417. return 'hi';
  418. })->name('v1.prefix2.domain2-2');
  419. });
  420. });
  421. $api->version('v2', function (Router $api) {
  422. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  423. $api->post('/domain1', function () {
  424. return 'hi';
  425. })->name('v2.domain1');
  426. $api->get('/prefix1/domain1', function () {
  427. return 'hi';
  428. })->name('v2.prefix1.domain1');
  429. $api->get('/prefix2/domain1', function () {
  430. return 'hi';
  431. })->name('v2.prefix2.domain1');
  432. });
  433. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  434. $api->post('/domain2', function () {
  435. return 'hi';
  436. })->name('v2.domain2');
  437. $api->get('/prefix1/domain2', function () {
  438. return 'hi';
  439. })->name('v2.prefix1.domain2');
  440. $api->get('/prefix2/domain2', function () {
  441. return 'hi';
  442. })->name('v2.prefix2.domain2');
  443. });
  444. });
  445. }
  446. }