ApiDocGeneratorTest.php 16 KB

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