ApiDocGeneratorTest.php 16 KB

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