DingoGeneratorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests;
  3. use Orchestra\Testbench\TestCase;
  4. use Mpociot\ApiDoc\Generators\DingoGenerator;
  5. use Dingo\Api\Provider\LaravelServiceProvider;
  6. use Mpociot\ApiDoc\Tests\Fixtures\TestRequest;
  7. use Mpociot\ApiDoc\Tests\Fixtures\TestController;
  8. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  9. use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController;
  10. class DingoGeneratorTest extends TestCase
  11. {
  12. /**
  13. * @var \Mpociot\ApiDoc\Generators\DingoGenerator
  14. */
  15. protected $generator;
  16. protected function getPackageProviders($app)
  17. {
  18. return [
  19. LaravelServiceProvider::class,
  20. ApiDocGeneratorServiceProvider::class,
  21. ];
  22. }
  23. /**
  24. * Setup the test environment.
  25. */
  26. public function setUp()
  27. {
  28. parent::setUp();
  29. $this->generator = new DingoGenerator();
  30. }
  31. public function testCanParseMethodDescription()
  32. {
  33. if (version_compare($this->app->version(), '5.4', '>=')) {
  34. $this->markTestSkipped('Dingo does not support Laravel 5.4');
  35. }
  36. $api = app('Dingo\Api\Routing\Router');
  37. $api->version('v1', function ($api) {
  38. $api->get('/api/test', TestController::class.'@parseMethodDescription');
  39. });
  40. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
  41. $parsed = $this->generator->processRoute($route);
  42. $this->assertSame('Example title.', $parsed['title']);
  43. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  44. }
  45. public function testCanParseRouteMethods()
  46. {
  47. if (version_compare($this->app->version(), '5.4', '>=')) {
  48. $this->markTestSkipped('Dingo does not support Laravel 5.4');
  49. }
  50. $api = app('Dingo\Api\Routing\Router');
  51. $api->version('v1', function ($api) {
  52. $api->get('/get', TestController::class.'@dummy');
  53. $api->post('/post', TestController::class.'@dummy');
  54. $api->put('/put', TestController::class.'@dummy');
  55. $api->delete('/delete', TestController::class.'@dummy');
  56. });
  57. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
  58. $parsed = $this->generator->processRoute($route);
  59. $this->assertSame(['GET', 'HEAD'], $parsed['methods']);
  60. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[1];
  61. $parsed = $this->generator->processRoute($route);
  62. $this->assertSame(['POST'], $parsed['methods']);
  63. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[2];
  64. $parsed = $this->generator->processRoute($route);
  65. $this->assertSame(['PUT'], $parsed['methods']);
  66. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[3];
  67. $parsed = $this->generator->processRoute($route);
  68. $this->assertSame(['DELETE'], $parsed['methods']);
  69. }
  70. public function testCanParseFormRequestRules()
  71. {
  72. if (version_compare($this->app->version(), '5.4', '>=')) {
  73. $this->markTestSkipped('Dingo does not support Laravel 5.4');
  74. }
  75. $api = app('Dingo\Api\Routing\Router');
  76. $api->version('v1', function ($api) {
  77. $api->post('/post', DingoTestController::class.'@parseFormRequestRules');
  78. });
  79. $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
  80. $parsed = $this->generator->processRoute($route);
  81. $parameters = $parsed['parameters'];
  82. $testRequest = new TestRequest();
  83. $rules = $testRequest->rules();
  84. foreach ($rules as $name => $rule) {
  85. $attribute = $parameters[$name];
  86. switch ($name) {
  87. case 'required':
  88. $this->assertTrue($attribute['required']);
  89. $this->assertSame('string', $attribute['type']);
  90. $this->assertCount(0, $attribute['description']);
  91. break;
  92. case 'accepted':
  93. $this->assertTrue($attribute['required']);
  94. $this->assertSame('boolean', $attribute['type']);
  95. $this->assertCount(0, $attribute['description']);
  96. break;
  97. case 'active_url':
  98. $this->assertFalse($attribute['required']);
  99. $this->assertSame('url', $attribute['type']);
  100. $this->assertCount(0, $attribute['description']);
  101. break;
  102. case 'alpha':
  103. $this->assertFalse($attribute['required']);
  104. $this->assertSame('string', $attribute['type']);
  105. $this->assertCount(1, $attribute['description']);
  106. $this->assertSame('Only alphabetic characters allowed', $attribute['description'][0]);
  107. break;
  108. case 'alpha_dash':
  109. $this->assertFalse($attribute['required']);
  110. $this->assertSame('string', $attribute['type']);
  111. $this->assertCount(1, $attribute['description']);
  112. $this->assertSame('Allowed: alpha-numeric characters, as well as dashes and underscores.', $attribute['description'][0]);
  113. break;
  114. case 'alpha_num':
  115. $this->assertFalse($attribute['required']);
  116. $this->assertSame('string', $attribute['type']);
  117. $this->assertCount(1, $attribute['description']);
  118. $this->assertSame('Only alpha-numeric characters allowed', $attribute['description'][0]);
  119. break;
  120. case 'array':
  121. $this->assertFalse($attribute['required']);
  122. $this->assertSame('array', $attribute['type']);
  123. $this->assertCount(0, $attribute['description']);
  124. break;
  125. case 'between':
  126. $this->assertFalse($attribute['required']);
  127. $this->assertSame('numeric', $attribute['type']);
  128. $this->assertCount(1, $attribute['description']);
  129. $this->assertSame('Between: `5` and `200`', $attribute['description'][0]);
  130. break;
  131. case 'string_between':
  132. $this->assertFalse($attribute['required']);
  133. $this->assertSame('string', $attribute['type']);
  134. $this->assertCount(1, $attribute['description']);
  135. $this->assertSame('Between: `5` and `200`', $attribute['description'][0]);
  136. break;
  137. case 'before':
  138. $this->assertFalse($attribute['required']);
  139. $this->assertSame('date', $attribute['type']);
  140. $this->assertCount(1, $attribute['description']);
  141. $this->assertSame('Must be a date preceding: `Saturday, 23-Apr-16 14:31:00 UTC`', $attribute['description'][0]);
  142. break;
  143. case 'boolean':
  144. $this->assertFalse($attribute['required']);
  145. $this->assertSame('boolean', $attribute['type']);
  146. $this->assertCount(0, $attribute['description']);
  147. break;
  148. case 'date':
  149. $this->assertFalse($attribute['required']);
  150. $this->assertSame('date', $attribute['type']);
  151. $this->assertCount(0, $attribute['description']);
  152. break;
  153. case 'date_format':
  154. $this->assertFalse($attribute['required']);
  155. $this->assertSame('date', $attribute['type']);
  156. $this->assertCount(1, $attribute['description']);
  157. $this->assertSame('Date format: `j.n.Y H:iP`', $attribute['description'][0]);
  158. break;
  159. case 'different':
  160. $this->assertFalse($attribute['required']);
  161. $this->assertSame('string', $attribute['type']);
  162. $this->assertCount(1, $attribute['description']);
  163. $this->assertSame('Must have a different value than parameter: `alpha_num`', $attribute['description'][0]);
  164. break;
  165. case 'digits':
  166. $this->assertFalse($attribute['required']);
  167. $this->assertSame('numeric', $attribute['type']);
  168. $this->assertCount(1, $attribute['description']);
  169. $this->assertSame('Must have an exact length of `2`', $attribute['description'][0]);
  170. break;
  171. case 'digits_between':
  172. $this->assertFalse($attribute['required']);
  173. $this->assertSame('numeric', $attribute['type']);
  174. $this->assertCount(1, $attribute['description']);
  175. $this->assertSame('Must have a length between `2` and `10`', $attribute['description'][0]);
  176. break;
  177. case 'email':
  178. $this->assertFalse($attribute['required']);
  179. $this->assertSame('email', $attribute['type']);
  180. $this->assertCount(0, $attribute['description']);
  181. break;
  182. case 'exists':
  183. $this->assertFalse($attribute['required']);
  184. $this->assertSame('string', $attribute['type']);
  185. $this->assertCount(1, $attribute['description']);
  186. $this->assertSame('Valid user firstname', $attribute['description'][0]);
  187. break;
  188. case 'single_exists':
  189. $this->assertFalse($attribute['required']);
  190. $this->assertSame('string', $attribute['type']);
  191. $this->assertCount(1, $attribute['description']);
  192. $this->assertSame('Valid user single_exists', $attribute['description'][0]);
  193. break;
  194. case 'file':
  195. $this->assertFalse($attribute['required']);
  196. $this->assertSame('file', $attribute['type']);
  197. $this->assertCount(1, $attribute['description']);
  198. $this->assertSame('Must be a file upload', $attribute['description'][0]);
  199. break;
  200. case 'image':
  201. $this->assertFalse($attribute['required']);
  202. $this->assertSame('image', $attribute['type']);
  203. $this->assertCount(1, $attribute['description']);
  204. $this->assertSame('Must be an image (jpeg, png, bmp, gif, or svg)', $attribute['description'][0]);
  205. break;
  206. case 'in':
  207. $this->assertFalse($attribute['required']);
  208. $this->assertSame('string', $attribute['type']);
  209. $this->assertCount(1, $attribute['description']);
  210. $this->assertSame('`jpeg`, `png`, `bmp`, `gif` or `svg`', $attribute['description'][0]);
  211. break;
  212. case 'integer':
  213. $this->assertFalse($attribute['required']);
  214. $this->assertSame('integer', $attribute['type']);
  215. $this->assertCount(0, $attribute['description']);
  216. break;
  217. case 'ip':
  218. $this->assertFalse($attribute['required']);
  219. $this->assertSame('ip', $attribute['type']);
  220. $this->assertCount(0, $attribute['description']);
  221. break;
  222. case 'json':
  223. $this->assertFalse($attribute['required']);
  224. $this->assertSame('string', $attribute['type']);
  225. $this->assertCount(1, $attribute['description']);
  226. $this->assertSame('Must be a valid JSON string.', $attribute['description'][0]);
  227. break;
  228. case 'max':
  229. $this->assertFalse($attribute['required']);
  230. $this->assertSame('string', $attribute['type']);
  231. $this->assertCount(1, $attribute['description']);
  232. $this->assertSame('Maximum: `10`', $attribute['description'][0]);
  233. break;
  234. case 'min':
  235. $this->assertFalse($attribute['required']);
  236. $this->assertSame('string', $attribute['type']);
  237. $this->assertCount(1, $attribute['description']);
  238. $this->assertSame('Minimum: `20`', $attribute['description'][0]);
  239. break;
  240. case 'mimes':
  241. $this->assertFalse($attribute['required']);
  242. $this->assertSame('string', $attribute['type']);
  243. $this->assertCount(1, $attribute['description']);
  244. $this->assertSame('Allowed mime types: `jpeg`, `bmp` or `png`', $attribute['description'][0]);
  245. break;
  246. case 'not_in':
  247. $this->assertFalse($attribute['required']);
  248. $this->assertSame('string', $attribute['type']);
  249. $this->assertCount(1, $attribute['description']);
  250. $this->assertSame('Not in: `foo` or `bar`', $attribute['description'][0]);
  251. break;
  252. case 'numeric':
  253. $this->assertFalse($attribute['required']);
  254. $this->assertSame('numeric', $attribute['type']);
  255. $this->assertCount(0, $attribute['description']);
  256. break;
  257. case 'regex':
  258. $this->assertFalse($attribute['required']);
  259. $this->assertSame('string', $attribute['type']);
  260. $this->assertCount(1, $attribute['description']);
  261. $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]);
  262. break;
  263. case 'multiple_required_if':
  264. $this->assertFalse($attribute['required']);
  265. $this->assertSame('string', $attribute['type']);
  266. $this->assertCount(1, $attribute['description']);
  267. $this->assertSame('Required if `foo` is `bar` or `baz` is `qux`', $attribute['description'][0]);
  268. break;
  269. case 'required_if':
  270. $this->assertFalse($attribute['required']);
  271. $this->assertSame('string', $attribute['type']);
  272. $this->assertCount(1, $attribute['description']);
  273. $this->assertSame('Required if `foo` is `bar`', $attribute['description'][0]);
  274. break;
  275. case 'required_unless':
  276. $this->assertFalse($attribute['required']);
  277. $this->assertSame('string', $attribute['type']);
  278. $this->assertCount(1, $attribute['description']);
  279. $this->assertSame('Required unless `foo` is `bar`', $attribute['description'][0]);
  280. break;
  281. case 'required_with':
  282. $this->assertFalse($attribute['required']);
  283. $this->assertSame('string', $attribute['type']);
  284. $this->assertCount(1, $attribute['description']);
  285. $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are present.', $attribute['description'][0]);
  286. break;
  287. case 'required_with_all':
  288. $this->assertFalse($attribute['required']);
  289. $this->assertSame('string', $attribute['type']);
  290. $this->assertCount(1, $attribute['description']);
  291. $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are present.', $attribute['description'][0]);
  292. break;
  293. case 'required_without':
  294. $this->assertFalse($attribute['required']);
  295. $this->assertSame('string', $attribute['type']);
  296. $this->assertCount(1, $attribute['description']);
  297. $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are not present.', $attribute['description'][0]);
  298. break;
  299. case 'required_without_all':
  300. $this->assertFalse($attribute['required']);
  301. $this->assertSame('string', $attribute['type']);
  302. $this->assertCount(1, $attribute['description']);
  303. $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are not present.', $attribute['description'][0]);
  304. break;
  305. case 'same':
  306. $this->assertFalse($attribute['required']);
  307. $this->assertSame('string', $attribute['type']);
  308. $this->assertCount(1, $attribute['description']);
  309. $this->assertSame('Must be the same as `foo`', $attribute['description'][0]);
  310. break;
  311. case 'size':
  312. $this->assertFalse($attribute['required']);
  313. $this->assertSame('string', $attribute['type']);
  314. $this->assertCount(1, $attribute['description']);
  315. $this->assertSame('Must have the size of `51`', $attribute['description'][0]);
  316. break;
  317. case 'timezone':
  318. $this->assertFalse($attribute['required']);
  319. $this->assertSame('string', $attribute['type']);
  320. $this->assertCount(1, $attribute['description']);
  321. $this->assertSame('Must be a valid timezone identifier', $attribute['description'][0]);
  322. break;
  323. case 'url':
  324. $this->assertFalse($attribute['required']);
  325. $this->assertSame('url', $attribute['type']);
  326. $this->assertCount(0, $attribute['description']);
  327. break;
  328. }
  329. }
  330. }
  331. }