GetFromResponseFieldAttributesTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\ResponseFields;
  3. use Closure;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Camel\Extraction\ResponseCollection;
  6. use Knuckles\Scribe\Attributes\ResponseField;
  7. use Knuckles\Scribe\Attributes\ResponseFromApiResource;
  8. use Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldAttribute;
  9. use Knuckles\Scribe\Tests\Fixtures\TestPet;
  10. use Knuckles\Scribe\Tests\Fixtures\TestPetApiResource;
  11. use Knuckles\Scribe\Tools\DocumentationConfig;
  12. use PHPUnit\Framework\TestCase;
  13. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  14. use ReflectionClass;
  15. class GetFromResponseFieldAttributesTest extends TestCase
  16. {
  17. use ArraySubsetAsserts;
  18. /** @test */
  19. public function can_fetch_from_responsefield_attribute()
  20. {
  21. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  22. $e->controller = new ReflectionClass(ResponseFieldAttributeTestController::class);
  23. $e->method = $e->controller->getMethod('methodWithAttributes');
  24. $e->responses = new ResponseCollection([
  25. [
  26. 'status' => 400,
  27. 'content' => json_encode(['id' => 6.4]),
  28. ],
  29. [
  30. 'status' => 200,
  31. 'content' => json_encode(['id' => 6]),
  32. ],
  33. [
  34. 'status' => 201,
  35. 'content' => json_encode(['id' => 'haha']),
  36. ],
  37. ]);
  38. });
  39. $results = $this->fetch($endpoint);
  40. $this->assertArraySubset([
  41. 'id' => [
  42. 'type' => 'integer',
  43. 'description' => 'The id of the newly created user.',
  44. 'required' => true,
  45. ],
  46. 'other' => [
  47. 'type' => 'string',
  48. 'description' => '',
  49. 'required' => true,
  50. ],
  51. 'required_attribute' => [
  52. 'required' => true,
  53. ],
  54. 'not_required_attribute' => [
  55. 'required' => false,
  56. ]
  57. ], $results);
  58. }
  59. /** @test */
  60. public function can_read_from_toArray_on_API_resources()
  61. {
  62. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  63. $e->controller = new ReflectionClass(ResponseFieldAttributeTestController::class);
  64. $e->method = $e->controller->getMethod('methodWithApiResourceResponse');
  65. $e->responses = new ResponseCollection([]);
  66. });
  67. $results = $this->fetch($endpoint);
  68. $this->assertArraySubset([
  69. 'id' => [
  70. 'type' => '',
  71. 'description' => 'The id of the pet.',
  72. ],
  73. 'species' => [
  74. 'type' => 'string',
  75. 'description' => 'The breed',
  76. ],
  77. ], $results);
  78. }
  79. protected function fetch($endpoint): array
  80. {
  81. $strategy = new GetFromResponseFieldAttribute(new DocumentationConfig([]));
  82. return $strategy($endpoint);
  83. }
  84. protected function endpoint(Closure $configure): ExtractedEndpointData
  85. {
  86. $endpoint = new class extends ExtractedEndpointData {
  87. public function __construct(array $parameters = [])
  88. {
  89. }
  90. };
  91. $configure($endpoint);
  92. return $endpoint;
  93. }
  94. }
  95. class ResponseFieldAttributeTestController
  96. {
  97. #[ResponseField('id', description: 'The id of the newly created user.')]
  98. #[ResponseField('other', 'string')]
  99. #[ResponseField('required_attribute', required: true)]
  100. #[ResponseField('not_required_attribute', required: false)]
  101. public function methodWithAttributes()
  102. {
  103. }
  104. #[ResponseFromApiResource(TestPetApiResource::class, TestPet::class)]
  105. public function methodWithApiResourceResponse()
  106. {
  107. }
  108. }