GetFromResponseFieldTagTest.php 3.2 KB

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