GetFromResponseFieldTagTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use Mpociot\Reflection\DocBlock\Tag;
  9. use PHPUnit\Framework\TestCase;
  10. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  11. class GetFromResponseFieldTagTest extends TestCase
  12. {
  13. use ArraySubsetAsserts;
  14. /** @test */
  15. public function can_fetch_from_responsefield_tag()
  16. {
  17. $tags = [
  18. new Tag('responseField', 'id int The id of the newly created user.'),
  19. new Tag('responseField', 'other string'),
  20. ];
  21. $results = $this->fetch($tags);
  22. $this->assertArraySubset([
  23. 'id' => [
  24. 'type' => 'integer',
  25. 'description' => 'The id of the newly created user.',
  26. ],
  27. 'other' => [
  28. 'type' => 'string',
  29. 'description' => '',
  30. ],
  31. ], $results);
  32. }
  33. /** @test */
  34. public function can_infer_type_from_first_2xx_response()
  35. {
  36. $responses = [
  37. [
  38. 'status' => 400,
  39. 'content' => json_encode(['id' => 6.4]),
  40. ],
  41. [
  42. 'status' => 200,
  43. 'content' => json_encode(['id' => 6]),
  44. ],
  45. [
  46. 'status' => 201,
  47. 'content' => json_encode(['id' => 'haha']),
  48. ],
  49. ];
  50. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) use ($responses) {
  51. $e->responses = new ResponseCollection($responses);
  52. });
  53. $tags = [
  54. new Tag('responseField', 'id The id of the newly created user.'),
  55. ];
  56. $results = $this->fetch($tags, $endpoint);
  57. $this->assertArraySubset([
  58. 'id' => [
  59. 'type' => 'integer',
  60. 'description' => 'The id of the newly created user.',
  61. ],
  62. ], $results);
  63. }
  64. /** @test */
  65. public function can_infer_type_from_first_2xx_response_for_lists()
  66. {
  67. $responses = [
  68. [
  69. 'status' => 200,
  70. 'content' => json_encode([['id' => 6]]),
  71. ],
  72. ];
  73. $tags = [
  74. new Tag('responseField', 'id The id of the newly created user.'),
  75. ];
  76. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) use ($responses) {
  77. $e->responses = new ResponseCollection($responses);
  78. });
  79. $results = $this->fetch($tags, $endpoint);
  80. $this->assertArraySubset([
  81. 'id' => [
  82. 'type' => 'integer',
  83. 'description' => 'The id of the newly created user.',
  84. ],
  85. ], $results);
  86. }
  87. /** @test */
  88. public function defaults_to_nothing_when_type_inference_fails()
  89. {
  90. $tags = [
  91. new Tag('responseField', 'id The id of the newly created user.'),
  92. ];
  93. $results = $this->fetch($tags);
  94. $this->assertArraySubset([
  95. 'id' => [
  96. 'type' => '',
  97. 'description' => 'The id of the newly created user.',
  98. ],
  99. ], $results);
  100. }
  101. protected function fetch($tags, $endpoint = null): array
  102. {
  103. $strategy = new GetFromResponseFieldTag(new DocumentationConfig([]));
  104. $strategy->endpointData = $endpoint ?: $this->endpoint(function (ExtractedEndpointData $e) {
  105. $e->responses = new ResponseCollection([]);
  106. });
  107. return $strategy->getFromTags($tags);
  108. }
  109. protected function endpoint(Closure $configure): ExtractedEndpointData
  110. {
  111. $endpoint = new class extends ExtractedEndpointData {
  112. public function __construct(array $parameters = []) {}
  113. };
  114. $configure($endpoint);
  115. return $endpoint;
  116. }
  117. }