GetFromUrlParamAttributeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\UrlParameters;
  3. use Closure;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Attributes\UrlParam;
  6. use Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamAttribute;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use PHPUnit\Framework\TestCase;
  9. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  10. use ReflectionClass;
  11. use ReflectionFunction;
  12. class GetFromUrlParamAttributeTest extends TestCase
  13. {
  14. use ArraySubsetAsserts;
  15. /** @test */
  16. public function can_fetch_from_urlparam_attribute()
  17. {
  18. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  19. $e->controller = new ReflectionClass(UrlParamAttributeTestController::class);
  20. $e->method = $e->controller->getMethod('methodWithAttributes');
  21. });
  22. $results = $this->fetch($endpoint);
  23. $this->assertArraySubset([
  24. 'id' => [
  25. 'type' => 'string',
  26. 'required' => true,
  27. 'description' => 'The id of the order.',
  28. ],
  29. 'lang' => [
  30. 'type' => 'string',
  31. 'required' => false,
  32. 'description' => 'Will be inherited.',
  33. ],
  34. 'withType' => [
  35. 'type' => 'number',
  36. 'required' => false,
  37. 'description' => 'With type, maybe.',
  38. ],
  39. 'withTypeDefinitely' => [
  40. 'type' => 'integer',
  41. 'required' => true,
  42. 'description' => 'With type.',
  43. ],
  44. 'barebones' => [
  45. 'type' => 'string',
  46. 'required' => true,
  47. 'description' => '',
  48. ],
  49. 'barebonesType' => [
  50. 'type' => 'number',
  51. 'required' => true,
  52. 'description' => '',
  53. ],
  54. 'barebonesOptional' => [
  55. 'type' => 'string',
  56. 'required' => false,
  57. 'description' => '',
  58. ],
  59. 'withExampleOnly' => [
  60. 'type' => 'string',
  61. 'required' => true,
  62. 'description' => '',
  63. 'example' => '12',
  64. ],
  65. 'withExampleOnlyButTyped' => [
  66. 'type' => 'integer',
  67. 'required' => true,
  68. 'description' => '',
  69. 'example' => 12
  70. ],
  71. 'noExampleNoDescription' => [
  72. 'type' => 'string',
  73. 'required' => true,
  74. 'description' => '',
  75. 'example' => null
  76. ],
  77. 'noExample' => [
  78. 'type' => 'string',
  79. 'required' => true,
  80. 'description' => 'Something',
  81. 'example' => null
  82. ],
  83. ], $results);
  84. }
  85. /** @test */
  86. public function can_fetch_from_urlparam_attribute_on_closure()
  87. {
  88. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  89. $e->controller = null;
  90. $e->method = new ReflectionFunction('\Knuckles\Scribe\Tests\Strategies\UrlParameters\functionWithAttributes');
  91. });
  92. $results = $this->fetch($endpoint);
  93. $this->assertArraySubset([
  94. 'a_parameter' => [
  95. 'type' => 'string',
  96. 'required' => false,
  97. 'description' => 'Described',
  98. 'example' => 'en',
  99. ],
  100. ], $results);
  101. }
  102. protected function fetch($endpoint): array
  103. {
  104. $strategy = new GetFromUrlParamAttribute(new DocumentationConfig([]));
  105. return $strategy($endpoint, []);
  106. }
  107. protected function endpoint(Closure $configure): ExtractedEndpointData
  108. {
  109. $endpoint = new class extends ExtractedEndpointData {
  110. public function __construct(array $parameters = []) {}
  111. };
  112. $configure($endpoint);
  113. return $endpoint;
  114. }
  115. }
  116. #[UrlParam("id", description: "Will be overriden.")]
  117. #[UrlParam("lang", description: "Will be inherited.", required: false)]
  118. class UrlParamAttributeTestController
  119. {
  120. #[UrlParam("id", description: "The id of the order.")]
  121. #[UrlParam("withType", required: false, type: "number", description: "With type, maybe.")]
  122. #[UrlParam("withTypeDefinitely", "integer", "With type.")]
  123. #[UrlParam("barebones")]
  124. #[UrlParam("barebonesType", type: "number")]
  125. #[UrlParam("barebonesOptional", required: false)]
  126. #[UrlParam("withExampleOnly", example: "12")]
  127. #[UrlParam("withExampleOnlyButTyped", type: "int", example: 12)]
  128. #[UrlParam("noExampleNoDescription", example: "No-example.")]
  129. #[UrlParam("noExample", description: "Something", example: "No-example")]
  130. public function methodWithAttributes()
  131. {
  132. }
  133. }
  134. #[UrlParam("a_parameter", required: false, type: "string", description: "Described", example: "en")]
  135. function functionWithAttributes()
  136. {
  137. }