GetFromResponseFieldAttributesTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Extracting\Strategies\ResponseFields\GetFromResponseFieldAttribute;
  8. use Knuckles\Scribe\Tools\DocumentationConfig;
  9. use PHPUnit\Framework\TestCase;
  10. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  11. use ReflectionClass;
  12. class GetFromResponseFieldAttributesTest extends TestCase
  13. {
  14. use ArraySubsetAsserts;
  15. /** @test */
  16. public function can_fetch_from_responsefield_attribute()
  17. {
  18. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  19. $e->controller = new ReflectionClass(ResponseFieldAttributeTestController::class);
  20. $e->method = $e->controller->getMethod('methodWithAttributes');
  21. $e->responses = new ResponseCollection([
  22. [
  23. 'status' => 400,
  24. 'content' => json_encode(['id' => 6.4]),
  25. ],
  26. [
  27. 'status' => 200,
  28. 'content' => json_encode(['id' => 6]),
  29. ],
  30. [
  31. 'status' => 201,
  32. 'content' => json_encode(['id' => 'haha']),
  33. ],
  34. ]);
  35. });
  36. $results = $this->fetch($endpoint);
  37. $this->assertArraySubset([
  38. 'id' => [
  39. 'type' => 'integer',
  40. 'description' => 'The id of the newly created user.',
  41. ],
  42. 'other' => [
  43. 'type' => 'string',
  44. 'description' => '',
  45. ],
  46. ], $results);
  47. }
  48. protected function fetch($endpoint): array
  49. {
  50. $strategy = new GetFromResponseFieldAttribute(new DocumentationConfig([]));
  51. return $strategy($endpoint);
  52. }
  53. protected function endpoint(Closure $configure): ExtractedEndpointData
  54. {
  55. $endpoint = new class extends ExtractedEndpointData {
  56. public function __construct(array $parameters = [])
  57. {
  58. }
  59. };
  60. $configure($endpoint);
  61. return $endpoint;
  62. }
  63. }
  64. class ResponseFieldAttributeTestController
  65. {
  66. #[ResponseField('id', description: 'The id of the newly created user.')]
  67. #[ResponseField('other', 'string')]
  68. public function methodWithAttributes()
  69. {
  70. }
  71. }