ApiDocGeneratorTest.php 16 KB

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