ApiDocGeneratorTest.php 16 KB

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