RouteMatcherDingoTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Dingo\Api\Routing\Router;
  4. use Illuminate\Support\Str;
  5. use Knuckles\Scribe\Matching\RouteMatcher;
  6. use Knuckles\Scribe\Tests\BaseLaravelTest;
  7. /**
  8. * @group dingo
  9. */
  10. class RouteMatcherDingoTest extends BaseLaravelTest
  11. {
  12. protected function getPackageProviders($app)
  13. {
  14. return [
  15. \Dingo\Api\Provider\LaravelServiceProvider::class,
  16. ];
  17. }
  18. public function testRespectsDomainsRuleForDingoRouter()
  19. {
  20. $this->registerDingoRoutes();
  21. $routeRules[0]['match']['versions'] = ['v1'];
  22. $routeRules[0]['match']['prefixes'] = ['*'];
  23. $routeRules[0]['match']['domains'] = ['*'];
  24. $matcher = new RouteMatcher();
  25. $routes = $matcher->getRoutes($routeRules, 'dingo');
  26. $this->assertCount(12, $routes);
  27. $routeRules[0]['match']['domains'] = ['domain1.*', 'domain2.*'];
  28. $matcher = new RouteMatcher();
  29. $routes = $matcher->getRoutes($routeRules, 'dingo');
  30. $this->assertCount(12, $routes);
  31. $routeRules[0]['match']['domains'] = ['domain1.*'];
  32. $matcher = new RouteMatcher();
  33. $routes = $matcher->getRoutes($routeRules, 'dingo');
  34. $this->assertCount(6, $routes);
  35. foreach ($routes as $route) {
  36. $this->assertStringContainsString('domain1', $route['route']->getDomain());
  37. }
  38. $routeRules[0]['match']['domains'] = ['domain2.*'];
  39. $matcher = new RouteMatcher();
  40. $routes = $matcher->getRoutes($routeRules, 'dingo');
  41. $this->assertCount(6, $routes);
  42. foreach ($routes as $route) {
  43. $this->assertStringContainsString('domain2', $route['route']->getDomain());
  44. }
  45. }
  46. public function testRespectsPrefixesRuleForDingoRouter()
  47. {
  48. $this->registerDingoRoutes();
  49. $routeRules[0]['match']['versions'] = ['v1'];
  50. $routeRules[0]['match']['domains'] = ['*'];
  51. $routeRules[0]['match']['prefixes'] = ['*'];
  52. $matcher = new RouteMatcher();
  53. $routes = $matcher->getRoutes($routeRules, 'dingo');
  54. $this->assertCount(12, $routes);
  55. $routeRules[0]['match']['prefixes'] = ['prefix1/*', 'prefix2/*'];
  56. $matcher = new RouteMatcher();
  57. $routes = $matcher->getRoutes($routeRules, 'dingo');
  58. $this->assertCount(8, $routes);
  59. $routeRules[0]['match']['prefixes'] = ['prefix1/*'];
  60. $matcher = new RouteMatcher();
  61. $routes = $matcher->getRoutes($routeRules, 'dingo');
  62. $this->assertCount(4, $routes);
  63. foreach ($routes as $route) {
  64. $this->assertTrue(Str::is('prefix1/*', $route['route']->uri()));
  65. }
  66. $routeRules[0]['match']['prefixes'] = ['prefix2/*'];
  67. $matcher = new RouteMatcher();
  68. $routes = $matcher->getRoutes($routeRules, 'dingo');
  69. $this->assertCount(4, $routes);
  70. foreach ($routes as $route) {
  71. $this->assertTrue(Str::is('prefix2/*', $route['route']->uri()));
  72. }
  73. }
  74. public function testRespectsVersionsRuleForDingoRouter()
  75. {
  76. $this->registerDingoRoutes();
  77. $routeRules[0]['match']['versions'] = ['v2'];
  78. $routeRules[0]['match']['domains'] = ['*'];
  79. $routeRules[0]['match']['prefixes'] = ['*'];
  80. $matcher = new RouteMatcher();
  81. $routes = $matcher->getRoutes($routeRules, 'dingo');
  82. $this->assertCount(6, $routes);
  83. foreach ($routes as $route) {
  84. $this->assertNotEmpty(array_intersect($route['route']->versions(), ['v2']));
  85. }
  86. $routeRules[0]['match']['versions'] = ['v1', 'v2'];
  87. $routeRules[0]['match']['domains'] = ['*'];
  88. $routeRules[0]['match']['prefixes'] = ['*'];
  89. $matcher = new RouteMatcher();
  90. $routes = $matcher->getRoutes($routeRules, 'dingo');
  91. $this->assertCount(18, $routes);
  92. }
  93. public function testWillIncludeRouteIfListedExplicitlyForDingoRouter()
  94. {
  95. $this->registerDingoRoutes();
  96. $mustInclude = 'v2.domain2';
  97. $routeRules = [
  98. [
  99. 'match' => [
  100. 'domains' => ['domain1.*'],
  101. 'prefixes' => ['prefix1/*'],
  102. 'versions' => ['v1'],
  103. ],
  104. 'include' => [$mustInclude],
  105. ],
  106. ];
  107. $matcher = new RouteMatcher();
  108. $routes = $matcher->getRoutes($routeRules, 'dingo');
  109. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  110. return $route['route']->getName() === $mustInclude;
  111. });
  112. $this->assertCount(1, $oddRuleOut);
  113. }
  114. public function testWillIncludeRouteIfMatchForAnIncludePatternForDingoRouter()
  115. {
  116. $this->registerDingoRoutes();
  117. $mustInclude = ['v2.domain1', 'v2.domain2'];
  118. $includePattern = 'v2.domain*';
  119. $routeRules = [
  120. [
  121. 'match' => [
  122. 'domains' => ['domain1.*'],
  123. 'prefixes' => ['prefix1/*'],
  124. 'versions' => ['v1'],
  125. ],
  126. 'include' => [$includePattern],
  127. ],
  128. ];
  129. $matcher = new RouteMatcher();
  130. $routes = $matcher->getRoutes($routeRules, 'dingo');
  131. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustInclude) {
  132. return in_array($route['route']->getName(), $mustInclude);
  133. });
  134. $this->assertCount(count($mustInclude), $oddRuleOut);
  135. }
  136. public function testWillExcludeRouteIfListedExplicitlyForDingoRouter()
  137. {
  138. $this->registerDingoRoutes();
  139. $mustNotInclude = 'v2.domain2';
  140. $routeRules = [
  141. [
  142. 'match' => [
  143. 'domains' => ['domain2.*'],
  144. 'prefixes' => ['*'],
  145. 'versions' => ['v2'],
  146. ],
  147. 'exclude' => [$mustNotInclude],
  148. ],
  149. ];
  150. $matcher = new RouteMatcher();
  151. $routes = $matcher->getRoutes($routeRules, 'dingo');
  152. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  153. return $route['route']->getName() === $mustNotInclude;
  154. });
  155. $this->assertCount(0, $oddRuleOut);
  156. }
  157. public function testWillExcludeRouteIfMatchForAnExcludePatterForDingoRouter()
  158. {
  159. $this->registerDingoRoutes();
  160. $mustNotInclude = ['v2.prefix1.domain2', 'v2.prefix2.domain2'];
  161. $excludePattern = 'v2.*.domain2';
  162. $routeRules = [
  163. [
  164. 'match' => [
  165. 'domains' => ['domain2.*'],
  166. 'prefixes' => ['*'],
  167. 'versions' => ['v2'],
  168. ],
  169. 'exclude' => [$excludePattern],
  170. ],
  171. ];
  172. $matcher = new RouteMatcher();
  173. $routes = $matcher->getRoutes($routeRules, 'dingo');
  174. $oddRuleOut = collect($routes)->filter(function ($route) use ($mustNotInclude) {
  175. return in_array($route['route']->getName(), $mustNotInclude);
  176. });
  177. $this->assertCount(0, $oddRuleOut);
  178. }
  179. public function testMergesRoutesFromDifferentRuleGroupsForDingoRouter()
  180. {
  181. $this->registerDingoRoutes();
  182. $routeRules = [
  183. [
  184. 'match' => [
  185. 'domains' => ['*'],
  186. 'prefixes' => ['*'],
  187. 'versions' => ['v1'],
  188. ],
  189. ],
  190. [
  191. 'match' => [
  192. 'domains' => ['*'],
  193. 'prefixes' => ['*'],
  194. 'versions' => ['v2'],
  195. ],
  196. ],
  197. ];
  198. $matcher = new RouteMatcher();
  199. $routes = $matcher->getRoutes($routeRules, 'dingo');
  200. $this->assertCount(18, $routes);
  201. $routes = collect($routes);
  202. $firstRuleGroup = $routes->filter(function ($route) {
  203. return ! empty(array_intersect($route['route']->versions(), ['v1']));
  204. });
  205. $this->assertCount(12, $firstRuleGroup);
  206. $secondRuleGroup = $routes->filter(function ($route) {
  207. return ! empty(array_intersect($route['route']->versions(), ['v2']));
  208. });
  209. $this->assertCount(6, $secondRuleGroup);
  210. }
  211. private function registerDingoRoutes()
  212. {
  213. $api = app('api.router');
  214. $api->version('v1', function (Router $api) {
  215. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  216. $api->post('/domain1-1', function () {
  217. return 'hi';
  218. })->name('v1.domain1-1');
  219. $api->get('domain1-2', function () {
  220. return 'hi';
  221. })->name('v1.domain1-2');
  222. $api->get('/prefix1/domain1-1', function () {
  223. return 'hi';
  224. })->name('v1.prefix1.domain1-1');
  225. $api->get('prefix1/domain1-2', function () {
  226. return 'hi';
  227. })->name('v1.prefix1.domain1-2');
  228. $api->get('/prefix2/domain1-1', function () {
  229. return 'hi';
  230. })->name('v1.prefix2.domain1-1');
  231. $api->get('prefix2/domain1-2', function () {
  232. return 'hi';
  233. })->name('v1.prefix2.domain1-2');
  234. });
  235. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  236. $api->post('/domain2-1', function () {
  237. return 'hi';
  238. })->name('v1.domain2-1');
  239. $api->get('domain2-2', function () {
  240. return 'hi';
  241. })->name('v1.domain2-2');
  242. $api->get('/prefix1/domain2-1', function () {
  243. return 'hi';
  244. })->name('v1.prefix1.domain2-1');
  245. $api->get('prefix1/domain2-2', function () {
  246. return 'hi';
  247. })->name('v1.prefix1.domain2-2');
  248. $api->get('/prefix2/domain2-1', function () {
  249. return 'hi';
  250. })->name('v1.prefix2.domain2-1');
  251. $api->get('prefix2/domain2-2', function () {
  252. return 'hi';
  253. })->name('v1.prefix2.domain2-2');
  254. });
  255. });
  256. $api->version('v2', function (Router $api) {
  257. $api->group(['domain' => 'domain1.app.test'], function (Router $api) {
  258. $api->post('/domain1', function () {
  259. return 'hi';
  260. })->name('v2.domain1');
  261. $api->get('/prefix1/domain1', function () {
  262. return 'hi';
  263. })->name('v2.prefix1.domain1');
  264. $api->get('/prefix2/domain1', function () {
  265. return 'hi';
  266. })->name('v2.prefix2.domain1');
  267. });
  268. $api->group(['domain' => 'domain2.app.test'], function (Router $api) {
  269. $api->post('/domain2', function () {
  270. return 'hi';
  271. })->name('v2.domain2');
  272. $api->get('/prefix1/domain2', function () {
  273. return 'hi';
  274. })->name('v2.prefix1.domain2');
  275. $api->get('/prefix2/domain2', function () {
  276. return 'hi';
  277. })->name('v2.prefix2.domain2');
  278. });
  279. });
  280. }
  281. }