GetFromQueryParamTagTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\QueryParameters;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamTag;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use Mpociot\Reflection\DocBlock\Tag;
  8. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  9. use PHPUnit\Framework\TestCase;
  10. class GetFromQueryParamTagTest extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. /** @test */
  14. public function can_fetch_from_queryparam_tag()
  15. {
  16. $strategy = new GetFromQueryParamTag(new DocumentationConfig([]));
  17. $tags = [
  18. new Tag('queryParam', 'location_id required The id of the location.'),
  19. new Tag('queryParam', 'user_id required The id of the user. Example: me'),
  20. new Tag('queryParam', 'page The page number. Example: 4'),
  21. new Tag('queryParam', 'with_type number Example: 13'),
  22. new Tag('queryParam', 'with_list_type int[]'),
  23. new Tag('queryParam', 'fields string[] The fields. Example: ["age", "name"]'),
  24. new Tag('queryParam', 'filters object The filters. '),
  25. new Tag('queryParam', 'filters.class double Class. Example: 11'),
  26. new Tag('queryParam', 'filters.other string required Other things.'),
  27. new Tag('queryParam', 'noExampleNoDescription No-example.'),
  28. new Tag('queryParam', 'noExample Something No-example'),
  29. ];
  30. $results = $strategy->getFromTags($tags);
  31. $this->assertArraySubset([
  32. 'location_id' => [
  33. 'type' => 'string',
  34. 'required' => true,
  35. 'description' => 'The id of the location.',
  36. ],
  37. 'user_id' => [
  38. 'type' => 'string',
  39. 'required' => true,
  40. 'description' => 'The id of the user.',
  41. 'example' => 'me',
  42. ],
  43. 'page' => [
  44. 'type' => 'integer',
  45. 'required' => false,
  46. 'description' => 'The page number.',
  47. 'example' => 4,
  48. ],
  49. 'with_type' => [
  50. 'type' => 'number',
  51. 'required' => false,
  52. 'description' => '',
  53. 'example' => 13.0,
  54. ],
  55. 'with_list_type' => [
  56. 'type' => 'integer[]',
  57. 'required' => false,
  58. 'description' => '',
  59. ],
  60. 'fields' => [
  61. 'type' => 'string[]',
  62. 'required' => false,
  63. 'description' => 'The fields.',
  64. 'example' => ['age', 'name']
  65. ],
  66. 'filters' => [
  67. 'type' => 'object',
  68. 'required' => false,
  69. 'description' => 'The filters.',
  70. ],
  71. 'filters.class' => [
  72. 'type' => 'number',
  73. 'required' => false,
  74. 'description' => 'Class.',
  75. 'example' => 11.0
  76. ],
  77. 'filters.other' => [
  78. 'type' => 'string',
  79. 'required' => true,
  80. 'description' => 'Other things.',
  81. ],
  82. 'noExampleNoDescription' => [
  83. 'type' => 'string',
  84. 'required' => false,
  85. 'description' => '',
  86. 'example' => null
  87. ],
  88. 'noExample' => [
  89. 'type' => 'string',
  90. 'required' => false,
  91. 'description' => 'Something',
  92. 'example' => null
  93. ],
  94. ], $results);
  95. }
  96. /** @test */
  97. public function can_fetch_from_form_request_method_argument()
  98. {
  99. $methodName = 'withFormRequestParameter';
  100. $method = new \ReflectionMethod(TestController::class, $methodName);
  101. $route = new Route(['POST'], "/$methodName", ['uses' => [TestController::class, $methodName]]);
  102. $strategy = new GetFromQueryParamTag(new DocumentationConfig([]));
  103. $results = $strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
  104. $this->assertArraySubset([
  105. 'location_id' => [
  106. 'required' => true,
  107. 'description' => 'The id of the location.',
  108. ],
  109. 'user_id' => [
  110. 'required' => true,
  111. 'description' => 'The id of the user.',
  112. 'example' => 'me',
  113. ],
  114. 'page' => [
  115. 'required' => true,
  116. 'description' => 'The page number.',
  117. 'example' => '4',
  118. ],
  119. ], $results);
  120. }
  121. }