RouteMatcherTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 testWillExcludeRouteIfMatchForAnExcludePatternForLaravelRouter()
  202. {
  203. $this->registerLaravelRoutes();
  204. $mustNotInclude = ['prefix1.domain1-1', 'prefix1.domain1-2'];
  205. $excludePattern = 'prefix1.domain1-*';
  206. $routeRules[0]['exclude'] = [$excludePattern];
  207. $routeRules[0]['match']['domains'] = ['domain1.*'];
  208. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  209. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  210. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  211. return in_array($route['route']->getName(), $mustNotInclude);
  212. });
  213. $this->assertCount(0, $oddRuleOut);
  214. }
  215. public function testWillExcludeRouteIfMatchForAnExcludePatterForDingoRouter()
  216. {
  217. $this->registerDingoRoutes();
  218. $mustNotInclude = ['v2.prefix1.domain2', 'v2.prefix2.domain2'];
  219. $excludePattern = 'v2.*.domain2';
  220. $routeRules = [
  221. [
  222. 'match' => [
  223. 'domains' => ['domain2.*'],
  224. 'prefixes' => ['*'],
  225. 'versions' => ['v2'],
  226. ],
  227. 'exclude' => [$excludePattern],
  228. ],
  229. ];
  230. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  231. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  232. return in_array($route['route']->getName(), $mustNotInclude);
  233. });
  234. $this->assertCount(0, $oddRuleOut);
  235. }
  236. public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
  237. {
  238. $this->registerLaravelRoutes();
  239. $routeRules = [
  240. [
  241. 'match' => [
  242. 'domains' => ['domain1.*'],
  243. 'prefixes' => ['prefix1/*'],
  244. ],
  245. ],
  246. [
  247. 'match' => [
  248. 'domains' => ['domain2.*'],
  249. 'prefixes' => ['prefix2*'],
  250. ],
  251. ],
  252. ];
  253. $routes = $this->matcher->getRoutesToBeDocumented($routeRules);
  254. $this->assertCount(4, $routes);
  255. $routes = collect($routes);
  256. $firstRuleGroup = $routes->filter(function ($route) {
  257. return str_is('prefix1/*', $route['route']->uri())
  258. && str_is('domain1.*', $route['route']->getDomain());
  259. });
  260. $this->assertCount(2, $firstRuleGroup);
  261. $secondRuleGroup = $routes->filter(function ($route) {
  262. return str_is('prefix2/*', $route['route']->uri())
  263. && str_is('domain2.*', $route['route']->getDomain());
  264. });
  265. $this->assertCount(2, $secondRuleGroup);
  266. }
  267. public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter()
  268. {
  269. $this->registerDingoRoutes();
  270. $routeRules = [
  271. [
  272. 'match' => [
  273. 'domains' => ['*'],
  274. 'prefixes' => ['*'],
  275. 'versions' => ['v1'],
  276. ],
  277. ],
  278. [
  279. 'match' => [
  280. 'domains' => ['*'],
  281. 'prefixes' => ['*'],
  282. 'versions' => ['v2'],
  283. ],
  284. ],
  285. ];
  286. $routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
  287. $this->assertCount(18, $routes);
  288. $routes = collect($routes);
  289. $firstRuleGroup = $routes->filter(function ($route) {
  290. return ! empty(array_intersect($route['route']->versions(), ['v1']));
  291. });
  292. $this->assertCount(12, $firstRuleGroup);
  293. $secondRuleGroup = $routes->filter(function ($route) {
  294. return ! empty(array_intersect($route['route']->versions(), ['v2']));
  295. });
  296. $this->assertCount(6, $secondRuleGroup);
  297. }
  298. private function registerLaravelRoutes()
  299. {
  300. RouteFacade::group(['domain' => 'domain1.app.test'], function () {
  301. RouteFacade::post('/domain1-1', function () {
  302. return 'hi';
  303. })->name('domain1-1');
  304. RouteFacade::get('domain1-2', function () {
  305. return 'hi';
  306. })->name('domain1-2');
  307. RouteFacade::get('/prefix1/domain1-1', function () {
  308. return 'hi';
  309. })->name('prefix1.domain1-1');
  310. RouteFacade::get('prefix1/domain1-2', function () {
  311. return 'hi';
  312. })->name('prefix1.domain1-2');
  313. RouteFacade::get('/prefix2/domain1-1', function () {
  314. return 'hi';
  315. })->name('prefix2.domain1-1');
  316. RouteFacade::get('prefix2/domain1-2', function () {
  317. return 'hi';
  318. })->name('prefix2.domain1-2');
  319. });
  320. RouteFacade::group(['domain' => 'domain2.app.test'], function () {
  321. RouteFacade::post('/domain2-1', function () {
  322. return 'hi';
  323. })->name('domain2-1');
  324. RouteFacade::get('domain2-2', function () {
  325. return 'hi';
  326. })->name('domain2-2');
  327. RouteFacade::get('/prefix1/domain2-1', function () {
  328. return 'hi';
  329. })->name('prefix1.domain2-1');
  330. RouteFacade::get('prefix1/domain2-2', function () {
  331. return 'hi';
  332. })->name('prefix1.domain2-2');
  333. RouteFacade::get('/prefix2/domain2-1', function () {
  334. return 'hi';
  335. })->name('prefix2.domain2-1');
  336. RouteFacade::get('prefix2/domain2-2', function () {
  337. return 'hi';
  338. })->name('prefix2.domain2-2');
  339. });
  340. }
  341. private function registerDingoRoutes()
  342. {
  343. $api = app('api.router');
  344. $api->version('v1', function (Router $api) {
  345. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  346. $api->post('/domain1-1', function () {
  347. return 'hi';
  348. })->name('v1.domain1-1');
  349. $api->get('domain1-2', function () {
  350. return 'hi';
  351. })->name('v1.domain1-2');
  352. $api->get('/prefix1/domain1-1', function () {
  353. return 'hi';
  354. })->name('v1.prefix1.domain1-1');
  355. $api->get('prefix1/domain1-2', function () {
  356. return 'hi';
  357. })->name('v1.prefix1.domain1-2');
  358. $api->get('/prefix2/domain1-1', function () {
  359. return 'hi';
  360. })->name('v1.prefix2.domain1-1');
  361. $api->get('prefix2/domain1-2', function () {
  362. return 'hi';
  363. })->name('v1.prefix2.domain1-2');
  364. });
  365. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  366. $api->post('/domain2-1', function () {
  367. return 'hi';
  368. })->name('v1.domain2-1');
  369. $api->get('domain2-2', function () {
  370. return 'hi';
  371. })->name('v1.domain2-2');
  372. $api->get('/prefix1/domain2-1', function () {
  373. return 'hi';
  374. })->name('v1.prefix1.domain2-1');
  375. $api->get('prefix1/domain2-2', function () {
  376. return 'hi';
  377. })->name('v1.prefix1.domain2-2');
  378. $api->get('/prefix2/domain2-1', function () {
  379. return 'hi';
  380. })->name('v1.prefix2.domain2-1');
  381. $api->get('prefix2/domain2-2', function () {
  382. return 'hi';
  383. })->name('v1.prefix2.domain2-2');
  384. });
  385. });
  386. $api->version('v2', function (Router $api) {
  387. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  388. $api->post('/domain1', function () {
  389. return 'hi';
  390. })->name('v2.domain1');
  391. $api->get('/prefix1/domain1', function () {
  392. return 'hi';
  393. })->name('v2.prefix1.domain1');
  394. $api->get('/prefix2/domain1', function () {
  395. return 'hi';
  396. })->name('v2.prefix2.domain1');
  397. });
  398. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  399. $api->post('/domain2', function () {
  400. return 'hi';
  401. })->name('v2.domain2');
  402. $api->get('/prefix1/domain2', function () {
  403. return 'hi';
  404. })->name('v2.prefix1.domain2');
  405. $api->get('/prefix2/domain2', function () {
  406. return 'hi';
  407. })->name('v2.prefix2.domain2');
  408. });
  409. });
  410. }
  411. }