GetFromResponseFieldTagTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\ResponseFields;
  3. use Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag;
  4. use Knuckles\Scribe\Tools\DocumentationConfig;
  5. use Mpociot\Reflection\DocBlock\Tag;
  6. use PHPUnit\Framework\TestCase;
  7. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  8. class GetFromResponseFieldTagTest extends TestCase
  9. {
  10. use ArraySubsetAsserts;
  11. /** @test */
  12. public function can_fetch_from_responsefield_tag()
  13. {
  14. $strategy = new GetFromResponseFieldTag(new DocumentationConfig([]));
  15. $tags = [
  16. new Tag('responseField', 'id int The id of the newly created user.'),
  17. ];
  18. $results = $strategy->getResponseFieldsFromDocBlock($tags, []);
  19. $this->assertArraySubset([
  20. 'id' => [
  21. 'type' => 'integer',
  22. 'description' => 'The id of the newly created user.',
  23. ],
  24. ], $results);
  25. }
  26. /** @test */
  27. public function can_infer_type_from_first_2xx_response()
  28. {
  29. $strategy = new GetFromResponseFieldTag(new DocumentationConfig([]));
  30. $tags = [
  31. new Tag('responseField', 'id The id of the newly created user.'),
  32. ];
  33. $responses = [
  34. [
  35. 'status' => 400,
  36. 'content' => json_encode(['id' => 6.4]),
  37. ],
  38. [
  39. 'status' => 200,
  40. 'content' => json_encode(['id' => 6]),
  41. ],
  42. [
  43. 'status' => 201,
  44. 'content' => json_encode(['id' => 'haha']),
  45. ],
  46. ];
  47. $results = $strategy->getResponseFieldsFromDocBlock($tags, $responses);
  48. $this->assertArraySubset([
  49. 'id' => [
  50. 'type' => 'integer',
  51. 'description' => 'The id of the newly created user.',
  52. ],
  53. ], $results);
  54. }
  55. /** @test */
  56. public function can_infer_type_from_first_2xx_response_for_lists()
  57. {
  58. $strategy = new GetFromResponseFieldTag(new DocumentationConfig([]));
  59. $tags = [
  60. new Tag('responseField', 'id The id of the newly created user.'),
  61. ];
  62. $responses = [
  63. [
  64. 'status' => 200,
  65. 'content' => json_encode([['id' => 6]]),
  66. ],
  67. ];
  68. $results = $strategy->getResponseFieldsFromDocBlock($tags, $responses);
  69. $this->assertArraySubset([
  70. 'id' => [
  71. 'type' => 'integer',
  72. 'description' => 'The id of the newly created user.',
  73. ],
  74. ], $results);
  75. }
  76. /** @test */
  77. public function defaults_to_nothing_when_type_inference_fails()
  78. {
  79. $strategy = new GetFromResponseFieldTag(new DocumentationConfig([]));
  80. $tags = [
  81. new Tag('responseField', 'id The id of the newly created user.'),
  82. ];
  83. $results = $strategy->getResponseFieldsFromDocBlock($tags, []);
  84. $this->assertArraySubset([
  85. 'id' => [
  86. 'type' => '',
  87. 'description' => 'The id of the newly created user.',
  88. ],
  89. ], $results);
  90. }
  91. }