GetFromResponseFieldAttributesTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ],
  45. 'other' => [
  46. 'type' => 'string',
  47. 'description' => '',
  48. ],
  49. ], $results);
  50. }
  51. /** @test */
  52. public function can_read_from_toArray_on_API_resources()
  53. {
  54. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  55. $e->controller = new ReflectionClass(ResponseFieldAttributeTestController::class);
  56. $e->method = $e->controller->getMethod('methodWithApiResourceResponse');
  57. $e->responses = new ResponseCollection([]);
  58. });
  59. $results = $this->fetch($endpoint);
  60. $this->assertArraySubset([
  61. 'id' => [
  62. 'type' => '',
  63. 'description' => 'The id of the pet.',
  64. ],
  65. 'species' => [
  66. 'type' => 'string',
  67. 'description' => 'The breed',
  68. ],
  69. ], $results);
  70. }
  71. protected function fetch($endpoint): array
  72. {
  73. $strategy = new GetFromResponseFieldAttribute(new DocumentationConfig([]));
  74. return $strategy($endpoint);
  75. }
  76. protected function endpoint(Closure $configure): ExtractedEndpointData
  77. {
  78. $endpoint = new class extends ExtractedEndpointData {
  79. public function __construct(array $parameters = [])
  80. {
  81. }
  82. };
  83. $configure($endpoint);
  84. return $endpoint;
  85. }
  86. }
  87. class ResponseFieldAttributeTestController
  88. {
  89. #[ResponseField('id', description: 'The id of the newly created user.')]
  90. #[ResponseField('other', 'string')]
  91. public function methodWithAttributes()
  92. {
  93. }
  94. #[ResponseFromApiResource(TestPetApiResource::class, TestPet::class)]
  95. public function methodWithApiResourceResponse()
  96. {
  97. }
  98. }