GetFromQueryParamAttributeTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\QueryParameters;
  3. use Closure;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Attributes\QueryParam;
  6. use Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamAttribute;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use PHPUnit\Framework\TestCase;
  9. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  10. use ReflectionClass;
  11. class GetFromQueryParamAttributeTest extends TestCase
  12. {
  13. use ArraySubsetAsserts;
  14. /** @test */
  15. public function can_fetch_from_queryparam_attribute()
  16. {
  17. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  18. $e->controller = new ReflectionClass(QueryParamAttributeTestController::class);
  19. $e->method = $e->controller->getMethod('methodWithAttributes');
  20. });
  21. $results = $this->fetch($endpoint);
  22. $this->assertArraySubset([
  23. 'location_id' => [
  24. 'type' => 'string',
  25. 'required' => true,
  26. 'description' => 'The id of the location.',
  27. ],
  28. 'user_id' => [
  29. 'type' => 'string',
  30. 'required' => true,
  31. 'description' => 'The id of the user.',
  32. 'example' => 'me',
  33. ],
  34. 'page' => [
  35. 'type' => 'integer',
  36. 'required' => false,
  37. 'description' => 'The page number.',
  38. 'example' => 4,
  39. ],
  40. 'with_type' => [
  41. 'type' => 'number',
  42. 'required' => false,
  43. 'description' => '',
  44. 'example' => 13.0,
  45. ],
  46. 'with_list_type' => [
  47. 'type' => 'integer[]',
  48. 'required' => false,
  49. 'description' => '',
  50. ],
  51. 'fields' => [
  52. 'type' => 'string[]',
  53. 'required' => false,
  54. 'description' => 'The fields.',
  55. 'example' => ['age', 'name']
  56. ],
  57. 'filters' => [
  58. 'type' => 'object',
  59. 'required' => false,
  60. 'description' => 'The filters.',
  61. ],
  62. 'filters.class' => [
  63. 'type' => 'number',
  64. 'required' => false,
  65. 'description' => 'Class.',
  66. 'example' => 11.0
  67. ],
  68. 'filters.other' => [
  69. 'type' => 'string',
  70. 'required' => true,
  71. 'description' => 'Other things.',
  72. ],
  73. 'noExampleNoDescription' => [
  74. 'type' => 'string',
  75. 'required' => true,
  76. 'description' => '',
  77. 'example' => null
  78. ],
  79. 'noExample' => [
  80. 'type' => 'string',
  81. 'required' => true,
  82. 'description' => 'Something',
  83. 'example' => null
  84. ],
  85. ], $results);
  86. }
  87. protected function fetch($endpoint): array
  88. {
  89. $strategy = new GetFromQueryParamAttribute(new DocumentationConfig([]));
  90. return $strategy($endpoint, []);
  91. }
  92. protected function endpoint(Closure $configure): ExtractedEndpointData
  93. {
  94. $endpoint = new class extends ExtractedEndpointData {
  95. public function __construct(array $parameters = []) {}
  96. };
  97. $configure($endpoint);
  98. return $endpoint;
  99. }
  100. }
  101. #[QueryParam("user_id", description: "Will be overriden.")]
  102. #[QueryParam("location_id", description: "The id of the location.")]
  103. class QueryParamAttributeTestController
  104. {
  105. #[QueryParam("user_id", description: "The id of the user.", example: "me")]
  106. #[QueryParam("page", 'integer', description: "The page number.", required: false, example: 4)]
  107. #[QueryParam("with_type", "number", example: 13.0, required: false)]
  108. #[QueryParam("with_list_type", type: "int[]", required: false)]
  109. #[QueryParam("fields", "string[]", "The fields.", required: false, example: ["age", "name"])]
  110. #[QueryParam("filters", "object", "The filters. ", required: false)]
  111. #[QueryParam("filters.class", "double", required: false, example: 11.0, description: "Class.")]
  112. #[QueryParam("filters.other", "string", description: "Other things.")]
  113. #[QueryParam("noExampleNoDescription", example: "No-example")]
  114. #[QueryParam("noExample", description: "Something", example: "No-example")]
  115. public function methodWithAttributes()
  116. {
  117. }
  118. }