UseResponseFileTagTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\Responses;
  3. use Illuminate\Support\Facades\Route as RouteFacade;
  4. use Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use Mpociot\Reflection\DocBlock\Tag;
  8. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  9. use PHPUnit\Framework\TestCase;
  10. class UseResponseFileTagTest extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. /**
  14. * @test
  15. * @dataProvider responseFileTags
  16. */
  17. public function allows_multiple_responsefile_tags_for_multiple_statuses_and_scenarios(array $tags, array $expected)
  18. {
  19. $filePath = __DIR__ . '/../../Fixtures/response_test.json';
  20. $filePath2 = __DIR__ . '/../../Fixtures/response_error_test.json';
  21. $strategy = new UseResponseFileTag(new DocumentationConfig([]));
  22. $results = $strategy->getFileResponses($tags);
  23. $this->assertArraySubset([
  24. [
  25. 'status' => 200,
  26. 'description' => $expected[0]['description'],
  27. 'content' => file_get_contents($filePath),
  28. ],
  29. [
  30. 'status' => 401,
  31. 'description' => $expected[1]['description'],
  32. 'content' => file_get_contents($filePath2),
  33. ],
  34. ], $results);
  35. }
  36. public function responseFileTags()
  37. {
  38. return [
  39. "with status as initial position" => [
  40. [
  41. new Tag('responseFile', 'tests/Fixtures/response_test.json'),
  42. new Tag('responseFile', '401 tests/Fixtures/response_error_test.json'),
  43. ],
  44. [
  45. [
  46. 'status' => 200,
  47. 'description' => '200',
  48. ],
  49. [
  50. 'status' => 401,
  51. 'description' => '401',
  52. ],
  53. ],
  54. ],
  55. "with fields" => [
  56. [
  57. new Tag('responseFile', 'scenario="success" tests/Fixtures/response_test.json'),
  58. new Tag('responseFile', 'status=401 scenario=\'auth problem\' tests/Fixtures/response_error_test.json'),
  59. ],
  60. [
  61. [
  62. 'status' => 200,
  63. 'description' => '200, success',
  64. ],
  65. [
  66. 'status' => 401,
  67. 'description' => '401, auth problem',
  68. ],
  69. ],
  70. ],
  71. ];
  72. }
  73. /** @test */
  74. public function can_add_or_replace_key_value_pair_in_response_file()
  75. {
  76. $strategy = new UseResponseFileTag(new DocumentationConfig([]));
  77. $tags = [
  78. new Tag('responseFile', 'tests/Fixtures/response_test.json {"message" : "Serendipity", "gender": "male"}'),
  79. ];
  80. $results = $strategy->getFileResponses($tags);
  81. $this->assertArraySubset([
  82. [
  83. 'status' => 200,
  84. 'content' => '{"id":5,"name":"Jessica Jones","gender":"male","message":"Serendipity"}',
  85. ],
  86. ], $results);
  87. }
  88. /** @test */
  89. public function supports_relative_or_absolute_paths()
  90. {
  91. $filePath = __DIR__ . '/../../Fixtures/response_test.json';
  92. $strategy = new UseResponseFileTag(new DocumentationConfig([]));
  93. $tags = [new Tag('responseFile', 'tests/Fixtures/response_test.json')];
  94. $this->assertArraySubset([
  95. [
  96. 'status' => 200,
  97. 'content' => file_get_contents($filePath),
  98. ],
  99. ], $strategy->getFileResponses($tags));
  100. $tags = [new Tag('responseFile', realpath($filePath))];
  101. $this->assertArraySubset([
  102. [
  103. 'status' => 200,
  104. 'content' => file_get_contents($filePath),
  105. ],
  106. ], $strategy->getFileResponses($tags));
  107. }
  108. }