DingoGeneratorTest.php 16 KB

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