DingoGeneratorTest.php 17 KB

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