UseResponseFileTagTest.php 3.9 KB

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